| 1 |
unit Img32; |
| 2 |
|
| 3 |
(******************************************************************************* |
| 4 |
* Author : Angus Johnson * |
| 5 |
* Version : 4.8 * |
| 6 |
* Date : 10 January 2025 * |
| 7 |
* Website : http://www.angusj.com * |
| 8 |
* Copyright : Angus Johnson 2019-2025 * |
| 9 |
* Purpose : The core module of the Image32 library * |
| 10 |
* License : http://www.boost.org/LICENSE_1_0.txt * |
| 11 |
*******************************************************************************) |
| 12 |
|
| 13 |
interface |
| 14 |
|
| 15 |
{$I Img32.inc} |
| 16 |
|
| 17 |
uses |
| 18 |
Types, SysUtils, Classes, |
| 19 |
{$IFDEF MSWINDOWS} Windows,{$ENDIF} |
| 20 |
{$IFDEF USING_VCL_LCL} |
| 21 |
{$IFDEF USES_NAMESPACES} Vcl.Graphics, Vcl.Forms, |
| 22 |
{$ELSE}Graphics, Forms, |
| 23 |
{$ENDIF} |
| 24 |
{$ENDIF} |
| 25 |
{$IFDEF XPLAT_GENERICS} |
| 26 |
Generics.Collections, Generics.Defaults, Character, |
| 27 |
{$ENDIF} |
| 28 |
{$IFDEF UITYPES} UITypes,{$ENDIF} Math; |
| 29 |
|
| 30 |
type |
| 31 |
{$IF not declared(SizeInt)} // FPC has SizeInt |
| 32 |
{$IF CompilerVersion < 20.0} |
| 33 |
SizeInt = Integer; // Delphi 7-2007 can't use NativeInt with "FOR" |
| 34 |
SizeUInt = Cardinal; // Delphi 7-2007 can't use NativeUInt with "FOR" |
| 35 |
{$ELSE} |
| 36 |
SizeInt = NativeInt; |
| 37 |
SizeUInt = NativeUInt; |
| 38 |
{$IFEND} |
| 39 |
{$IFEND} |
| 40 |
|
| 41 |
TRect = Types.TRect; |
| 42 |
TColor32 = type Cardinal; |
| 43 |
|
| 44 |
TPointD = record |
| 45 |
X, Y: double; |
| 46 |
end; |
| 47 |
|
| 48 |
PARGB = ^TARGB; |
| 49 |
TARGB = packed record |
| 50 |
case boolean of |
| 51 |
false: (B: Byte; G: Byte; R: Byte; A: Byte); |
| 52 |
true : (Color: TColor32); |
| 53 |
end; |
| 54 |
TArrayOfARGB = array of TARGB; |
| 55 |
|
| 56 |
const |
| 57 |
clNone32 = TColor32($00000000); |
| 58 |
clAqua32 = TColor32($FF00FFFF); |
| 59 |
clBlack32 = TColor32($FF000000); |
| 60 |
clBlue32 = TColor32($FF0000FF); |
| 61 |
clFuchsia32 = TColor32($FFFF00FF); |
| 62 |
clGray32 = TColor32($FF808080); |
| 63 |
clGreen32 = TColor32($FF008000); |
| 64 |
clGrey32 = TColor32($FF808080); |
| 65 |
clLime32 = TColor32($FF00FF00); |
| 66 |
clMaroon32 = TColor32($FF800000); |
| 67 |
clNavy32 = TColor32($FF000080); |
| 68 |
clOlive32 = TColor32($FF7F7F00); |
| 69 |
clOrange32 = TColor32($FFFF7F00); |
| 70 |
clPurple32 = TColor32($FF7F00FF); |
| 71 |
clRed32 = TColor32($FFFF0000); |
| 72 |
clSilver32 = TColor32($FFC0C0C0); |
| 73 |
clTeal32 = TColor32($FF007F7F); |
| 74 |
clWhite32 = TColor32($FFFFFFFF); |
| 75 |
clYellow32 = TColor32($FFFFFF00); |
| 76 |
|
| 77 |
//custom gray colors |
| 78 |
clDarkGray32 = TColor32($FF505050); |
| 79 |
clDarkGrey32 = TColor32($FF505050); |
| 80 |
//clGray32 = TColor32($FF808080); |
| 81 |
//clSilver32 = TColor32($FFC0C0C0); |
| 82 |
clLiteGray32 = TColor32($FFD3D3D3); |
| 83 |
clLiteGrey32 = TColor32($FFD3D3D3); |
| 84 |
clPaleGray32 = TColor32($FFE0E0E0); |
| 85 |
clPaleGrey32 = TColor32($FFE0E0E0); |
| 86 |
clDarkBtn32 = TColor32($FFE8E8E8); |
| 87 |
clBtnFace32 = TColor32($FFF0F0F0); |
| 88 |
clLiteBtn32 = TColor32($FFF8F8F8); |
| 89 |
|
| 90 |
defaultCompression = -1; |
| 91 |
|
| 92 |
{$IFDEF ZEROBASEDSTR} |
| 93 |
{$ZEROBASEDSTRINGS OFF} |
| 94 |
{$ENDIF} |
| 95 |
|
| 96 |
RT_BITMAP = PChar(2); |
| 97 |
|
| 98 |
type |
| 99 |
{$IFDEF SUPPORTS_POINTERMATH} |
| 100 |
// Works for Delphi 2009 and newer. For FPC, POINTERMATH is |
| 101 |
// a requirement for negative indices. Otherwise 32bit and 64bit |
| 102 |
// code would behave differently since FPC doesn't otherwise |
| 103 |
// sign-extend the index variable of type Integer when it's used |
| 104 |
// as an array-index into an array with an unsigned index range. |
| 105 |
// i32:=-1; i64:=-1 => i32=i64 but @arr[i32] <> @arr[i64] |
| 106 |
|
| 107 |
PByteArray = PByte; // PByte already has PointerMath |
| 108 |
{$POINTERMATH ON} |
| 109 |
PDoubleArray = ^Double; |
| 110 |
PInt64Array = ^Int64; |
| 111 |
PColor32Array = ^TColor32; |
| 112 |
PARGBArray = ^TARGB; |
| 113 |
{$POINTERMATH OFF} |
| 114 |
|
| 115 |
{$ELSE} // Delphi 7-2007 |
| 116 |
PByteArray = ^TStaticByteArray; |
| 117 |
TStaticByteArray = array[0..MaxInt div SizeOf(byte) - 1] of byte; |
| 118 |
PDoubleArray = ^TStaticDoubleArray; |
| 119 |
TStaticDoubleArray = array[0..MaxInt div SizeOf(double) - 1] of double; |
| 120 |
PInt64Array = ^TStaticInt64Array; |
| 121 |
TStaticInt64Array = array[0..MaxInt div SizeOf(int64) - 1] of int64; |
| 122 |
PColor32Array = ^TStaticColor32Array; |
| 123 |
TStaticColor32Array = array[0..MaxInt div SizeOf(TColor32) - 1] of TColor32; |
| 124 |
PARGBArray = ^TStaticARGBArray; |
| 125 |
TStaticARGBArray = array[0..MaxInt div SizeOf(TARGB) - 1] of TARGB; |
| 126 |
{$ENDIF} |
| 127 |
|
| 128 |
TArrayOfByte = array of Byte; |
| 129 |
TArrayOfWord = array of WORD; |
| 130 |
TArrayOfInteger = array of Integer; |
| 131 |
TArrayOfDouble = array of double; |
| 132 |
|
| 133 |
PColor32 = ^TColor32; |
| 134 |
TArrayOfColor32 = array of TColor32; |
| 135 |
TArrayOfArrayOfColor32 = array of TArrayOfColor32; |
| 136 |
|
| 137 |
TArrayOfString = array of string; |
| 138 |
|
| 139 |
TClipboardPriority = (cpLow, cpMedium, cpHigh); |
| 140 |
TImg32Notification = (inStateChange, inDestroy); |
| 141 |
|
| 142 |
//A INotifyRecipient receives change notifications though a property |
| 143 |
//interface from a single NotifySender (eg a Font property). |
| 144 |
//A NotifySender can send change notificatons to multiple NotifyRecipients |
| 145 |
//(eg where multiple object use the same font property). NotifyRecipients can |
| 146 |
//still receive change notificatons from mulitple NotifySenders, but it |
| 147 |
//must use a separate property for each NotifySender. (Also there's little |
| 148 |
//benefit in using INotifySender and INotifyRecipient interfaces where there |
| 149 |
//will only be one receiver - eg scroll - scrolling window.) |
| 150 |
|
| 151 |
INotifyRecipient = interface |
| 152 |
['{95F50C62-D321-46A4-A42C-8E9D0E3149B5}'] |
| 153 |
procedure ReceiveNotification(Sender: TObject; notify: TImg32Notification); |
| 154 |
end; |
| 155 |
TRecipients = array of INotifyRecipient; |
| 156 |
|
| 157 |
INotifySender = interface |
| 158 |
['{52072382-8B2F-481D-BE0A-E1C0A216B03E}'] |
| 159 |
procedure AddRecipient(recipient: INotifyRecipient); |
| 160 |
procedure DeleteRecipient(recipient: INotifyRecipient); |
| 161 |
end; |
| 162 |
|
| 163 |
TInterfacedObj = class(TObject, IInterface) |
| 164 |
public |
| 165 |
{$IFDEF FPC} |
| 166 |
function _AddRef: Integer; |
| 167 |
{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; |
| 168 |
function _Release: Integer; |
| 169 |
{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; |
| 170 |
function QueryInterface( |
| 171 |
{$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid; |
| 172 |
out obj) : longint; |
| 173 |
{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; |
| 174 |
{$ELSE} |
| 175 |
function _AddRef: Integer; stdcall; |
| 176 |
function _Release: Integer; stdcall; |
| 177 |
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall; |
| 178 |
{$ENDIF} |
| 179 |
end; |
| 180 |
|
| 181 |
TImage32 = class; |
| 182 |
TImageFormatClass = class of TImageFormat; |
| 183 |
|
| 184 |
//TImageFormat: Abstract base class for loading and saving images in TImage32.<br> |
| 185 |
//This class is overridden to provide support for separate |
| 186 |
//file storage formats (eg BMP, PNG, GIF & JPG).<br> |
| 187 |
//Derived classes register with TImage32 using TImage32.RegisterImageFormatClass. |
| 188 |
TImageFormat = class |
| 189 |
public |
| 190 |
class function IsValidImageStream(stream: TStream): Boolean; virtual; abstract; |
| 191 |
procedure SaveToStream(stream: TStream; img32: TImage32; quality: integer = 0); virtual; abstract; |
| 192 |
function SaveToFile(const filename: string; img32: TImage32; quality: integer = 0): Boolean; virtual; |
| 193 |
function LoadFromStream(stream: TStream; |
| 194 |
img32: TImage32; imgIndex: integer = 0): Boolean; virtual; abstract; |
| 195 |
function LoadFromFile(const filename: string; img32: TImage32): Boolean; virtual; |
| 196 |
class function GetImageCount(stream: TStream): integer; virtual; |
| 197 |
class function CanCopyToClipboard: Boolean; virtual; |
| 198 |
class function CopyToClipboard(img32: TImage32): Boolean; virtual; abstract; |
| 199 |
class function CanPasteFromClipboard: Boolean; virtual; abstract; |
| 200 |
class function PasteFromClipboard(img32: TImage32): Boolean; virtual; abstract; |
| 201 |
end; |
| 202 |
|
| 203 |
TBlendFunction = function(bgColor, fgColor: TColor32): TColor32; |
| 204 |
TBlendLineFunction = procedure(bgColor, fgColor: PColor32; width: nativeint); |
| 205 |
|
| 206 |
TCompareFunction = function(master, current: TColor32; data: integer): Boolean; |
| 207 |
TCompareFunctionEx = function(master, current: TColor32): Byte; |
| 208 |
|
| 209 |
TTileFillStyle = (tfsRepeat, tfsMirrorHorz, tfsMirrorVert, tfsRotate180); |
| 210 |
|
| 211 |
TResamplerFunction = function(img: TImage32; x, y: double): TColor32; |
| 212 |
|
| 213 |
TGrayscaleMode = (gsmSaturation, gsmLinear, gsmColorimetric); |
| 214 |
|
| 215 |
TImage32 = class(TObject) |
| 216 |
private |
| 217 |
fWidth: integer; |
| 218 |
fHeight: Integer; |
| 219 |
fResampler: integer; |
| 220 |
fIsPremultiplied: Boolean; |
| 221 |
fColorCount: integer; |
| 222 |
fPixels: TArrayOfColor32; |
| 223 |
fOnChange: TNotifyEvent; |
| 224 |
fOnResize: TNotifyEvent; |
| 225 |
fUpdateCnt: integer; |
| 226 |
fAntiAliased: Boolean; |
| 227 |
fNotifyBlockCnt: integer; |
| 228 |
function GetPixel(x,y: Integer): TColor32; |
| 229 |
procedure SetPixel(x,y: Integer; color: TColor32); |
| 230 |
function GetIsBlank: Boolean; |
| 231 |
function GetIsEmpty: Boolean; |
| 232 |
function GetPixelBase: PColor32; |
| 233 |
function GetPixelRow(row: Integer): PColor32; |
| 234 |
procedure RotateLeft90; |
| 235 |
procedure RotateRight90; |
| 236 |
procedure Rotate180; |
| 237 |
function GetColorCount: Integer; |
| 238 |
function GetHasTransparency: Boolean; |
| 239 |
function GetBounds: TRect; |
| 240 |
function GetMidPoint: TPointD; |
| 241 |
protected |
| 242 |
procedure ResetColorCount; |
| 243 |
function RectHasTransparency(const rec: TRect): Boolean; |
| 244 |
function CopyPixels(const rec: TRect): TArrayOfColor32; |
| 245 |
//CopyInternal: Internal routine (has no scaling or bounds checking) |
| 246 |
procedure CopyInternal(src: TImage32; |
| 247 |
const srcRec, dstRec: TRect; blendFunc: TBlendFunction); |
| 248 |
procedure CopyInternalLine(src: TImage32; |
| 249 |
const srcRec, dstRec: TRect; blendLineFunc: TBlendLineFunction); |
| 250 |
function CopyBlendInternal(src: TImage32; const srcRec: TRect; dstRec: TRect; |
| 251 |
blendFunc: TBlendFunction = nil; blendLineFunc: TBlendLineFunction = nil): Boolean; overload; |
| 252 |
procedure Changed; virtual; |
| 253 |
procedure Resized; virtual; |
| 254 |
function SetPixels(const newPixels: TArrayOfColor32): Boolean; |
| 255 |
property UpdateCount: integer read fUpdateCnt; |
| 256 |
public |
| 257 |
constructor Create(width: Integer = 0; height: Integer = 0); overload; |
| 258 |
//Create(src:array, width, height): Uses the specified array for the pixels. |
| 259 |
// Uses src for the pixels without copying it. |
| 260 |
constructor Create(const src: TArrayOfColor32; width: Integer; height: Integer); overload; |
| 261 |
constructor Create(src: TImage32); overload; |
| 262 |
constructor Create(src: TImage32; const srcRec: TRect); overload; |
| 263 |
destructor Destroy; override; |
| 264 |
//BeginUpdate/EndUpdate: postpones calls to OnChange event (can be nested) |
| 265 |
procedure BeginUpdate; |
| 266 |
procedure EndUpdate; |
| 267 |
//BlockUpdate/UnBlockUpdate: blocks calls to OnChange event (can be nested) |
| 268 |
procedure BlockNotify; |
| 269 |
procedure UnblockNotify; |
| 270 |
|
| 271 |
procedure Assign(src: TImage32); |
| 272 |
procedure AssignTo(dst: TImage32); |
| 273 |
procedure AssignSettings(src: TImage32); |
| 274 |
//AssignPixelArray: Replaces the content and takes ownership of src. |
| 275 |
// Uses src for the pixels without copying it. |
| 276 |
procedure AssignPixelArray(const src: TArrayOfColor32; width: Integer; height: Integer); |
| 277 |
|
| 278 |
//SetSize: Erases any current image, and fills with the specified color. |
| 279 |
procedure SetSize(newWidth, newHeight: Integer; color: TColor32 = 0); |
| 280 |
//Resize: is very similar to Scale() |
| 281 |
procedure Resize(newWidth, newHeight: Integer); |
| 282 |
procedure ResizeTo(targetImg: TImage32; newWidth, newHeight: Integer); |
| 283 |
//ScaleToFit: The image will be scaled proportionally |
| 284 |
procedure ScaleToFit(width, height: integer); |
| 285 |
//ScaleToFitCentered: The new image will be scaled and also centred |
| 286 |
procedure ScaleToFitCentered(width, height: integer); overload; |
| 287 |
procedure ScaleToFitCentered(const rect: TRect); overload; |
| 288 |
procedure Scale(s: double); overload; |
| 289 |
procedure Scale(sx, sy: double); overload; |
| 290 |
procedure ScaleTo(targetImg: TImage32; s: double); overload; |
| 291 |
procedure ScaleTo(targetImg: TImage32; sx, sy: double); overload; |
| 292 |
|
| 293 |
function Copy(src: TImage32; srcRec, dstRec: TRect): Boolean; |
| 294 |
//CopyBlend: Copies part or all of another image (src) on top of the |
| 295 |
//existing image. If no blend function is provided, then the function |
| 296 |
//will behave exactly as the Copy function above. However, when a blend |
| 297 |
//function is specified, that function will determine how the images will |
| 298 |
//be blended. If srcRec and dstRec have different widths or heights, |
| 299 |
//then the image in srcRec will also be stretched to fit dstRec. |
| 300 |
function CopyBlend(src: TImage32; const srcRec, dstRec: TRect; |
| 301 |
blendFunc: TBlendFunction = nil): Boolean; overload; {$IFDEF INLINE} inline; {$ENDIF} |
| 302 |
function CopyBlend(src: TImage32; const srcRec, dstRec: TRect; |
| 303 |
blendLineFunc: TBlendLineFunction): Boolean; overload; {$IFDEF INLINE} inline; {$ENDIF} |
| 304 |
|
| 305 |
{$IFDEF MSWINDOWS} |
| 306 |
//CopyFromDC: Copies an image from a Windows device context, erasing |
| 307 |
//any current image in TImage32. (eg copying from TBitmap.canvas.handle) |
| 308 |
procedure CopyFromDC(srcDc: HDC; const srcRect: TRect); |
| 309 |
//CopyToDc: Copies the image into a Windows device context |
| 310 |
procedure CopyToDc(dstDc: HDC; x: Integer = 0; y: Integer = 0; |
| 311 |
transparent: Boolean = true); overload; |
| 312 |
procedure CopyToDc(const srcRect: TRect; dstDc: HDC; |
| 313 |
x: Integer = 0; y: Integer = 0; transparent: Boolean = true); overload; |
| 314 |
procedure CopyToDc(const srcRect, dstRect: TRect; dstDc: HDC; |
| 315 |
transparent: Boolean = true); overload; |
| 316 |
{$ENDIF} |
| 317 |
{$IF DEFINED(USING_VCL_LCL)} |
| 318 |
procedure CopyFromBitmap(bmp: TBitmap); |
| 319 |
procedure CopyToBitmap(bmp: TBitmap); |
| 320 |
{$IFEND} |
| 321 |
function CopyToClipBoard: Boolean; |
| 322 |
class function CanPasteFromClipBoard: Boolean; |
| 323 |
function PasteFromClipBoard: Boolean; |
| 324 |
procedure Crop(const rec: TRect); |
| 325 |
//SetBackgroundColor: Assumes the current image is semi-transparent. |
| 326 |
procedure SetBackgroundColor(bgColor: TColor32); |
| 327 |
procedure Clear(color: TColor32 = 0); overload; |
| 328 |
procedure Clear(const rec: TRect; color: TColor32 = 0); overload; |
| 329 |
procedure FillRect(const rec: TRect; color: TColor32); |
| 330 |
|
| 331 |
procedure ConvertToBoolMask(reference: TColor32; |
| 332 |
tolerance: integer; colorFunc: TCompareFunction; |
| 333 |
maskBg: TColor32 = clWhite32; maskFg: TColor32 = clBlack32); |
| 334 |
procedure ConvertToAlphaMask(reference: TColor32; |
| 335 |
colorFunc: TCompareFunctionEx); |
| 336 |
|
| 337 |
procedure FlipVertical; |
| 338 |
procedure FlipHorizontal; |
| 339 |
procedure PreMultiply; |
| 340 |
//SetAlpha: Sets 'alpha' to the alpha byte of every pixel in the image |
| 341 |
procedure SetAlpha(alpha: Byte); |
| 342 |
procedure ReduceOpacity(opacity: Byte); overload; |
| 343 |
procedure ReduceOpacity(opacity: Byte; rec: TRect); overload; |
| 344 |
//SetRGB: Sets the RGB channels leaving the alpha channel unchanged |
| 345 |
procedure SetRGB(rgbColor: TColor32); overload; |
| 346 |
procedure SetRGB(rgbColor: TColor32; rec: TRect); overload; |
| 347 |
//Grayscale: Only changes color channels. The alpha channel is untouched. |
| 348 |
procedure Grayscale(mode: TGrayscaleMode = gsmSaturation; |
| 349 |
linearAmountPercentage: double = 1.0); |
| 350 |
procedure InvertColors; |
| 351 |
procedure InvertAlphas; |
| 352 |
procedure AdjustHue(percent: Integer); //ie +/- 100% |
| 353 |
procedure AdjustLuminance(percent: Integer); //ie +/- 100% |
| 354 |
procedure AdjustSaturation(percent: Integer); //ie +/- 100% |
| 355 |
|
| 356 |
function GetOpaqueBounds: TRect; |
| 357 |
//CropTransparentPixels: Trims transparent edges until each edge contains |
| 358 |
//at least one opaque or semi-opaque pixel. |
| 359 |
function CropTransparentPixels: TRect; |
| 360 |
procedure Rotate(angleRads: double); |
| 361 |
//RotateRect: Rotates part of an image, but also clips those parts of the |
| 362 |
//rotated image that fall outside rec. The eraseColor parameter indicates |
| 363 |
//the color to fill those uncovered pixels in rec following rotation. |
| 364 |
procedure RotateRect(const rec: TRect; |
| 365 |
angleRads: double; eraseColor: TColor32 = 0); |
| 366 |
procedure Skew(dx,dy: double); |
| 367 |
|
| 368 |
//ScaleAlpha: Scales the alpha byte of every pixel by the specified amount. |
| 369 |
procedure ScaleAlpha(scale: double); |
| 370 |
class procedure RegisterImageFormatClass(ext: string; |
| 371 |
bm32ExClass: TImageFormatClass; clipPriority: TClipboardPriority); |
| 372 |
class function GetImageFormatClass(const ext: string): TImageFormatClass; overload; |
| 373 |
class function GetImageFormatClass(stream: TStream): TImageFormatClass; overload; |
| 374 |
class function IsRegisteredFormat(const ext: string): Boolean; |
| 375 |
function SaveToFile(filename: string; |
| 376 |
compressionQuality: integer = defaultCompression): Boolean; |
| 377 |
function SaveToStream(stream: TStream; const FmtExt: string; |
| 378 |
compressionQuality: integer = defaultCompression): Boolean; |
| 379 |
function LoadFromFile(const filename: string): Boolean; |
| 380 |
function LoadFromStream(stream: TStream; imgIdx: integer = 0): Boolean; |
| 381 |
function LoadFromResource(const resName: string; resType: PChar): Boolean; |
| 382 |
|
| 383 |
//properties ... |
| 384 |
|
| 385 |
property AntiAliased: Boolean read fAntiAliased write fAntiAliased; |
| 386 |
property Width: Integer read fWidth; |
| 387 |
property Height: Integer read fHeight; |
| 388 |
property Bounds: TRect read GetBounds; |
| 389 |
property IsBlank: Boolean read GetIsBlank; |
| 390 |
property IsEmpty: Boolean read GetIsEmpty; |
| 391 |
property IsPreMultiplied: Boolean read fIsPremultiplied; |
| 392 |
property MidPoint: TPointD read GetMidPoint; |
| 393 |
property Pixel[x,y: Integer]: TColor32 read GetPixel write SetPixel; |
| 394 |
property Pixels: TArrayOfColor32 read fPixels; |
| 395 |
property PixelBase: PColor32 read GetPixelBase; |
| 396 |
property PixelRow[row: Integer]: PColor32 read GetPixelRow; |
| 397 |
property ColorCount: Integer read GetColorCount; |
| 398 |
//HasTransparency: Returns true if any pixel's alpha byte < 255. |
| 399 |
property HasTransparency: Boolean read GetHasTransparency; |
| 400 |
//Resampler: is used in scaling and rotation transforms |
| 401 |
property Resampler: integer read fResampler write fResampler; |
| 402 |
property OnChange: TNotifyEvent read fOnChange write fOnChange; |
| 403 |
property OnResize: TNotifyEvent read fOnResize write fOnResize; |
| 404 |
end; |
| 405 |
|
| 406 |
TImageList32 = class |
| 407 |
private |
| 408 |
{$IFDEF XPLAT_GENERICS} |
| 409 |
fList: TList<TImage32>; |
| 410 |
{$ELSE} |
| 411 |
fList: TList; |
| 412 |
{$ENDIF} |
| 413 |
fIsImageOwner: Boolean; |
| 414 |
function GetImage(index: integer): TImage32; |
| 415 |
procedure SetImage(index: integer; img: TIMage32); |
| 416 |
function GetLast: TImage32; |
| 417 |
public |
| 418 |
constructor Create; |
| 419 |
destructor Destroy; override; |
| 420 |
procedure Clear; |
| 421 |
function Count: integer; |
| 422 |
procedure Add(image: TImage32); overload; |
| 423 |
function Add(width, height: integer): TImage32; overload; |
| 424 |
procedure Insert(index: integer; image: TImage32); |
| 425 |
procedure Move(currentIndex, newIndex: integer); |
| 426 |
procedure Delete(index: integer); |
| 427 |
property Image[index: integer]: TImage32 read GetImage write SetImage; default; |
| 428 |
property IsImageOwner: Boolean read fIsImageOwner write fIsImageOwner; |
| 429 |
property Last: TImage32 read GetLast; |
| 430 |
end; |
| 431 |
|
| 432 |
THsl = packed record |
| 433 |
hue : byte; |
| 434 |
sat : byte; |
| 435 |
lum : byte; |
| 436 |
alpha: byte; |
| 437 |
end; |
| 438 |
PHsl = ^THsl; |
| 439 |
TArrayofHSL = array of THsl; |
| 440 |
|
| 441 |
TTriState = (tsUnknown = 0, tsYes = 1, tsChecked = 1, tsNo = 2, tsUnchecked = 2); |
| 442 |
|
| 443 |
PPointD = ^TPointD; |
| 444 |
TPathD = array of TPointD; //nb: watch for ambiguity with Clipper.pas |
| 445 |
TPathsD = array of TPathD; //nb: watch for ambiguity with Clipper.pas |
| 446 |
TArrayOfPathsD = array of TPathsD; |
| 447 |
|
| 448 |
TRectD = {$IFDEF RECORD_METHODS} record {$ELSE} object {$ENDIF} |
| 449 |
{$IFNDEF RECORD_METHODS} |
| 450 |
Left, Top, Right, Bottom: Double; |
| 451 |
function TopLeft: TPointD; |
| 452 |
function BottomRight: TPointD; |
| 453 |
{$ENDIF} |
| 454 |
function IsEmpty: Boolean; |
| 455 |
function Width: double; |
| 456 |
function Height: double; |
| 457 |
//Normalize: Returns True if swapping top & bottom or left & right |
| 458 |
function Normalize: Boolean; |
| 459 |
function Contains(const Pt: TPoint): Boolean; overload; |
| 460 |
function Contains(const Pt: TPointD): Boolean; overload; |
| 461 |
function MidPoint: TPointD; |
| 462 |
{$IFDEF RECORD_METHODS} |
| 463 |
case Integer of |
| 464 |
0: (Left, Top, Right, Bottom: Double); |
| 465 |
1: (TopLeft, BottomRight: TPointD); |
| 466 |
{$ENDIF} |
| 467 |
end; |
| 468 |
|
| 469 |
{$IFNDEF PBYTE} |
| 470 |
PByte = type PChar; |
| 471 |
{$ENDIF} |
| 472 |
|
| 473 |
//BLEND FUNCTIONS ( see TImage32.CopyBlend() ) |
| 474 |
|
| 475 |
//BlendToOpaque: Blends a semi-transparent image onto an opaque background |
| 476 |
function BlendToOpaque(bgColor, fgColor: TColor32): TColor32; |
| 477 |
//BlendToAlpha: Blends two semi-transparent images (slower than BlendToOpaque) |
| 478 |
function BlendToAlpha(bgColor, fgColor: TColor32): TColor32; |
| 479 |
function BlendToAlpha3(bgColor, fgColor: TColor32; blendOpacity: Byte): TColor32; |
| 480 |
procedure BlendToAlphaLine(bgColor, fgColor: PColor32; width: nativeint); |
| 481 |
//BlendMask: Whereever the mask is, preserves the background |
| 482 |
function BlendMask(bgColor, alphaMask: TColor32): TColor32; |
| 483 |
procedure BlendMaskLine(bgColor, alphaMask: PColor32; width: nativeint); |
| 484 |
function BlendAltMask(bgColor, alphaMask: TColor32): TColor32; |
| 485 |
function BlendDifference(color1, color2: TColor32): TColor32; |
| 486 |
function BlendSubtract(bgColor, fgColor: TColor32): TColor32; |
| 487 |
function BlendLighten(bgColor, fgColor: TColor32): TColor32; |
| 488 |
function BlendDarken(bgColor, fgColor: TColor32): TColor32; |
| 489 |
function BlendInvertedMask(bgColor, alphaMask: TColor32): TColor32; |
| 490 |
procedure BlendInvertedMaskLine(bgColor, alphaMask: PColor32; width: nativeint); |
| 491 |
//BlendBlueChannel: typically useful for white color masks |
| 492 |
function BlendBlueChannel(bgColor, blueMask: TColor32): TColor32; |
| 493 |
procedure BlendBlueChannelLine(bgColor, blueMask: PColor32; width: nativeint); |
| 494 |
|
| 495 |
//COMPARE COLOR FUNCTIONS (ConvertToBoolMask, FloodFill, Vectorize etc.) |
| 496 |
|
| 497 |
function CompareRGB(master, current: TColor32; tolerance: Integer): Boolean; |
| 498 |
function CompareHue(master, current: TColor32; tolerance: Integer): Boolean; |
| 499 |
function CompareAlpha(master, current: TColor32; tolerance: Integer): Boolean; |
| 500 |
|
| 501 |
//CompareEx COLOR FUNCTIONS (see ConvertToAlphaMask) |
| 502 |
function CompareRgbEx(master, current: TColor32): Byte; |
| 503 |
function CompareAlphaEx(master, current: TColor32): Byte; |
| 504 |
|
| 505 |
//MISCELLANEOUS FUNCTIONS ... |
| 506 |
|
| 507 |
function GetBoolMask(img: TImage32; reference: TColor32; |
| 508 |
compareFunc: TCompareFunction; tolerance: Integer): TArrayOfByte; |
| 509 |
|
| 510 |
function GetByteMask(img: TImage32; reference: TColor32; |
| 511 |
compareFunc: TCompareFunctionEx): TArrayOfByte; |
| 512 |
|
| 513 |
function GetColorMask(img: TImage32; reference: TColor32; |
| 514 |
compareFunc: TCompareFunction; tolerance: Integer): TArrayOfColor32; |
| 515 |
|
| 516 |
{$IFDEF MSWINDOWS} |
| 517 |
//Color32: Converts a Graphics.TColor value into a TColor32 value. |
| 518 |
function Color32(rgbColor: Integer): TColor32; overload; {$IFDEF INLINE} inline; {$ENDIF} |
| 519 |
|
| 520 |
procedure FixPalette(p: PARGB; count: integer); |
| 521 |
{$ENDIF} |
| 522 |
function Color32(a, r, g, b: Byte): TColor32; overload; {$IFDEF INLINE} inline; {$ENDIF} |
| 523 |
|
| 524 |
//RGBColor: Converts a TColor32 value into a COLORREF value |
| 525 |
function RGBColor(color: TColor32): Cardinal; {$IFDEF INLINE} inline; {$ENDIF} |
| 526 |
function InvertColor(color: TColor32): TColor32; {$IFDEF INLINE} inline; {$ENDIF} |
| 527 |
|
| 528 |
//RgbToHsl: See https://en.wikipedia.org/wiki/HSL_and_HSV |
| 529 |
function RgbToHsl(color: TColor32): THsl; |
| 530 |
//HslToRgb: See https://en.wikipedia.org/wiki/HSL_and_HSV |
| 531 |
function HslToRgb(hslColor: THsl): TColor32; |
| 532 |
function AdjustHue(color: TColor32; percent: Integer): TColor32; |
| 533 |
function ArrayOfColor32ToArrayHSL(const clr32Arr: TArrayOfColor32): TArrayofHSL; |
| 534 |
function ArrayOfHSLToArrayColor32(const hslArr: TArrayofHSL): TArrayOfColor32; |
| 535 |
|
| 536 |
function GetAlpha(color: TColor32): Byte; {$IFDEF INLINE} inline; {$ENDIF} |
| 537 |
|
| 538 |
function PointD(const X, Y: Double): TPointD; overload; {$IFDEF INLINE} inline; {$ENDIF} |
| 539 |
function PointD(const pt: TPoint): TPointD; overload; {$IFDEF INLINE} inline; {$ENDIF} |
| 540 |
|
| 541 |
function RectD(left, top, right, bottom: double): TRectD; overload; |
| 542 |
function RectD(const rec: TRect): TRectD; overload; |
| 543 |
|
| 544 |
function ClampByte(val: Integer): byte; overload; {$IFDEF INLINE} inline; {$ENDIF} |
| 545 |
function ClampByte(val: double): byte; overload; {$IFDEF INLINE} inline; {$ENDIF} |
| 546 |
function ClampRange(val, min, max: Integer): Integer; overload; |
| 547 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 548 |
function ClampRange(val, min, max: double): double; overload; |
| 549 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 550 |
function IncPColor32(pc: Pointer; cnt: Integer): PColor32; {$IFDEF INLINE} inline; {$ENDIF} |
| 551 |
|
| 552 |
procedure NormalizeAngle(var angle: double; tolerance: double = Pi/360); |
| 553 |
function GrayScale(color: TColor32): TColor32; {$IFDEF INLINE} inline; {$ENDIF} |
| 554 |
|
| 555 |
//DPIAware: Useful for DPIAware sizing of images and their container controls. |
| 556 |
//It scales values relative to the display's resolution (PixelsPerInch). |
| 557 |
//See https://docs.microsoft.com/en-us/windows/desktop/hidpi/high-DPIAware-desktop-application-development-on-windows |
| 558 |
function DPIAware(val: Integer): Integer; overload; {$IFDEF INLINE} inline; {$ENDIF} |
| 559 |
function DPIAware(val: double): double; overload; {$IFDEF INLINE} inline; {$ENDIF} |
| 560 |
function DPIAware(const pt: TPoint): TPoint; overload; |
| 561 |
function DPIAware(const pt: TPointD): TPointD; overload; |
| 562 |
function DPIAware(const rec: TRect): TRect; overload; |
| 563 |
function DPIAware(const rec: TRectD): TRectD; overload; |
| 564 |
|
| 565 |
{$IFDEF MSWINDOWS} |
| 566 |
{$IFDEF FPC} |
| 567 |
function AlphaBlend(DC: HDC; p2, p3, p4, p5: Integer; |
| 568 |
DC6: HDC; p7, p8, p9, p10: Integer; p11: Windows.TBlendFunction): BOOL; |
| 569 |
stdcall; external 'msimg32.dll' name 'AlphaBlend'; |
| 570 |
{$ENDIF} |
| 571 |
{$ENDIF} |
| 572 |
|
| 573 |
//CreateResourceStream: handles both numeric and string names and types |
| 574 |
function CreateResourceStream(const resName: string; |
| 575 |
resType: PChar): TResourceStream; |
| 576 |
|
| 577 |
function GetResampler(id: integer): TResamplerFunction; |
| 578 |
function RegisterResampler(func: TResamplerFunction; const name: string): integer; |
| 579 |
procedure GetResamplerList(stringList: TStringList); |
| 580 |
|
| 581 |
const |
| 582 |
TwoPi = Pi *2; |
| 583 |
angle0 = 0; |
| 584 |
angle1 = Pi/180; |
| 585 |
angle15 = Pi /12; |
| 586 |
angle30 = angle15 *2; |
| 587 |
angle45 = angle15 *3; |
| 588 |
angle60 = angle15 *4; |
| 589 |
angle75 = angle15 *5; |
| 590 |
angle90 = Pi /2; |
| 591 |
angle105 = Pi - angle75; |
| 592 |
angle120 = Pi - angle60; |
| 593 |
angle135 = Pi - angle45; |
| 594 |
angle150 = Pi - angle30; |
| 595 |
angle165 = Pi - angle15; |
| 596 |
angle180 = Pi; |
| 597 |
angle195 = Pi + angle15; |
| 598 |
angle210 = Pi + angle30; |
| 599 |
angle225 = Pi + angle45; |
| 600 |
angle240 = Pi + angle60; |
| 601 |
angle255 = Pi + angle75; |
| 602 |
angle270 = TwoPi - angle90; |
| 603 |
angle285 = TwoPi - angle75; |
| 604 |
angle300 = TwoPi - angle60; |
| 605 |
angle315 = TwoPi - angle45; |
| 606 |
angle330 = TwoPi - angle30; |
| 607 |
angle345 = TwoPi - angle15; |
| 608 |
angle360 = TwoPi; |
| 609 |
|
| 610 |
div255: Double = 1 / 255; |
| 611 |
|
| 612 |
var |
| 613 |
//Resampling function identifiers (initialized in Img32.Resamplers) |
| 614 |
rNearestResampler : integer; |
| 615 |
rBilinearResampler: integer; |
| 616 |
rBicubicResampler : integer; |
| 617 |
rWeightedBilinear : integer; |
| 618 |
DefaultResampler: Integer = 0; |
| 619 |
|
| 620 |
//Both MulTable and DivTable are used in blend functions |
| 621 |
//MulTable[a,b] = a * b / 255 |
| 622 |
MulTable: array [Byte,Byte] of Byte; |
| 623 |
//DivTable[a,b] = a * 255/b (for a <= b) |
| 624 |
DivTable: array [Byte,Byte] of Byte; |
| 625 |
|
| 626 |
//Sigmoid: weight byte values towards each end |
| 627 |
Sigmoid: array[Byte] of Byte; |
| 628 |
|
| 629 |
dpiAware1 : integer = 1; |
| 630 |
DpiAwareOne : double = 1.0; |
| 631 |
|
| 632 |
//AND BECAUSE OLDER DELPHI COMPILERS (OLDER THAN D2006) |
| 633 |
//DON'T SUPPORT RECORD METHODS |
| 634 |
procedure RectWidthHeight(const rec: TRect; out width, height: Integer); overload; |
| 635 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 636 |
procedure RectWidthHeight(const rec: TRectD; out width, height: double); overload; |
| 637 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 638 |
function RectWidth(const rec: TRect): Integer; |
| 639 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 640 |
function RectHeight(const rec: TRect): Integer; |
| 641 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 642 |
|
| 643 |
function IsEmptyRect(const rec: TRect): Boolean; overload; |
| 644 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 645 |
function IsEmptyRect(const rec: TRectD): Boolean; overload; |
| 646 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 647 |
|
| 648 |
function SwapRedBlue(color: TColor32): TColor32; overload; |
| 649 |
procedure SwapRedBlue(color: PColor32; count: integer); overload; |
| 650 |
|
| 651 |
function MulBytes(b1, b2: Byte) : Byte; |
| 652 |
|
| 653 |
function __Trunc(Value: Double): Integer; {$IFNDEF CPUX86} {$IFDEF INLINE} inline; {$ENDIF} {$ENDIF} |
| 654 |
|
| 655 |
// NewColor32Array creates a new "array of TColor32". "a" is nil'ed |
| 656 |
// before allocating the array. If "count" is zero or negative "a" will |
| 657 |
// be nil. If "uninitialized" is True, the memory will not be zero'ed. |
| 658 |
procedure NewColor32Array(var a: TArrayOfColor32; count: nativeint; |
| 659 |
uninitialized: boolean = False); |
| 660 |
procedure NewIntegerArray(var a: TArrayOfInteger; count: nativeint; |
| 661 |
uninitialized: boolean = False); |
| 662 |
procedure NewByteArray(var a: TArrayOfByte; count: nativeint; |
| 663 |
uninitialized: boolean = False); |
| 664 |
procedure NewPointDArray(var a: TPathD; count: nativeint; |
| 665 |
uninitialized: boolean = False); |
| 666 |
|
| 667 |
// SetLengthUninit changes the dyn. array's length but does not initialize |
| 668 |
// the new elements with zeros. It can be used as a replacement for |
| 669 |
// SetLength where the zero-initialitation is not required. |
| 670 |
procedure SetLengthUninit(var a: TArrayOfColor32; count: nativeint); overload; |
| 671 |
procedure SetLengthUninit(var a: TArrayOfInteger; count: nativeint); overload; |
| 672 |
procedure SetLengthUninit(var a: TArrayOfByte; count: nativeint); overload; |
| 673 |
procedure SetLengthUninit(var a: TPathD; count: nativeint); overload; |
| 674 |
|
| 675 |
implementation |
| 676 |
|
| 677 |
uses |
| 678 |
Img32.Vector, Img32.Resamplers, Img32.Transform |
| 679 |
{$IF DEFINED(USING_VCL_LCL)} |
| 680 |
, Img32.Fmt.BMP |
| 681 |
{$ENDIF} |
| 682 |
; |
| 683 |
|
| 684 |
resourcestring |
| 685 |
rsImageTooLarge = 'Image32 error: the image is too large.'; |
| 686 |
rsInvalidImageArrayData = 'Image32 error: the specified pixels array and the size does not match.'; |
| 687 |
|
| 688 |
//------------------------------------------------------------------------------ |
| 689 |
//------------------------------------------------------------------------------ |
| 690 |
|
| 691 |
{$IFDEF CPUX86} |
| 692 |
const |
| 693 |
// Use faster Trunc for x86 code in this unit. |
| 694 |
Trunc: function(Value: Double): Integer = __Trunc; |
| 695 |
{$ENDIF CPUX86} |
| 696 |
|
| 697 |
type |
| 698 |
TImgFmtRec = record |
| 699 |
Fmt: string; |
| 700 |
SortOrder: TClipboardPriority; |
| 701 |
Obj: TImageFormatClass; |
| 702 |
end; |
| 703 |
PImgFmtRec = ^TImgFmtRec; |
| 704 |
|
| 705 |
TResamplerObj = class |
| 706 |
id: integer; |
| 707 |
name: string; |
| 708 |
func: TResamplerFunction; |
| 709 |
end; |
| 710 |
|
| 711 |
PDynArrayRec = ^TDynArrayRec; |
| 712 |
{$IFDEF FPC} |
| 713 |
tdynarrayindex = sizeint; |
| 714 |
TDynArrayRec = packed record |
| 715 |
refcount: ptrint; |
| 716 |
high: tdynarrayindex; |
| 717 |
Data: record end; |
| 718 |
end; |
| 719 |
{$ELSE} |
| 720 |
TDynArrayRec = packed record |
| 721 |
{$IFDEF CPU64BITS} |
| 722 |
_Padding: Integer; |
| 723 |
{$ENDIF} |
| 724 |
RefCnt: Integer; |
| 725 |
Length: NativeInt; |
| 726 |
Data: record end; |
| 727 |
end; |
| 728 |
{$ENDIF} |
| 729 |
|
| 730 |
var |
| 731 |
{$IFDEF XPLAT_GENERICS} |
| 732 |
ImageFormatClassList: TList<PImgFmtRec>; //list of supported file extensions |
| 733 |
ResamplerList: TList<TResamplerObj>; //list of resampler functions |
| 734 |
{$ELSE} |
| 735 |
ImageFormatClassList: TList; |
| 736 |
ResamplerList: TList; |
| 737 |
{$ENDIF} |
| 738 |
|
| 739 |
//------------------------------------------------------------------------------ |
| 740 |
//------------------------------------------------------------------------------ |
| 741 |
|
| 742 |
function NewSimpleDynArray(count: nativeint; elemSize: integer; uninitialized: boolean = False): Pointer; |
| 743 |
var |
| 744 |
p: PDynArrayRec; |
| 745 |
begin |
| 746 |
Result := nil; |
| 747 |
if (count > 0) and (elemSize > 0) then |
| 748 |
begin |
| 749 |
if uninitialized then |
| 750 |
GetMem(Pointer(p), SizeOf(TDynArrayRec) + count * elemSize) |
| 751 |
else |
| 752 |
p := AllocMem(SizeOf(TDynArrayRec) + count * elemSize); |
| 753 |
{$IFDEF FPC} |
| 754 |
p.refcount := 1; |
| 755 |
p.high := count -1; |
| 756 |
{$ELSE} |
| 757 |
p.RefCnt := 1; |
| 758 |
p.Length := count; |
| 759 |
{$ENDIF} |
| 760 |
Result := @p.Data; |
| 761 |
end; |
| 762 |
end; |
| 763 |
//------------------------------------------------------------------------------ |
| 764 |
|
| 765 |
function InternSetSimpleDynArrayLengthUninit(a: Pointer; count: nativeint; elemSize: integer): Pointer; |
| 766 |
var |
| 767 |
p: PDynArrayRec; |
| 768 |
oldCount: nativeint; |
| 769 |
begin |
| 770 |
if a = nil then |
| 771 |
Result := NewSimpleDynArray(count, elemSize) |
| 772 |
else if (count > 0) and (elemSize > 0) then |
| 773 |
begin |
| 774 |
p := PDynArrayRec(PByte(a) - SizeOf(TDynArrayRec)); |
| 775 |
{$IFDEF FPC} |
| 776 |
oldCount := p.high + 1; |
| 777 |
if p.refcount = 1 then |
| 778 |
{$ELSE} |
| 779 |
oldCount := p.Length; |
| 780 |
if p.RefCnt = 1 then |
| 781 |
{$ENDIF} |
| 782 |
begin |
| 783 |
// There is only one reference to this array and that is "a", |
| 784 |
// so we can use ReallocMem to change the array's length. |
| 785 |
if oldCount = count then |
| 786 |
begin |
| 787 |
Result := a; |
| 788 |
Exit; |
| 789 |
end; |
| 790 |
ReallocMem(Pointer(p), SizeOf(TDynArrayRec) + count * elemSize); |
| 791 |
end |
| 792 |
else |
| 793 |
begin |
| 794 |
// SetLength makes a copy of the dyn array to get RefCnt=1 |
| 795 |
GetMem(Pointer(p), SizeOf(TDynArrayRec) + count * elemSize); |
| 796 |
if oldCount < 0 then oldCount := 0; // data corruption detected |
| 797 |
if oldCount > count then oldCount := count; |
| 798 |
Move(a^, p.Data, oldCount * elemSize); |
| 799 |
TArrayOfByte(a) := nil; // use a non-managed dyn.array type |
| 800 |
end; |
| 801 |
|
| 802 |
{$IFDEF FPC} |
| 803 |
p.refcount := 1; |
| 804 |
p.high := count -1; |
| 805 |
{$ELSE} |
| 806 |
p.RefCnt := 1; |
| 807 |
p.Length := count; |
| 808 |
{$ENDIF} |
| 809 |
Result := @p.Data; |
| 810 |
end |
| 811 |
else |
| 812 |
begin |
| 813 |
TArrayOfByte(a) := nil; // use a non-managed dyn.array type |
| 814 |
Result := nil; |
| 815 |
end; |
| 816 |
end; |
| 817 |
//------------------------------------------------------------------------------ |
| 818 |
|
| 819 |
function CanReuseDynArray(a: Pointer; count: nativeint): Boolean; |
| 820 |
// returns True if RefCnt=1 and Length=count |
| 821 |
begin |
| 822 |
//Assert(a <> nil); |
| 823 |
a := PByte(a) - SizeOf(TDynArrayRec); |
| 824 |
Result := |
| 825 |
{$IFDEF FPC} |
| 826 |
(PDynArrayRec(a).refcount = 1) and |
| 827 |
(PDynArrayRec(a).high = count - 1); |
| 828 |
{$ELSE} |
| 829 |
(PDynArrayRec(a).RefCnt = 1) and |
| 830 |
(PDynArrayRec(a).Length = count); |
| 831 |
{$ENDIF} |
| 832 |
end; |
| 833 |
//------------------------------------------------------------------------------ |
| 834 |
|
| 835 |
procedure NewColor32Array(var a: TArrayOfColor32; count: nativeint; uninitialized: boolean); |
| 836 |
begin |
| 837 |
{$IF COMPILERVERSION < 16} |
| 838 |
SetLength(a, count); |
| 839 |
{$ELSE} |
| 840 |
if a <> nil then |
| 841 |
begin |
| 842 |
if uninitialized and CanReuseDynArray(a, count) then Exit; |
| 843 |
a := nil; |
| 844 |
end; |
| 845 |
Pointer(a) := NewSimpleDynArray(count, SizeOf(TColor32), uninitialized); |
| 846 |
{$IFEND} |
| 847 |
end; |
| 848 |
//------------------------------------------------------------------------------ |
| 849 |
|
| 850 |
procedure NewIntegerArray(var a: TArrayOfInteger; count: nativeint; uninitialized: boolean); |
| 851 |
begin |
| 852 |
{$IF COMPILERVERSION < 16} |
| 853 |
SetLength(a, count); |
| 854 |
{$ELSE} |
| 855 |
if a <> nil then |
| 856 |
begin |
| 857 |
if uninitialized and CanReuseDynArray(a, count) then |
| 858 |
Exit; |
| 859 |
a := nil; |
| 860 |
end; |
| 861 |
Pointer(a) := NewSimpleDynArray(count, SizeOf(Integer), uninitialized); |
| 862 |
{$IFEND} |
| 863 |
end; |
| 864 |
//------------------------------------------------------------------------------ |
| 865 |
|
| 866 |
procedure NewByteArray(var a: TArrayOfByte; count: nativeint; uninitialized: boolean); |
| 867 |
begin |
| 868 |
{$IF COMPILERVERSION < 16} |
| 869 |
SetLength(a, count); |
| 870 |
{$ELSE} |
| 871 |
if a <> nil then |
| 872 |
begin |
| 873 |
if uninitialized and CanReuseDynArray(a, count) then |
| 874 |
Exit; |
| 875 |
a := nil; |
| 876 |
end; |
| 877 |
Pointer(a) := NewSimpleDynArray(count, SizeOf(Byte), uninitialized); |
| 878 |
{$IFEND} |
| 879 |
end; |
| 880 |
//------------------------------------------------------------------------------ |
| 881 |
|
| 882 |
procedure NewPointDArray(var a: TPathD; count: nativeint; uninitialized: boolean); |
| 883 |
begin |
| 884 |
{$IF COMPILERVERSION < 16} |
| 885 |
SetLength(a, count); |
| 886 |
{$ELSE} |
| 887 |
if a <> nil then |
| 888 |
begin |
| 889 |
if uninitialized and CanReuseDynArray(a, count) then |
| 890 |
Exit; |
| 891 |
a := nil; |
| 892 |
end; |
| 893 |
Pointer(a) := NewSimpleDynArray(count, SizeOf(TPointD), uninitialized); |
| 894 |
{$IFEND} |
| 895 |
end; |
| 896 |
//------------------------------------------------------------------------------ |
| 897 |
|
| 898 |
procedure SetLengthUninit(var a: TArrayOfColor32; count: nativeint); |
| 899 |
begin |
| 900 |
SetLength(a, count); |
| 901 |
// Pointer(a) := InternSetSimpleDynArrayLengthUninit(Pointer(a), count, SizeOf(TColor32)); |
| 902 |
end; |
| 903 |
//------------------------------------------------------------------------------ |
| 904 |
|
| 905 |
procedure SetLengthUninit(var a: TArrayOfInteger; count: nativeint); |
| 906 |
begin |
| 907 |
{$IF COMPILERVERSION < 16} |
| 908 |
SetLength(a, count); |
| 909 |
{$ELSE} |
| 910 |
Pointer(a) := InternSetSimpleDynArrayLengthUninit(Pointer(a), count, SizeOf(Integer)); |
| 911 |
{$IFEND} |
| 912 |
end; |
| 913 |
//------------------------------------------------------------------------------ |
| 914 |
|
| 915 |
procedure SetLengthUninit(var a: TArrayOfByte; count: nativeint); |
| 916 |
begin |
| 917 |
{$IF COMPILERVERSION < 16} |
| 918 |
SetLength(a, count); |
| 919 |
{$ELSE} |
| 920 |
Pointer(a) := InternSetSimpleDynArrayLengthUninit(Pointer(a), count, SizeOf(Byte)); |
| 921 |
{$IFEND} |
| 922 |
end; |
| 923 |
//------------------------------------------------------------------------------ |
| 924 |
|
| 925 |
procedure SetLengthUninit(var a: TPathD; count: nativeint); |
| 926 |
begin |
| 927 |
{$IF COMPILERVERSION < 16} |
| 928 |
SetLength(a, count); |
| 929 |
{$ELSE} |
| 930 |
Pointer(a) := InternSetSimpleDynArrayLengthUninit(Pointer(a), count, SizeOf(TPointD)); |
| 931 |
{$IFEND} |
| 932 |
end; |
| 933 |
//------------------------------------------------------------------------------ |
| 934 |
|
| 935 |
procedure CreateImageFormatList; |
| 936 |
begin |
| 937 |
if Assigned(ImageFormatClassList) then Exit; |
| 938 |
|
| 939 |
{$IFDEF XPLAT_GENERICS} |
| 940 |
ImageFormatClassList := TList<PImgFmtRec>.Create; |
| 941 |
{$ELSE} |
| 942 |
ImageFormatClassList := TList.Create; |
| 943 |
{$ENDIF} |
| 944 |
end; |
| 945 |
//------------------------------------------------------------------------------ |
| 946 |
|
| 947 |
function FMod(const ANumerator, ADenominator: Double): Double; |
| 948 |
begin |
| 949 |
Result := ANumerator - Trunc(ANumerator / ADenominator) * ADenominator; |
| 950 |
end; |
| 951 |
//------------------------------------------------------------------------------ |
| 952 |
|
| 953 |
procedure NormalizeAngle(var angle: double; tolerance: double = Pi/360); |
| 954 |
var |
| 955 |
aa: double; |
| 956 |
begin |
| 957 |
angle := FMod(angle, angle360); |
| 958 |
if angle < -Angle180 then angle := angle + angle360 |
| 959 |
else if angle > angle180 then angle := angle - angle360; |
| 960 |
|
| 961 |
aa := Abs(angle); |
| 962 |
if aa < tolerance then angle := 0 |
| 963 |
else if aa > angle180 - tolerance then angle := angle180 |
| 964 |
else if (aa < angle90 - tolerance) or (aa > angle90 + tolerance) then Exit |
| 965 |
else if angle < 0 then angle := -angle90 |
| 966 |
else angle := angle90; |
| 967 |
end; |
| 968 |
//------------------------------------------------------------------------------ |
| 969 |
|
| 970 |
{$IFDEF CPUX86} |
| 971 |
{ Trunc with FPU code is very slow because the x87 ControlWord has to be changed |
| 972 |
and then there is Delphi's Default8087CW variable that is not thread-safe. } |
| 973 |
|
| 974 |
//__Trunc: An efficient Trunc() algorithm (ie rounds toward zero) |
| 975 |
function __Trunc(Value: Double): Integer; |
| 976 |
var |
| 977 |
exp: integer; |
| 978 |
i64: UInt64 absolute Value; |
| 979 |
valueBytes: array[0..7] of Byte absolute Value; |
| 980 |
begin |
| 981 |
// https://en.wikipedia.org/wiki/Double-precision_floating-point_format |
| 982 |
// 52 bit fractional value, 11bit ($7FF) exponent, and 1bit sign |
| 983 |
Result := 0; |
| 984 |
if i64 = 0 then Exit; |
| 985 |
exp := Integer(Cardinal(i64 shr 52) and $7FF) - 1023; |
| 986 |
// nb: when exp == 1024 then Value == INF or NAN. |
| 987 |
if exp < 0 then |
| 988 |
Exit |
| 989 |
//else if exp > 52 then // ie only for 64bit int results |
| 990 |
// Result := ((i64 and $1FFFFFFFFFFFFF) shl (exp - 52)) or (1 shl exp) |
| 991 |
//else if exp > 31 then // alternatively, range check for 32bit ints ???? |
| 992 |
// raise Exception.Create(rsIntegerOverflow) |
| 993 |
else |
| 994 |
Result := Integer((i64 and $1FFFFFFFFFFFFF) shr (52 - exp)) or (1 shl exp); |
| 995 |
// Check for the sign bit without loading Value into the FPU. |
| 996 |
if valueBytes[7] and $80 <> 0 then Result := -Result; |
| 997 |
end; |
| 998 |
//------------------------------------------------------------------------------ |
| 999 |
|
| 1000 |
{$ELSE} |
| 1001 |
function __Trunc(Value: Double): Integer; |
| 1002 |
begin |
| 1003 |
// Uses fast SSE2 instruction |
| 1004 |
Result := System.Trunc(Value); |
| 1005 |
end; |
| 1006 |
//------------------------------------------------------------------------------ |
| 1007 |
{$ENDIF CPUX86} |
| 1008 |
|
| 1009 |
function SwapRedBlue(color: TColor32): TColor32; |
| 1010 |
var |
| 1011 |
c: array[0..3] of byte absolute color; |
| 1012 |
r: array[0..3] of byte absolute Result; |
| 1013 |
begin |
| 1014 |
result := color; |
| 1015 |
r[0] := c[2]; |
| 1016 |
r[2] := c[0]; |
| 1017 |
end; |
| 1018 |
//------------------------------------------------------------------------------ |
| 1019 |
|
| 1020 |
procedure SwapRedBlue(color: PColor32; count: integer); |
| 1021 |
var |
| 1022 |
i: integer; |
| 1023 |
begin |
| 1024 |
for i := 1 to count do |
| 1025 |
begin |
| 1026 |
color^ := SwapRedBlue(color^); |
| 1027 |
inc(color); |
| 1028 |
end; |
| 1029 |
end; |
| 1030 |
//------------------------------------------------------------------------------ |
| 1031 |
|
| 1032 |
function MulBytes(b1, b2: Byte) : Byte; {$IFDEF INLINE} inline; {$ENDIF} |
| 1033 |
begin |
| 1034 |
Result := MulTable[b1, b2]; |
| 1035 |
end; |
| 1036 |
//------------------------------------------------------------------------------ |
| 1037 |
|
| 1038 |
function ImageFormatClassListSort(item1, item2: Pointer): integer; |
| 1039 |
var |
| 1040 |
imgFmtRec1: PImgFmtRec absolute item1; |
| 1041 |
imgFmtRec2: PImgFmtRec absolute item2; |
| 1042 |
begin |
| 1043 |
Result := Integer(imgFmtRec1.SortOrder) - Integer(imgFmtRec2.SortOrder); |
| 1044 |
end; |
| 1045 |
//------------------------------------------------------------------------------ |
| 1046 |
|
| 1047 |
function ClampByte(val: Integer): byte; |
| 1048 |
begin |
| 1049 |
if val < 0 then result := 0 |
| 1050 |
else if val > 255 then result := 255 |
| 1051 |
else result := val; |
| 1052 |
end; |
| 1053 |
//------------------------------------------------------------------------------ |
| 1054 |
|
| 1055 |
function ClampByte(val: double): byte; |
| 1056 |
begin |
| 1057 |
if val <= 0 then result := 0 |
| 1058 |
else if val >= 255 then result := 255 |
| 1059 |
else result := Round(val); |
| 1060 |
end; |
| 1061 |
//------------------------------------------------------------------------------ |
| 1062 |
|
| 1063 |
//------------------------------------------------------------------------------ |
| 1064 |
// Blend functions - used by TImage32.CopyBlend() |
| 1065 |
//------------------------------------------------------------------------------ |
| 1066 |
|
| 1067 |
function BlendToOpaque(bgColor, fgColor: TColor32): TColor32; |
| 1068 |
var |
| 1069 |
fgA: byte; |
| 1070 |
fw,bw: PByteArray; |
| 1071 |
begin |
| 1072 |
fgA := fgColor shr 24; |
| 1073 |
if fgA = 0 then Result := bgColor |
| 1074 |
else if fgA = 255 then Result := fgColor |
| 1075 |
else |
| 1076 |
begin |
| 1077 |
//assuming bg.A = 255, use just fg.A for color weighting |
| 1078 |
fw := PByteArray(@MulTable[fgA]); //ie weight of foreground |
| 1079 |
bw := PByteArray(@MulTable[not fgA]); //ie weight of background |
| 1080 |
|
| 1081 |
Result := $FF000000 |
| 1082 |
or (TColor32(Byte(fw[Byte(fgColor shr 16)] + bw[Byte(bgColor shr 16)])) shl 16) |
| 1083 |
or (TColor32(Byte(fw[Byte(fgColor shr 8 )] + bw[Byte(bgColor shr 8)])) shl 8) |
| 1084 |
or (TColor32(Byte(fw[Byte(fgColor )] + bw[Byte(bgColor )])) ); |
| 1085 |
end; |
| 1086 |
end; |
| 1087 |
//------------------------------------------------------------------------------ |
| 1088 |
|
| 1089 |
function BlendToAlpha(bgColor, fgColor: TColor32): TColor32; |
| 1090 |
var |
| 1091 |
fgWeight: byte; |
| 1092 |
R, InvR: PByteArray; |
| 1093 |
bgA, fgA: byte; |
| 1094 |
begin |
| 1095 |
//(see https://en.wikipedia.org/wiki/Alpha_compositing) |
| 1096 |
fgA := fgColor shr 24; |
| 1097 |
bgA := bgColor shr 24; |
| 1098 |
if fgA = 0 then Result := bgColor |
| 1099 |
else if (bgA = 0) or (fgA = 255) then Result := fgColor |
| 1100 |
else |
| 1101 |
begin |
| 1102 |
//combine alphas ... |
| 1103 |
bgA := not MulTable[not fgA, not bgA]; |
| 1104 |
fgWeight := DivTable[fgA, bgA]; // fgWeight = amount foreground color |
| 1105 |
// contibutes to the result color |
| 1106 |
|
| 1107 |
R := PByteArray(@MulTable[fgWeight]); // ie weight of foreground |
| 1108 |
InvR := PByteArray(@MulTable[not fgWeight]); // ie weight of background |
| 1109 |
|
| 1110 |
Result := bgA shl 24 |
| 1111 |
or (TColor32(R[Byte(fgColor shr 16)] + InvR[Byte(bgColor shr 16)]) shl 16) |
| 1112 |
or (TColor32(R[Byte(fgColor shr 8 )] + InvR[Byte(bgColor shr 8)]) shl 8) |
| 1113 |
or (TColor32(R[Byte(fgColor) ] + InvR[Byte(bgColor) ]) ); |
| 1114 |
end; |
| 1115 |
end; |
| 1116 |
//------------------------------------------------------------------------------ |
| 1117 |
|
| 1118 |
function BlendToAlpha3(bgColor, fgColor: TColor32; blendOpacity: Byte): TColor32; |
| 1119 |
var |
| 1120 |
fgWeight: byte; |
| 1121 |
R, InvR: PByteArray; |
| 1122 |
bgA, fgA: byte; |
| 1123 |
begin |
| 1124 |
fgA := MulTable[blendOpacity, fgColor shr 24]; |
| 1125 |
bgA := bgColor shr 24; |
| 1126 |
if fgA = 0 then |
| 1127 |
Result := bgColor // must do first |
| 1128 |
else if (bgA = 0) or (fgA = 255) then |
| 1129 |
Result := (fgA shl 24) or (fgColor and $FFFFFF) |
| 1130 |
else |
| 1131 |
begin |
| 1132 |
//combine alphas ... |
| 1133 |
bgA := not MulTable[not fgA, not bgA]; |
| 1134 |
fgWeight := DivTable[fgA, bgA]; // fgWeight = amount foreground color |
| 1135 |
// contibutes to the result color |
| 1136 |
|
| 1137 |
R := PByteArray(@MulTable[fgWeight]); // ie weight of foreground |
| 1138 |
InvR := PByteArray(@MulTable[not fgWeight]); // ie weight of background |
| 1139 |
|
| 1140 |
Result := bgA shl 24 |
| 1141 |
or (TColor32(R[Byte(fgColor shr 16)] + InvR[Byte(bgColor shr 16)]) shl 16) |
| 1142 |
or (TColor32(R[Byte(fgColor shr 8 )] + InvR[Byte(bgColor shr 8)]) shl 8) |
| 1143 |
or (TColor32(R[Byte(fgColor) ] + InvR[Byte(bgColor) ]) ); |
| 1144 |
end; |
| 1145 |
end; |
| 1146 |
//------------------------------------------------------------------------------ |
| 1147 |
|
| 1148 |
{$RANGECHECKS OFF} // negative array index is used |
| 1149 |
|
| 1150 |
{$IFNDEF CPUX64} |
| 1151 |
function BlendToAlphaLineX86(bgColorArr, fgColorArr: PColor32Array; |
| 1152 |
idx: nativeint): nativeint; |
| 1153 |
// Helper function for x86 code, reduces the CPU register pressure in |
| 1154 |
// BlendToAlphaLine(). |
| 1155 |
var |
| 1156 |
fgWeight: byte; |
| 1157 |
R, InvR: PByteArray; |
| 1158 |
fgA, bgA, newBgA: byte; |
| 1159 |
fgCol, bgCol: TColor32; |
| 1160 |
begin |
| 1161 |
fgCol := fgColorArr[idx]; |
| 1162 |
bgCol := bgColorArr[idx]; |
| 1163 |
Result := idx; // idx - negative offset into color arrays |
| 1164 |
|
| 1165 |
while True do |
| 1166 |
begin |
| 1167 |
fgA := fgCol shr 24; |
| 1168 |
bgA := bgCol shr 24; |
| 1169 |
|
| 1170 |
//combine alphas ... |
| 1171 |
newBgA := not MulTable[not fgA, not bgA]; |
| 1172 |
fgWeight := DivTable[fgA, newBgA]; //fgWeight = amount foreground color |
| 1173 |
//contibutes to total (result) color |
| 1174 |
|
| 1175 |
R := PByteArray(@MulTable[fgWeight]); //ie weight of foreground |
| 1176 |
InvR := PByteArray(@MulTable[not fgWeight]); //ie weight of foreground |
| 1177 |
|
| 1178 |
while True do |
| 1179 |
begin |
| 1180 |
bgColorArr[Result] := TColor32(newBgA) shl 24 |
| 1181 |
or (TColor32(R[Byte(fgCol shr 16)] + InvR[Byte(bgCol shr 16)]) shl 16) |
| 1182 |
or (TColor32(R[Byte(fgCol shr 8 )] + InvR[Byte(bgCol shr 8)]) shl 8) |
| 1183 |
or (TColor32(R[Byte(fgCol) ] + InvR[Byte(bgCol) ]) ); |
| 1184 |
inc(Result); |
| 1185 |
if Result = 0 then exit; |
| 1186 |
|
| 1187 |
fgCol := fgColorArr[Result]; |
| 1188 |
bgCol := bgColorArr[Result]; |
| 1189 |
|
| 1190 |
// if both alpha channels are the same in the new pixels, we |
| 1191 |
// can use the already calculated R/InvR tables. |
| 1192 |
if (fgCol shr 24 <> fgA) or (bgCol shr 24 <> bgA) then break; |
| 1193 |
end; |
| 1194 |
// return if we have alpha channel values for which we have special code |
| 1195 |
if (fgCol and $FF000000 = 0) or (fgCol and $FF000000 = $FF000000) or (bgCol and $FF000000 = 0) then exit; |
| 1196 |
end; |
| 1197 |
end; |
| 1198 |
//------------------------------------------------------------------------------ |
| 1199 |
{$ENDIF ~CPUX64} |
| 1200 |
|
| 1201 |
procedure BlendToAlphaLine(bgColor, fgColor: PColor32; width: nativeint); |
| 1202 |
label |
| 1203 |
LabelBgAlphaIsZero; |
| 1204 |
var |
| 1205 |
bgColorArr, fgColorArr: PColor32Array; |
| 1206 |
bgCol, fgCol: TColor32; |
| 1207 |
{$IFDEF CPUX64} |
| 1208 |
fgWeight, fgA, bgA: byte; |
| 1209 |
R, InvR: PByteArray; |
| 1210 |
{$ENDIF CPUX64} |
| 1211 |
begin |
| 1212 |
//(see https://en.wikipedia.org/wiki/Alpha_compositing) |
| 1213 |
|
| 1214 |
// Use the negative offset trick to only increment the array "width" |
| 1215 |
// until it reaches zero. And by offsetting the arrays by "width", |
| 1216 |
// the negative "width" values also becomes the index into these arrays. |
| 1217 |
inc(bgColor, width); |
| 1218 |
inc(fgColor, width); |
| 1219 |
width := -width; |
| 1220 |
|
| 1221 |
bgColorArr := PColor32Array(bgColor); |
| 1222 |
fgColorArr := PColor32Array(fgColor); |
| 1223 |
|
| 1224 |
while width < 0 do |
| 1225 |
begin |
| 1226 |
bgCol := bgColorArr[width]; |
| 1227 |
fgCol := fgColorArr[width]; |
| 1228 |
|
| 1229 |
// bgColor.A is zero => change bgColor to fgColor |
| 1230 |
while bgCol shr 24 = 0 do |
| 1231 |
begin |
| 1232 |
LabelBgAlphaIsZero: |
| 1233 |
bgColorArr[width] := fgCol; |
| 1234 |
inc(width); |
| 1235 |
if width = 0 then exit; |
| 1236 |
fgCol := fgColorArr[width]; |
| 1237 |
bgCol := bgColorArr[width]; |
| 1238 |
end; |
| 1239 |
|
| 1240 |
// fgColor.A is zero => don't change bgColor |
| 1241 |
while fgCol shr 24 = 0 do |
| 1242 |
begin |
| 1243 |
// bgColorArr[w] := bgColorArr[w]; |
| 1244 |
inc(width); |
| 1245 |
if width = 0 then exit; |
| 1246 |
fgCol := fgColorArr[width]; |
| 1247 |
bgCol := bgColorArr[width]; |
| 1248 |
if bgCol shr 24 = 0 then goto LabelBgAlphaIsZero; |
| 1249 |
end; |
| 1250 |
|
| 1251 |
// fgColor.A is 255 => change bgColor to fgColor |
| 1252 |
while fgCol shr 24 = 255 do |
| 1253 |
begin |
| 1254 |
bgColorArr[width] := fgCol; |
| 1255 |
inc(width); |
| 1256 |
if width = 0 then exit; |
| 1257 |
fgCol := fgColorArr[width]; |
| 1258 |
bgCol := bgColorArr[width]; |
| 1259 |
if bgCol shr 24 = 0 then goto LabelBgAlphaIsZero; |
| 1260 |
end; |
| 1261 |
|
| 1262 |
{$IFDEF CPUX64} |
| 1263 |
// x64 has more CPU registers than x86 and calling BlendToAlphaLineX86 |
| 1264 |
// is slower, so we inline it. |
| 1265 |
|
| 1266 |
//combine alphas ... |
| 1267 |
fgA := fgCol shr 24; |
| 1268 |
bgA := bgCol shr 24; |
| 1269 |
bgA := not MulTable[not fgA, not bgA]; |
| 1270 |
fgWeight := DivTable[fgA, bgA]; //fgWeight = amount foreground color |
| 1271 |
//contibutes to total (result) color |
| 1272 |
|
| 1273 |
R := PByteArray(@MulTable[fgWeight]); //ie weight of foreground |
| 1274 |
InvR := PByteArray(@MulTable[not fgWeight]); //ie weight of foreground |
| 1275 |
|
| 1276 |
bgColorArr[width] := TColor32(bgA) shl 24 |
| 1277 |
or (TColor32(R[Byte(fgCol shr 16)] + InvR[Byte(bgCol shr 16)]) shl 16) |
| 1278 |
or (TColor32(R[Byte(fgCol shr 8 )] + InvR[Byte(bgCol shr 8)]) shl 8) |
| 1279 |
or (TColor32(R[Byte(fgCol) ] + InvR[Byte(bgCol) ]) ); |
| 1280 |
inc(width); |
| 1281 |
{$ELSE} |
| 1282 |
// x86 has not enough CPU registers and the loops above will suffer if we |
| 1283 |
// inline the code. So we let the compiler use a "new set" of CPU registers |
| 1284 |
// by calling a function. |
| 1285 |
width := BlendToAlphaLineX86(bgColorArr, fgColorArr, width); |
| 1286 |
{$ENDIF CPUX64} |
| 1287 |
end; |
| 1288 |
end; |
| 1289 |
//------------------------------------------------------------------------------ |
| 1290 |
|
| 1291 |
{ |
| 1292 |
// reference implementation |
| 1293 |
procedure BlendToAlphaLine(bgColor, fgColor: PColor32; width: nativeint); |
| 1294 |
var |
| 1295 |
fgWeight: byte; |
| 1296 |
R, InvR: PByteArray; |
| 1297 |
bgA, fgA: Byte; |
| 1298 |
bgColorArr, fgColorArr: PColor32Array; |
| 1299 |
bgCol, fgCol: TColor32; |
| 1300 |
begin |
| 1301 |
//(see https://en.wikipedia.org/wiki/Alpha_compositing) |
| 1302 |
|
| 1303 |
// Use the negative offset trick to only increment the array "width" |
| 1304 |
// until it reaches zero. And by offsetting the arrays by "width", |
| 1305 |
// the negative "width" values also becomes the index into these arrays. |
| 1306 |
inc(bgColor, width); |
| 1307 |
inc(fgColor, width); |
| 1308 |
width := -width; |
| 1309 |
|
| 1310 |
bgColorArr := PColor32Array(bgColor); |
| 1311 |
fgColorArr := PColor32Array(fgColor); |
| 1312 |
|
| 1313 |
while width < 0 do |
| 1314 |
begin |
| 1315 |
bgCol := bgColorArr[width]; |
| 1316 |
fgCol := fgColorArr[width]; |
| 1317 |
bgA := bgCol shr 24; |
| 1318 |
if bgA = 0 then bgColorArr[width] := fgCol |
| 1319 |
else |
| 1320 |
begin |
| 1321 |
fgA := fgCol shr 24; |
| 1322 |
if fgA > 0 then |
| 1323 |
begin |
| 1324 |
if fgA = 255 then bgColorArr[width] := fgCol |
| 1325 |
else if fgA > 0 then |
| 1326 |
begin |
| 1327 |
//combine alphas ... |
| 1328 |
bgA := not MulTable[not fgA, not bgA]; |
| 1329 |
fgWeight := DivTable[fgA, bgA]; //fgWeight = amount foreground color |
| 1330 |
//contibutes to total (result) color |
| 1331 |
|
| 1332 |
R := PByteArray(@MulTable[fgWeight]); //ie weight of foreground |
| 1333 |
InvR := PByteArray(@MulTable[not fgWeight]); //ie weight of foreground |
| 1334 |
|
| 1335 |
bgColorArr[width] := TColor32(bgA) shl 24 |
| 1336 |
or (TColor32(R[Byte(fgCol shr 16)] + InvR[Byte(bgCol shr 16)]) shl 16) |
| 1337 |
or (TColor32(R[Byte(fgCol shr 8 )] + InvR[Byte(bgCol shr 8)]) shl 8) |
| 1338 |
or (TColor32(R[Byte(fgCol) ] + InvR[Byte(bgCol) ]) ); |
| 1339 |
end; |
| 1340 |
end; |
| 1341 |
end; |
| 1342 |
|
| 1343 |
inc(width); |
| 1344 |
end; |
| 1345 |
end;} |
| 1346 |
{$IFDEF RANGECHECKS_ENABLED} |
| 1347 |
{$RANGECHECKS ON} |
| 1348 |
{$ENDIF} |
| 1349 |
//------------------------------------------------------------------------------ |
| 1350 |
|
| 1351 |
function BlendMask(bgColor, alphaMask: TColor32): TColor32; |
| 1352 |
var |
| 1353 |
a: byte; |
| 1354 |
begin |
| 1355 |
a := MulTable[bgColor shr 24, alphaMask shr 24]; |
| 1356 |
if a <> 0 then Result := (TColor32(a) shl 24) or (bgColor and $00FFFFFF) |
| 1357 |
else Result := 0; |
| 1358 |
end; |
| 1359 |
//------------------------------------------------------------------------------ |
| 1360 |
|
| 1361 |
{$RANGECHECKS OFF} // negative array index is used |
| 1362 |
|
| 1363 |
procedure BlendMaskLine(bgColor, alphaMask: PColor32; width: nativeint); |
| 1364 |
label |
| 1365 |
SkipNone32; |
| 1366 |
var |
| 1367 |
a: byte; |
| 1368 |
begin |
| 1369 |
// Use the negative offset trick to only increment the array "width" |
| 1370 |
// until it reaches zero. And by offsetting the arrays by "width", |
| 1371 |
// the negative "width" values also becomes the index into these arrays. |
| 1372 |
inc(bgColor, width); |
| 1373 |
inc(alphaMask, width); |
| 1374 |
width := -width; |
| 1375 |
|
| 1376 |
// Handle special cases Alpha=0 or 255 as those are the most |
| 1377 |
// common values. |
| 1378 |
while width < 0 do |
| 1379 |
begin |
| 1380 |
// MulTable[0, fgA] -> 0, if bgColor is already 0 => skip |
| 1381 |
while PARGBArray(bgColor)[width].Color = 0 do |
| 1382 |
begin |
| 1383 |
SkipNone32: |
| 1384 |
inc(width); |
| 1385 |
if width = 0 then exit; |
| 1386 |
end; |
| 1387 |
a := PARGBArray(bgColor)[width].A; |
| 1388 |
// MulTable[0, fgA] -> 0 => replace color with 0 |
| 1389 |
while a = 0 do |
| 1390 |
begin |
| 1391 |
PColor32Array(bgColor)[width] := 0; |
| 1392 |
inc(width); |
| 1393 |
if width = 0 then exit; |
| 1394 |
if PARGBArray(bgColor)[width].Color = 0 then |
| 1395 |
goto SkipNone32; |
| 1396 |
a := PARGBArray(bgColor)[width].A; |
| 1397 |
end; |
| 1398 |
// MulTable[255, fgA] -> fgA => replace alpha with fgA |
| 1399 |
while a = 255 do |
| 1400 |
begin |
| 1401 |
PARGBArray(bgColor)[width].A := PARGBArray(alphaMask)[width].A; |
| 1402 |
inc(width); |
| 1403 |
if width = 0 then exit; |
| 1404 |
a := PARGBArray(bgColor)[width].A; |
| 1405 |
end; |
| 1406 |
|
| 1407 |
a := PARGBArray(alphaMask)[width].A; |
| 1408 |
// MulTable[bgA, 0] -> 0 => replace color with 0 |
| 1409 |
while a = 0 do |
| 1410 |
begin |
| 1411 |
PColor32Array(bgColor)[width] := 0; |
| 1412 |
inc(width); |
| 1413 |
if width = 0 then exit; |
| 1414 |
a := PARGBArray(alphaMask)[width].A; |
| 1415 |
end; |
| 1416 |
// MulTable[bgA, 255] -> bgA => nothing to do |
| 1417 |
while a = 255 do |
| 1418 |
begin |
| 1419 |
inc(width); |
| 1420 |
if width = 0 then exit; |
| 1421 |
a := PARGBArray(alphaMask)[width].A; |
| 1422 |
end; |
| 1423 |
|
| 1424 |
a := MulTable[PARGBArray(bgColor)[width].A, a]; |
| 1425 |
if a <> 0 then PARGBArray(bgColor)[width].A := a |
| 1426 |
else PColor32Array(bgColor)[width] := 0; |
| 1427 |
|
| 1428 |
inc(width); |
| 1429 |
end; |
| 1430 |
end; |
| 1431 |
//------------------------------------------------------------------------------ |
| 1432 |
|
| 1433 |
{ |
| 1434 |
// reference implementation |
| 1435 |
procedure BlendMaskLine(bgColor, alphaMask: PColor32; width: nativeint); |
| 1436 |
var |
| 1437 |
a: byte; |
| 1438 |
begin |
| 1439 |
// Use the negative offset trick to only increment the array "width" |
| 1440 |
// until it reaches zero. And by offsetting the arrays by "width", |
| 1441 |
// the negative "width" values also becomes the index into these arrays. |
| 1442 |
inc(bgColor, width); |
| 1443 |
inc(alphaMask, width); |
| 1444 |
width := -width; |
| 1445 |
|
| 1446 |
while width < 0 do |
| 1447 |
begin |
| 1448 |
a := MulTable[PARGBArray(bgColor)[width].A, |
| 1449 |
PARGBArray(alphaMask)[width].A]; |
| 1450 |
if a = 0 then PColor32Array(bgColor)[width] := 0 |
| 1451 |
else PARGBArray(bgColor)[width].A := a; |
| 1452 |
|
| 1453 |
inc(width); |
| 1454 |
end; |
| 1455 |
end;} |
| 1456 |
{$IFDEF RANGECHECKS_ENABLED} |
| 1457 |
{$RANGECHECKS ON} |
| 1458 |
{$ENDIF} |
| 1459 |
//------------------------------------------------------------------------------ |
| 1460 |
|
| 1461 |
function BlendAltMask(bgColor, alphaMask: TColor32): TColor32; |
| 1462 |
var |
| 1463 |
a: byte; |
| 1464 |
begin |
| 1465 |
a := MulTable[bgColor shr 24, (alphaMask shr 24) xor 255]; |
| 1466 |
if a <> 0 then Result := (TColor32(a) shl 24) or (bgColor and $00FFFFFF) |
| 1467 |
else Result := 0; |
| 1468 |
end; |
| 1469 |
//------------------------------------------------------------------------------ |
| 1470 |
|
| 1471 |
function BlendDifference(color1, color2: TColor32): TColor32; |
| 1472 |
var |
| 1473 |
fgA, bgA: byte; |
| 1474 |
begin |
| 1475 |
fgA := color2 shr 24; |
| 1476 |
bgA := color1 shr 24; |
| 1477 |
if fgA = 0 then Result := color1 |
| 1478 |
else if bgA = 0 then Result := color2 |
| 1479 |
else |
| 1480 |
begin |
| 1481 |
Result := TColor32(MulTable[(fgA xor 255), (bgA xor 255)] xor 255) shl 24 |
| 1482 |
or (TColor32(Abs(Byte(color2 shr 16) - Byte(color1 shr 16))) shl 16) |
| 1483 |
or (TColor32(Abs(Byte(color2 shr 8) - Byte(color1 shr 8))) shl 8) |
| 1484 |
or (TColor32(Abs(Byte(color2 ) - Byte(color1 ))) ); |
| 1485 |
end; |
| 1486 |
end; |
| 1487 |
//------------------------------------------------------------------------------ |
| 1488 |
|
| 1489 |
function BlendSubtract(bgColor, fgColor: TColor32): TColor32; |
| 1490 |
var |
| 1491 |
fgA, bgA: byte; |
| 1492 |
begin |
| 1493 |
fgA := fgColor shr 24; |
| 1494 |
bgA := bgColor shr 24; |
| 1495 |
if fgA = 0 then Result := bgColor |
| 1496 |
else if bgA = 0 then Result := fgColor |
| 1497 |
else |
| 1498 |
begin |
| 1499 |
Result := TColor32(MulTable[(fgA xor 255), (bgA xor 255)] xor 255) shl 24 |
| 1500 |
or (TColor32(ClampByte(Byte(fgColor shr 16) - Byte(bgColor shr 16))) shl 16) |
| 1501 |
or (TColor32(ClampByte(Byte(fgColor shr 8 ) - Byte(bgColor shr 8))) shl 8) |
| 1502 |
or (TColor32(ClampByte(Byte(fgColor ) - Byte(bgColor ))) ); |
| 1503 |
end; |
| 1504 |
end; |
| 1505 |
//------------------------------------------------------------------------------ |
| 1506 |
|
| 1507 |
function BlendLighten(bgColor, fgColor: TColor32): TColor32; |
| 1508 |
var |
| 1509 |
fgA, bgA: byte; |
| 1510 |
begin |
| 1511 |
fgA := fgColor shr 24; |
| 1512 |
bgA := bgColor shr 24; |
| 1513 |
if fgA = 0 then Result := bgColor |
| 1514 |
else if bgA = 0 then Result := fgColor |
| 1515 |
else |
| 1516 |
begin |
| 1517 |
Result := TColor32(MulTable[(fgA xor 255), (bgA xor 255)] xor 255) shl 24 |
| 1518 |
or (TColor32(Max(Byte(fgColor shr 16), Byte(bgColor shr 16))) shl 16) |
| 1519 |
or (TColor32(Max(Byte(fgColor shr 8 ), Byte(bgColor shr 8))) shl 8) |
| 1520 |
or (TColor32(Max(Byte(fgColor ), Byte(bgColor ))) ); |
| 1521 |
end; |
| 1522 |
end; |
| 1523 |
//------------------------------------------------------------------------------ |
| 1524 |
|
| 1525 |
function BlendDarken(bgColor, fgColor: TColor32): TColor32; |
| 1526 |
var |
| 1527 |
fgA, bgA: byte; |
| 1528 |
begin |
| 1529 |
fgA := fgColor shr 24; |
| 1530 |
bgA := bgColor shr 24; |
| 1531 |
if fgA = 0 then Result := bgColor |
| 1532 |
else if bgA = 0 then Result := fgColor |
| 1533 |
else |
| 1534 |
begin |
| 1535 |
Result := TColor32(MulTable[(fgA xor 255), (bgA xor 255)] xor 255) shl 24 |
| 1536 |
or (TColor32(Min(Byte(fgColor shr 16), Byte(bgColor shr 16))) shl 16) |
| 1537 |
or (TColor32(Min(Byte(fgColor shr 8 ), Byte(bgColor shr 8))) shl 8) |
| 1538 |
or (TColor32(Min(Byte(fgColor ), Byte(bgColor ))) ); |
| 1539 |
end; |
| 1540 |
end; |
| 1541 |
//------------------------------------------------------------------------------ |
| 1542 |
|
| 1543 |
function BlendBlueChannel(bgColor, blueMask: TColor32): TColor32; |
| 1544 |
begin |
| 1545 |
Result := (bgColor and $00FFFFFF) or |
| 1546 |
(TColor32(MulTable[bgColor shr 24, Byte(blueMask)]) shl 24); |
| 1547 |
end; |
| 1548 |
//------------------------------------------------------------------------------ |
| 1549 |
|
| 1550 |
function BlendInvertedMask(bgColor, alphaMask: TColor32): TColor32; |
| 1551 |
var |
| 1552 |
a: byte; |
| 1553 |
begin |
| 1554 |
a := MulTable[bgColor shr 24, (alphaMask shr 24) xor 255]; |
| 1555 |
if a < 2 then Result := 0 |
| 1556 |
else Result := (bgColor and $00FFFFFF) or (TColor32(a) shl 24); |
| 1557 |
end; |
| 1558 |
//------------------------------------------------------------------------------ |
| 1559 |
|
| 1560 |
{$RANGECHECKS OFF} // negative array index is used |
| 1561 |
|
| 1562 |
procedure BlendBlueChannelLine(bgColor, blueMask: PColor32; width: nativeint); |
| 1563 |
begin |
| 1564 |
inc(bgColor, width); |
| 1565 |
inc(blueMask, width); |
| 1566 |
width := -width; |
| 1567 |
while width < 0 do |
| 1568 |
begin |
| 1569 |
PARGBArray(bgColor)[width].A := |
| 1570 |
MulTable[PARGBArray(bgColor)[width].A, |
| 1571 |
PARGBArray(blueMask)[width].B]; |
| 1572 |
inc(width); |
| 1573 |
end; |
| 1574 |
end; |
| 1575 |
//------------------------------------------------------------------------------ |
| 1576 |
|
| 1577 |
procedure BlendInvertedMaskLine(bgColor, alphaMask: PColor32; width: nativeint); |
| 1578 |
var |
| 1579 |
a: byte; |
| 1580 |
begin |
| 1581 |
// Use the negative offset trick to only increment the array "width" |
| 1582 |
// until it reaches zero. And by offsetting the arrays by "width", |
| 1583 |
// the negative "width" values also becomes the index into these arrays. |
| 1584 |
inc(bgColor, width); |
| 1585 |
inc(alphaMask, width); |
| 1586 |
width := -width; |
| 1587 |
|
| 1588 |
while width < 0 do |
| 1589 |
begin |
| 1590 |
a := MulTable[PARGBArray(bgColor)[width].A, |
| 1591 |
PARGBArray(alphaMask)[width].A xor 255]; |
| 1592 |
if a < 2 then PColor32Array(bgColor)[width] := 0 |
| 1593 |
else PARGBArray(bgColor)[width].A := a; |
| 1594 |
|
| 1595 |
inc(width); |
| 1596 |
end; |
| 1597 |
end; |
| 1598 |
{$IFDEF RANGECHECKS_ENABLED} |
| 1599 |
{$RANGECHECKS ON} |
| 1600 |
{$ENDIF} |
| 1601 |
|
| 1602 |
//------------------------------------------------------------------------------ |
| 1603 |
// Compare functions (see ConvertToBoolMask, FloodFill & Vectorize) |
| 1604 |
//------------------------------------------------------------------------------ |
| 1605 |
|
| 1606 |
function CompareRGB(master, current: TColor32; tolerance: Integer): Boolean; |
| 1607 |
var |
| 1608 |
mast: TARGB absolute master; |
| 1609 |
curr: TARGB absolute current; |
| 1610 |
begin |
| 1611 |
if curr.A < $80 then |
| 1612 |
Result := false |
| 1613 |
else if (master and $FFFFFF) = (current and $FFFFFF) then |
| 1614 |
Result := true |
| 1615 |
else if tolerance = 0 then |
| 1616 |
Result := false |
| 1617 |
else result := |
| 1618 |
(Abs(curr.R - mast.R) <= tolerance) and |
| 1619 |
(Abs(curr.G - mast.G) <= tolerance) and |
| 1620 |
(Abs(curr.B - mast.B) <= tolerance); |
| 1621 |
end; |
| 1622 |
//------------------------------------------------------------------------------ |
| 1623 |
|
| 1624 |
function CompareAlpha(master, current: TColor32; tolerance: Integer): Boolean; |
| 1625 |
var |
| 1626 |
mast: TARGB absolute master; |
| 1627 |
curr: TARGB absolute current; |
| 1628 |
begin |
| 1629 |
if mast.A = curr.A then Result := true |
| 1630 |
else if tolerance = 0 then Result := false |
| 1631 |
else result := Abs(curr.A - mast.A) <= tolerance; |
| 1632 |
end; |
| 1633 |
//------------------------------------------------------------------------------ |
| 1634 |
|
| 1635 |
function CompareHue(master, current: TColor32; tolerance: Integer): Boolean; |
| 1636 |
var |
| 1637 |
curr, mast: THsl; |
| 1638 |
val: Integer; |
| 1639 |
begin |
| 1640 |
if TARGB(current).A < $80 then |
| 1641 |
begin |
| 1642 |
Result := false; |
| 1643 |
Exit; |
| 1644 |
end; |
| 1645 |
curr := RgbToHsl(current); |
| 1646 |
mast := RgbToHsl(master); |
| 1647 |
if curr.hue > mast.hue then |
| 1648 |
begin |
| 1649 |
val := curr.hue - mast.hue; |
| 1650 |
if val > 127 then val := mast.hue - curr.hue + 255; |
| 1651 |
end else |
| 1652 |
begin |
| 1653 |
val := mast.hue - curr.hue; |
| 1654 |
if val > 127 then val := curr.hue - mast.hue + 255; |
| 1655 |
end; |
| 1656 |
result := val <= tolerance; |
| 1657 |
end; |
| 1658 |
|
| 1659 |
//------------------------------------------------------------------------------ |
| 1660 |
// CompareEx functions (see ConvertToAlphaMask) |
| 1661 |
//------------------------------------------------------------------------------ |
| 1662 |
|
| 1663 |
function CompareRgbEx(master, current: TColor32): Byte; |
| 1664 |
var |
| 1665 |
mast: TARGB absolute master; |
| 1666 |
curr: TARGB absolute current; |
| 1667 |
res: Cardinal; |
| 1668 |
begin |
| 1669 |
res := Sqr(mast.R - curr.R) + Sqr(mast.G - curr.G) + Sqr(mast.B - curr.B); |
| 1670 |
if res >= 65025 then result := 255 |
| 1671 |
else result := Round(Sqrt(res)); |
| 1672 |
end; |
| 1673 |
//------------------------------------------------------------------------------ |
| 1674 |
|
| 1675 |
function CompareAlphaEx(master, current: TColor32): Byte; |
| 1676 |
var |
| 1677 |
mast: TARGB absolute master; |
| 1678 |
curr: TARGB absolute current; |
| 1679 |
begin |
| 1680 |
Result := abs(mast.A - curr.A); |
| 1681 |
end; |
| 1682 |
|
| 1683 |
//------------------------------------------------------------------------------ |
| 1684 |
// Miscellaneous functions ... |
| 1685 |
//------------------------------------------------------------------------------ |
| 1686 |
|
| 1687 |
function IsAlphaChar(c: Char): Boolean; |
| 1688 |
begin |
| 1689 |
Result := ((c >= 'A') and (c <= 'Z')) or ((c >= 'a') and (c <= 'z')); |
| 1690 |
end; |
| 1691 |
//------------------------------------------------------------------------------ |
| 1692 |
|
| 1693 |
procedure RectWidthHeight(const rec: TRect; out width, height: Integer); |
| 1694 |
begin |
| 1695 |
width := rec.Right - rec.Left; |
| 1696 |
height := rec.Bottom - rec.Top; |
| 1697 |
end; |
| 1698 |
//------------------------------------------------------------------------------ |
| 1699 |
|
| 1700 |
procedure RectWidthHeight(const rec: TRectD; out width, height: double); |
| 1701 |
begin |
| 1702 |
width := rec.Right - rec.Left; |
| 1703 |
height := rec.Bottom - rec.Top; |
| 1704 |
end; |
| 1705 |
//------------------------------------------------------------------------------ |
| 1706 |
|
| 1707 |
function RectWidth(const rec: TRect): Integer; |
| 1708 |
begin |
| 1709 |
Result := rec.Right - rec.Left; |
| 1710 |
end; |
| 1711 |
//------------------------------------------------------------------------------ |
| 1712 |
|
| 1713 |
function RectHeight(const rec: TRect): Integer; |
| 1714 |
begin |
| 1715 |
Result := rec.Bottom - rec.Top; |
| 1716 |
end; |
| 1717 |
//------------------------------------------------------------------------------ |
| 1718 |
|
| 1719 |
function IsEmptyRect(const rec: TRect): Boolean; |
| 1720 |
begin |
| 1721 |
Result := (rec.Right <= rec.Left) or (rec.Bottom <= rec.Top); |
| 1722 |
end; |
| 1723 |
//------------------------------------------------------------------------------ |
| 1724 |
|
| 1725 |
function IsEmptyRect(const rec: TRectD): Boolean; |
| 1726 |
begin |
| 1727 |
Result := (rec.Right <= rec.Left) or (rec.Bottom <= rec.Top); |
| 1728 |
end; |
| 1729 |
//------------------------------------------------------------------------------ |
| 1730 |
|
| 1731 |
function InvertColor(color: TColor32): TColor32; |
| 1732 |
begin |
| 1733 |
Result := color xor $00FFFFFF; |
| 1734 |
end; |
| 1735 |
//------------------------------------------------------------------------------ |
| 1736 |
|
| 1737 |
function GetAlpha(color: TColor32): Byte; |
| 1738 |
begin |
| 1739 |
Result := Byte(color shr 24); |
| 1740 |
end; |
| 1741 |
//------------------------------------------------------------------------------ |
| 1742 |
|
| 1743 |
function RGBColor(color: TColor32): Cardinal; |
| 1744 |
var |
| 1745 |
c : TARGB absolute color; |
| 1746 |
res: TARGB absolute Result; |
| 1747 |
begin |
| 1748 |
res.R := c.B; res.G := c.G; res.B := c.R; res.A := 0; |
| 1749 |
end; |
| 1750 |
//------------------------------------------------------------------------------ |
| 1751 |
|
| 1752 |
function Color32(a, r, g, b: Byte): TColor32; |
| 1753 |
var |
| 1754 |
res: TARGB absolute Result; |
| 1755 |
begin |
| 1756 |
res.A := a; res.R := r; res.G := g; res.B := b; |
| 1757 |
end; |
| 1758 |
//------------------------------------------------------------------------------ |
| 1759 |
|
| 1760 |
{$IFDEF MSWINDOWS} |
| 1761 |
function Color32(rgbColor: Integer): TColor32; |
| 1762 |
var |
| 1763 |
res: TARGB absolute Result; |
| 1764 |
begin |
| 1765 |
if rgbColor < 0 then |
| 1766 |
result := GetSysColor(rgbColor and $FFFFFF) else |
| 1767 |
result := rgbColor; |
| 1768 |
res.A := res.B; res.B := res.R; res.R := res.A; //byte swap |
| 1769 |
res.A := 255; |
| 1770 |
end; |
| 1771 |
//------------------------------------------------------------------------------ |
| 1772 |
|
| 1773 |
procedure FixPalette(p: PARGB; count: integer); |
| 1774 |
var |
| 1775 |
i: integer; |
| 1776 |
begin |
| 1777 |
for i := 1 to count do |
| 1778 |
begin |
| 1779 |
p.Color := SwapRedBlue(p.Color); |
| 1780 |
p.A := 255; |
| 1781 |
inc(p); |
| 1782 |
end; |
| 1783 |
end; |
| 1784 |
//------------------------------------------------------------------------------ |
| 1785 |
|
| 1786 |
function Get32bitBitmapInfoHeader(width, height: Integer): TBitmapInfoHeader; |
| 1787 |
begin |
| 1788 |
FillChar(Result, sizeof(Result), #0); |
| 1789 |
Result.biSize := sizeof(TBitmapInfoHeader); |
| 1790 |
Result.biWidth := width; |
| 1791 |
Result.biHeight := height; |
| 1792 |
Result.biPlanes := 1; |
| 1793 |
Result.biBitCount := 32; |
| 1794 |
Result.biSizeImage := width * Abs(height) * SizeOf(TColor32); |
| 1795 |
Result.biCompression := BI_RGB; |
| 1796 |
end; |
| 1797 |
//------------------------------------------------------------------------------ |
| 1798 |
{$ENDIF} |
| 1799 |
|
| 1800 |
function DPIAware(val: Integer): Integer; |
| 1801 |
begin |
| 1802 |
result := Round(val * DpiAwareOne); |
| 1803 |
end; |
| 1804 |
//------------------------------------------------------------------------------ |
| 1805 |
|
| 1806 |
function DPIAware(val: double): double; |
| 1807 |
begin |
| 1808 |
result := val * DpiAwareOne; |
| 1809 |
end; |
| 1810 |
//------------------------------------------------------------------------------ |
| 1811 |
|
| 1812 |
function DPIAware(const pt: TPoint): TPoint; |
| 1813 |
begin |
| 1814 |
result.X := Round(pt.X * DpiAwareOne); |
| 1815 |
result.Y := Round(pt.Y * DpiAwareOne); |
| 1816 |
end; |
| 1817 |
//------------------------------------------------------------------------------ |
| 1818 |
|
| 1819 |
function DPIAware(const pt: TPointD): TPointD; |
| 1820 |
begin |
| 1821 |
result.X := pt.X * DpiAwareOne; |
| 1822 |
result.Y := pt.Y * DpiAwareOne; |
| 1823 |
end; |
| 1824 |
//------------------------------------------------------------------------------ |
| 1825 |
|
| 1826 |
function DPIAware(const rec: TRect): TRect; |
| 1827 |
begin |
| 1828 |
result.Left := Round(rec.Left * DpiAwareOne); |
| 1829 |
result.Top := Round(rec.Top * DpiAwareOne); |
| 1830 |
result.Right := Round(rec.Right * DpiAwareOne); |
| 1831 |
result.Bottom := Round(rec.Bottom * DpiAwareOne); |
| 1832 |
end; |
| 1833 |
//------------------------------------------------------------------------------ |
| 1834 |
|
| 1835 |
function DPIAware(const rec: TRectD): TRectD; |
| 1836 |
begin |
| 1837 |
result.Left := rec.Left * DpiAwareOne; |
| 1838 |
result.Top := rec.Top * DpiAwareOne; |
| 1839 |
result.Right := rec.Right * DpiAwareOne; |
| 1840 |
result.Bottom := rec.Bottom * DpiAwareOne; |
| 1841 |
end; |
| 1842 |
//------------------------------------------------------------------------------ |
| 1843 |
|
| 1844 |
function GrayScale(color: TColor32): TColor32; |
| 1845 |
var |
| 1846 |
c: TARGB absolute color; |
| 1847 |
r: TARGB absolute result; |
| 1848 |
g: Byte; |
| 1849 |
begin |
| 1850 |
//https://www.w3.org/TR/AERT/#color-contrast |
| 1851 |
g := ClampByte(0.299 * c.R + 0.587 * c.G + 0.114 * c.B); |
| 1852 |
r.A := c.A; |
| 1853 |
r.R := g; r.G := g; r.B := g; |
| 1854 |
end; |
| 1855 |
//------------------------------------------------------------------------------ |
| 1856 |
|
| 1857 |
function ClampRange(val, min, max: Integer): Integer; |
| 1858 |
begin |
| 1859 |
if val < min then result := min |
| 1860 |
else if val > max then result := max |
| 1861 |
else result := val; |
| 1862 |
end; |
| 1863 |
//------------------------------------------------------------------------------ |
| 1864 |
|
| 1865 |
function ClampRange(val, min, max: double): double; |
| 1866 |
begin |
| 1867 |
if val < min then result := min |
| 1868 |
else if val > max then result := max |
| 1869 |
else result := val; |
| 1870 |
end; |
| 1871 |
//------------------------------------------------------------------------------ |
| 1872 |
|
| 1873 |
procedure ScaleRect(var rec: TRect; x,y: double); |
| 1874 |
begin |
| 1875 |
rec.Right := rec.Left + Round((rec.Right - rec.Left) * x); |
| 1876 |
rec.Bottom := rec.Top + Round((rec.Bottom - rec.Top) * y); |
| 1877 |
end; |
| 1878 |
//------------------------------------------------------------------------------ |
| 1879 |
|
| 1880 |
function IncPColor32(pc: Pointer; cnt: Integer): PColor32; |
| 1881 |
begin |
| 1882 |
result := PColor32(PByte(pc) + cnt * SizeOf(TColor32)); |
| 1883 |
end; |
| 1884 |
//------------------------------------------------------------------------------ |
| 1885 |
|
| 1886 |
function PointD(const X, Y: Double): TPointD; |
| 1887 |
begin |
| 1888 |
Result.X := X; |
| 1889 |
Result.Y := Y; |
| 1890 |
end; |
| 1891 |
//------------------------------------------------------------------------------ |
| 1892 |
|
| 1893 |
function PointD(const pt: TPoint): TPointD; |
| 1894 |
begin |
| 1895 |
Result.X := pt.X; |
| 1896 |
Result.Y := pt.Y; |
| 1897 |
end; |
| 1898 |
//------------------------------------------------------------------------------ |
| 1899 |
|
| 1900 |
function GetBoolMask(img: TImage32; reference: TColor32; |
| 1901 |
compareFunc: TCompareFunction; tolerance: Integer): TArrayOfByte; |
| 1902 |
var |
| 1903 |
i: integer; |
| 1904 |
pa: PByte; |
| 1905 |
pc: PColor32; |
| 1906 |
begin |
| 1907 |
result := nil; |
| 1908 |
if not assigned(img) or img.IsEmpty then Exit; |
| 1909 |
if not Assigned(compareFunc) then compareFunc := CompareRGB; |
| 1910 |
NewByteArray(Result, img.Width * img.Height, True); |
| 1911 |
pa := @Result[0]; |
| 1912 |
pc := img.PixelBase; |
| 1913 |
for i := 0 to img.Width * img.Height -1 do |
| 1914 |
begin |
| 1915 |
if compareFunc(reference, pc^, tolerance) then |
| 1916 |
{$IFDEF PBYTE} |
| 1917 |
pa^ := 1 else |
| 1918 |
pa^ := 0; |
| 1919 |
{$ELSE} |
| 1920 |
pa^ := #1 else |
| 1921 |
pa^ := #0; |
| 1922 |
{$ENDIF} |
| 1923 |
inc(pc); inc(pa); |
| 1924 |
end; |
| 1925 |
end; |
| 1926 |
//------------------------------------------------------------------------------ |
| 1927 |
|
| 1928 |
function GetColorMask(img: TImage32; reference: TColor32; |
| 1929 |
compareFunc: TCompareFunction; tolerance: Integer): TArrayOfColor32; |
| 1930 |
var |
| 1931 |
i: integer; |
| 1932 |
pDstPxl: PColor32; |
| 1933 |
pSrcPxl: PColor32; |
| 1934 |
begin |
| 1935 |
result := nil; |
| 1936 |
if not assigned(img) or img.IsEmpty then Exit; |
| 1937 |
if not Assigned(compareFunc) then compareFunc := CompareRGB; |
| 1938 |
NewColor32Array(Result, img.Width * img.Height, True); |
| 1939 |
pDstPxl := @Result[0]; |
| 1940 |
pSrcPxl := img.PixelBase; |
| 1941 |
for i := 0 to img.Width * img.Height -1 do |
| 1942 |
begin |
| 1943 |
if compareFunc(reference, pSrcPxl^, tolerance) then |
| 1944 |
pDstPxl^ := clWhite32 else |
| 1945 |
pDstPxl^ := clBlack32; |
| 1946 |
inc(pSrcPxl); inc(pDstPxl); |
| 1947 |
end; |
| 1948 |
end; |
| 1949 |
//------------------------------------------------------------------------------ |
| 1950 |
|
| 1951 |
function GetAlphaEx(master, current: TColor32): Byte; |
| 1952 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 1953 |
var |
| 1954 |
curr: TARGB absolute current; |
| 1955 |
begin |
| 1956 |
result := curr.A; //nb: 'master' is ignored |
| 1957 |
end; |
| 1958 |
//------------------------------------------------------------------------------ |
| 1959 |
|
| 1960 |
function GetByteMask(img: TImage32; reference: TColor32; |
| 1961 |
compareFunc: TCompareFunctionEx): TArrayOfByte; |
| 1962 |
var |
| 1963 |
i: integer; |
| 1964 |
pa: PByte; |
| 1965 |
pc: PColor32; |
| 1966 |
begin |
| 1967 |
result := nil; |
| 1968 |
if not assigned(img) or img.IsEmpty then Exit; |
| 1969 |
if not Assigned(compareFunc) then compareFunc := GetAlphaEx; |
| 1970 |
NewByteArray(Result, img.Width * img.Height, True); |
| 1971 |
pa := @Result[0]; |
| 1972 |
pc := img.PixelBase; |
| 1973 |
for i := 0 to img.Width * img.Height -1 do |
| 1974 |
begin |
| 1975 |
{$IFDEF PBYTE} |
| 1976 |
pa^ := compareFunc(reference, pc^); |
| 1977 |
{$ELSE} |
| 1978 |
pa^ := Char(compareFunc(reference, pc^)); |
| 1979 |
{$ENDIF} |
| 1980 |
inc(pc); inc(pa); |
| 1981 |
end; |
| 1982 |
end; |
| 1983 |
//------------------------------------------------------------------------------ |
| 1984 |
|
| 1985 |
function RgbToHsl(color: TColor32): THsl; |
| 1986 |
var |
| 1987 |
rgba: TARGB absolute color; |
| 1988 |
hsl: THsl absolute result; |
| 1989 |
r,g,b: byte; |
| 1990 |
maxRGB, minRGB, mAdd, mSub: Integer; |
| 1991 |
begin |
| 1992 |
//https://en.wikipedia.org/wiki/HSL_and_HSV and |
| 1993 |
//http://en.wikipedia.org/wiki/HSL_color_space |
| 1994 |
{$IF DEFINED(ANDROID)} |
| 1995 |
color := SwapRedBlue(color); |
| 1996 |
{$IFEND} |
| 1997 |
|
| 1998 |
r := rgba.R; g := rgba.G; b := rgba.B; |
| 1999 |
maxRGB := Max(r, Max(g, b)); |
| 2000 |
minRGB := Min(r, Min(g, b)); |
| 2001 |
mAdd := maxRGB + minRGB; |
| 2002 |
hsl.lum := mAdd shr 1; |
| 2003 |
hsl.alpha := rgba.A; |
| 2004 |
|
| 2005 |
if maxRGB = minRGB then |
| 2006 |
begin |
| 2007 |
hsl.hue := 0; //hsl.hue is undefined when gray |
| 2008 |
hsl.sat := 0; |
| 2009 |
Exit; |
| 2010 |
end; |
| 2011 |
|
| 2012 |
mSub := maxRGB - minRGB; |
| 2013 |
if mAdd <= 255 then |
| 2014 |
hsl.sat := DivTable[mSub, mAdd] else |
| 2015 |
hsl.sat := DivTable[mSub, 511 - mAdd]; |
| 2016 |
|
| 2017 |
mSub := mSub * 6; |
| 2018 |
if r = maxRGB then |
| 2019 |
begin |
| 2020 |
if g >= b then |
| 2021 |
hsl.hue := (g - b) * 255 div mSub else |
| 2022 |
hsl.hue := 255 - ((b - g) * 255 div mSub); |
| 2023 |
end |
| 2024 |
else if G = maxRGB then |
| 2025 |
begin |
| 2026 |
if b > r then |
| 2027 |
hsl.hue := 85 + (b - r) * 255 div mSub else |
| 2028 |
hsl.hue := 85 - (r - b) * 255 div mSub; |
| 2029 |
end else |
| 2030 |
begin |
| 2031 |
if r > g then |
| 2032 |
hsl.hue := 170 + (r - g) * 255 div mSub else |
| 2033 |
hsl.hue := 170 - (g - r) * 255 div mSub; |
| 2034 |
end; |
| 2035 |
end; |
| 2036 |
//------------------------------------------------------------------------------ |
| 2037 |
|
| 2038 |
function HslToRgb(hslColor: THsl): TColor32; |
| 2039 |
var |
| 2040 |
rgba: TARGB absolute result; |
| 2041 |
hsl: THsl absolute hslColor; |
| 2042 |
c, x, m, a: Integer; |
| 2043 |
begin |
| 2044 |
//formula from https://www.rapidtables.com/convert/color/hsl-to-rgb.html |
| 2045 |
c := ((255 - abs(2 * hsl.lum - 255)) * hsl.sat) shr 8; |
| 2046 |
a := 252 - (hsl.hue mod 85) * 6; |
| 2047 |
x := (c * (255 - abs(a))) shr 8; |
| 2048 |
m := hsl.lum - c shr 1{div 2}; // Delphi's 64bit compiler can't optimize this |
| 2049 |
rgba.A := hsl.alpha; |
| 2050 |
case (hsl.hue * 6) shr 8 of |
| 2051 |
0: begin rgba.R := c + m; rgba.G := x + m; rgba.B := 0 + m; end; |
| 2052 |
1: begin rgba.R := x + m; rgba.G := c + m; rgba.B := 0 + m; end; |
| 2053 |
2: begin rgba.R := 0 + m; rgba.G := c + m; rgba.B := x + m; end; |
| 2054 |
3: begin rgba.R := 0 + m; rgba.G := x + m; rgba.B := c + m; end; |
| 2055 |
4: begin rgba.R := x + m; rgba.G := 0 + m; rgba.B := c + m; end; |
| 2056 |
5: begin rgba.R := c + m; rgba.G := 0 + m; rgba.B := x + m; end; |
| 2057 |
end; |
| 2058 |
{$IF DEFINED(ANDROID)} |
| 2059 |
Result := SwapRedBlue(Result); |
| 2060 |
{$IFEND} |
| 2061 |
end; |
| 2062 |
//------------------------------------------------------------------------------ |
| 2063 |
|
| 2064 |
function AdjustHue(color: TColor32; percent: Integer): TColor32; |
| 2065 |
var |
| 2066 |
hsl: THsl; |
| 2067 |
begin |
| 2068 |
percent := percent mod 100; |
| 2069 |
if percent < 0 then inc(percent, 100); |
| 2070 |
hsl := RgbToHsl(color); |
| 2071 |
hsl.hue := (hsl.hue + Round(percent*255/100)) mod 256; |
| 2072 |
result := HslToRgb(hsl); |
| 2073 |
end; |
| 2074 |
//------------------------------------------------------------------------------ |
| 2075 |
|
| 2076 |
function ArrayOfColor32ToArrayHSL(const clr32Arr: TArrayOfColor32): TArrayofHSL; |
| 2077 |
var |
| 2078 |
i, len: Integer; |
| 2079 |
begin |
| 2080 |
len := length(clr32Arr); |
| 2081 |
setLength(result, len); |
| 2082 |
for i := 0 to len -1 do |
| 2083 |
result[i] := RgbToHsl(clr32Arr[i]); |
| 2084 |
end; |
| 2085 |
//------------------------------------------------------------------------------ |
| 2086 |
|
| 2087 |
function ArrayOfHSLToArrayColor32(const hslArr: TArrayofHSL): TArrayOfColor32; |
| 2088 |
var |
| 2089 |
i, len: Integer; |
| 2090 |
begin |
| 2091 |
len := length(hslArr); |
| 2092 |
NewColor32Array(result, len, True); |
| 2093 |
for i := 0 to len -1 do |
| 2094 |
result[i] := HslToRgb(hslArr[i]); |
| 2095 |
end; |
| 2096 |
//------------------------------------------------------------------------------ |
| 2097 |
|
| 2098 |
function NameToId(Name: PChar): Longint; |
| 2099 |
begin |
| 2100 |
if Name < Pointer(30) then |
| 2101 |
begin |
| 2102 |
Result := Longint(Name) |
| 2103 |
end else |
| 2104 |
begin |
| 2105 |
if Name^ = '#' then inc(Name); |
| 2106 |
Result := StrToIntDef(Name, 0); |
| 2107 |
if Result > 65535 then Result := 0; |
| 2108 |
end; |
| 2109 |
end; |
| 2110 |
//------------------------------------------------------------------------------ |
| 2111 |
|
| 2112 |
function CreateResourceStream(const resName: string; |
| 2113 |
resType: PChar): TResourceStream; |
| 2114 |
var |
| 2115 |
nameId, typeId: Cardinal; |
| 2116 |
begin |
| 2117 |
Result := nil; |
| 2118 |
typeId := NameToId(resType); |
| 2119 |
if (typeId > 0) then resType := PChar(typeId) |
| 2120 |
else if (resType = 'BMP') then resType := RT_BITMAP; |
| 2121 |
|
| 2122 |
nameId := NameToId(PChar(resName)); |
| 2123 |
if nameId > 0 then |
| 2124 |
begin |
| 2125 |
if FindResource(hInstance, PChar(nameId), resType) <> 0 then |
| 2126 |
Result := TResourceStream.CreateFromID(hInstance, nameId, resType); |
| 2127 |
end else |
| 2128 |
begin |
| 2129 |
if FindResource(hInstance, PChar(resName), resType) <> 0 then |
| 2130 |
Result := TResourceStream.Create(hInstance, PChar(resName), resType); |
| 2131 |
end; |
| 2132 |
end; |
| 2133 |
|
| 2134 |
//------------------------------------------------------------------------------ |
| 2135 |
// TRectD methods (and helpers) |
| 2136 |
//------------------------------------------------------------------------------ |
| 2137 |
|
| 2138 |
function TRectD.IsEmpty: Boolean; |
| 2139 |
begin |
| 2140 |
result := (right <= left) or (bottom <= top); |
| 2141 |
end; |
| 2142 |
//------------------------------------------------------------------------------ |
| 2143 |
|
| 2144 |
function TRectD.Width: double; |
| 2145 |
begin |
| 2146 |
result := Max(0, right - left); |
| 2147 |
end; |
| 2148 |
//------------------------------------------------------------------------------ |
| 2149 |
|
| 2150 |
function TRectD.Height: double; |
| 2151 |
begin |
| 2152 |
result := Max(0, bottom - top); |
| 2153 |
end; |
| 2154 |
//------------------------------------------------------------------------------ |
| 2155 |
|
| 2156 |
function TRectD.MidPoint: TPointD; |
| 2157 |
begin |
| 2158 |
Result.X := (Right + Left)/2; |
| 2159 |
Result.Y := (Bottom + Top)/2; |
| 2160 |
end; |
| 2161 |
//------------------------------------------------------------------------------ |
| 2162 |
|
| 2163 |
{$IFNDEF RECORD_METHODS} |
| 2164 |
function TRectD.TopLeft: TPointD; |
| 2165 |
begin |
| 2166 |
Result.X := Left; |
| 2167 |
Result.Y := Top; |
| 2168 |
end; |
| 2169 |
//------------------------------------------------------------------------------ |
| 2170 |
|
| 2171 |
function TRectD.BottomRight: TPointD; |
| 2172 |
begin |
| 2173 |
Result.X := Right; |
| 2174 |
Result.Y := Bottom; |
| 2175 |
end; |
| 2176 |
//------------------------------------------------------------------------------ |
| 2177 |
{$ENDIF} |
| 2178 |
|
| 2179 |
function TRectD.Normalize: Boolean; |
| 2180 |
var |
| 2181 |
d: double; |
| 2182 |
begin |
| 2183 |
Result := false; |
| 2184 |
if Left > Right then |
| 2185 |
begin |
| 2186 |
d := Left; |
| 2187 |
Left := Right; |
| 2188 |
Right := d; |
| 2189 |
Result := True; |
| 2190 |
end; |
| 2191 |
if Top > Bottom then |
| 2192 |
begin |
| 2193 |
d := Top; |
| 2194 |
Top := Bottom; |
| 2195 |
Bottom := d; |
| 2196 |
Result := True; |
| 2197 |
end; |
| 2198 |
end; |
| 2199 |
//------------------------------------------------------------------------------ |
| 2200 |
|
| 2201 |
function TRectD.Contains(const Pt: TPoint): Boolean; |
| 2202 |
begin |
| 2203 |
Result := (pt.X >= Left) and (pt.X < Right) and |
| 2204 |
(pt.Y >= Top) and (pt.Y < Bottom); |
| 2205 |
end; |
| 2206 |
//------------------------------------------------------------------------------ |
| 2207 |
|
| 2208 |
function TRectD.Contains(const Pt: TPointD): Boolean; |
| 2209 |
begin |
| 2210 |
Result := (pt.X >= Left) and (pt.X < Right) and |
| 2211 |
(pt.Y >= Top) and (pt.Y < Bottom); |
| 2212 |
end; |
| 2213 |
//------------------------------------------------------------------------------ |
| 2214 |
|
| 2215 |
function RectD(left, top, right, bottom: double): TRectD; |
| 2216 |
begin |
| 2217 |
result.Left := left; |
| 2218 |
result.Top := top; |
| 2219 |
result.Right := right; |
| 2220 |
result.Bottom := bottom; |
| 2221 |
end; |
| 2222 |
//------------------------------------------------------------------------------ |
| 2223 |
|
| 2224 |
function RectD(const rec: TRect): TRectD; |
| 2225 |
begin |
| 2226 |
with rec do |
| 2227 |
begin |
| 2228 |
result.Left := left; |
| 2229 |
result.Top := top; |
| 2230 |
result.Right := right; |
| 2231 |
result.Bottom := bottom; |
| 2232 |
end; |
| 2233 |
end; |
| 2234 |
|
| 2235 |
//------------------------------------------------------------------------------ |
| 2236 |
// TImage32 methods |
| 2237 |
//------------------------------------------------------------------------------ |
| 2238 |
|
| 2239 |
constructor TImage32.Create(width: Integer; height: Integer); |
| 2240 |
begin |
| 2241 |
fAntiAliased := true; |
| 2242 |
fResampler := DefaultResampler; |
| 2243 |
fwidth := Max(0, width); |
| 2244 |
fheight := Max(0, height); |
| 2245 |
NewColor32Array(fPixels, fwidth * fheight); |
| 2246 |
end; |
| 2247 |
//------------------------------------------------------------------------------ |
| 2248 |
|
| 2249 |
constructor TImage32.Create(const src: TArrayOfColor32; width: Integer; height: Integer); |
| 2250 |
begin |
| 2251 |
fAntiAliased := true; |
| 2252 |
fResampler := DefaultResampler; |
| 2253 |
|
| 2254 |
width := Max(0, width); |
| 2255 |
height := Max(0, height); |
| 2256 |
if Length(src) <> width * height then |
| 2257 |
raise Exception.Create(rsInvalidImageArrayData); |
| 2258 |
|
| 2259 |
fWidth := width; |
| 2260 |
fHeight := height; |
| 2261 |
fPixels := src; |
| 2262 |
end; |
| 2263 |
//------------------------------------------------------------------------------ |
| 2264 |
|
| 2265 |
constructor TImage32.Create(src: TImage32); |
| 2266 |
begin |
| 2267 |
Assign(src); |
| 2268 |
end; |
| 2269 |
//------------------------------------------------------------------------------ |
| 2270 |
|
| 2271 |
constructor TImage32.Create(src: TImage32; const srcRec: TRect); |
| 2272 |
var |
| 2273 |
rec: TRect; |
| 2274 |
begin |
| 2275 |
fAntiAliased := src.AntiAliased; |
| 2276 |
fResampler := src.fResampler; |
| 2277 |
types.IntersectRect(rec, src.Bounds, srcRec); |
| 2278 |
RectWidthHeight(rec, fWidth, fHeight); |
| 2279 |
if (fWidth = 0) or (fheight = 0) then Exit; |
| 2280 |
fPixels := src.CopyPixels(rec); |
| 2281 |
end; |
| 2282 |
//------------------------------------------------------------------------------ |
| 2283 |
|
| 2284 |
destructor TImage32.Destroy; |
| 2285 |
begin |
| 2286 |
fPixels := nil; |
| 2287 |
inherited; |
| 2288 |
end; |
| 2289 |
//------------------------------------------------------------------------------ |
| 2290 |
|
| 2291 |
class function TImage32.IsRegisteredFormat(const ext: string): Boolean; |
| 2292 |
begin |
| 2293 |
result := Assigned(TImage32.GetImageFormatClass(ext)); |
| 2294 |
end; |
| 2295 |
//------------------------------------------------------------------------------ |
| 2296 |
|
| 2297 |
class procedure TImage32.RegisterImageFormatClass(ext: string; |
| 2298 |
bm32ExClass: TImageFormatClass; clipPriority: TClipboardPriority); |
| 2299 |
var |
| 2300 |
i: Integer; |
| 2301 |
imgFmtRec: PImgFmtRec; |
| 2302 |
isNewFormat: Boolean; |
| 2303 |
begin |
| 2304 |
if not Assigned(ImageFormatClassList) then CreateImageFormatList; |
| 2305 |
|
| 2306 |
if (ext = '') or (ext = '.') then Exit; |
| 2307 |
if (ext[1] = '.') then Delete(ext, 1,1); |
| 2308 |
if not IsAlphaChar(ext[1]) then Exit; |
| 2309 |
isNewFormat := true; |
| 2310 |
|
| 2311 |
// avoid duplicates but still allow overriding |
| 2312 |
for i := 0 to imageFormatClassList.count -1 do |
| 2313 |
begin |
| 2314 |
imgFmtRec := PImgFmtRec(imageFormatClassList[i]); |
| 2315 |
if SameText(imgFmtRec.Fmt, ext) then |
| 2316 |
begin |
| 2317 |
imgFmtRec.Obj := bm32ExClass; // replace prior class |
| 2318 |
if imgFmtRec.SortOrder = clipPriority then |
| 2319 |
Exit; // re-sorting isn't required |
| 2320 |
imgFmtRec.SortOrder := clipPriority; |
| 2321 |
isNewFormat := false; |
| 2322 |
Break; |
| 2323 |
end; |
| 2324 |
end; |
| 2325 |
|
| 2326 |
if isNewFormat then |
| 2327 |
begin |
| 2328 |
new(imgFmtRec); |
| 2329 |
imgFmtRec.Fmt := ext; |
| 2330 |
imgFmtRec.SortOrder := clipPriority; |
| 2331 |
imgFmtRec.Obj := bm32ExClass; |
| 2332 |
ImageFormatClassList.Add(imgFmtRec); |
| 2333 |
end; |
| 2334 |
|
| 2335 |
// Sort with lower priority before higher. |
| 2336 |
// Sorting here is arguably inefficient but, with so few |
| 2337 |
// entries, this inefficiency will be inconsequential. |
| 2338 |
|
| 2339 |
{$IFDEF XPLAT_GENERICS} |
| 2340 |
ImageFormatClassList.Sort(TComparer<PImgFmtRec>.Construct( |
| 2341 |
function(const imgFmtRec1, imgFmtRec2: PImgFmtRec): Integer |
| 2342 |
begin |
| 2343 |
Result := Integer(imgFmtRec1.SortOrder) - Integer(imgFmtRec2.SortOrder); |
| 2344 |
end)); |
| 2345 |
{$ELSE} |
| 2346 |
ImageFormatClassList.Sort(ImageFormatClassListSort); |
| 2347 |
{$ENDIF} |
| 2348 |
end; |
| 2349 |
//------------------------------------------------------------------------------ |
| 2350 |
|
| 2351 |
class function TImage32.GetImageFormatClass(const ext: string): TImageFormatClass; |
| 2352 |
var |
| 2353 |
i: Integer; |
| 2354 |
pattern: string; |
| 2355 |
imgFmtRec: PImgFmtRec; |
| 2356 |
begin |
| 2357 |
Result := nil; |
| 2358 |
pattern := ext; |
| 2359 |
if (pattern = '') or (pattern = '.') then Exit; |
| 2360 |
if pattern[1] = '.' then Delete(pattern, 1,1); |
| 2361 |
|
| 2362 |
//try for highest priority first |
| 2363 |
for i := imageFormatClassList.count -1 downto 0 do |
| 2364 |
begin |
| 2365 |
imgFmtRec := PImgFmtRec(imageFormatClassList[i]); |
| 2366 |
if not SameText(imgFmtRec.Fmt, pattern) then Continue; |
| 2367 |
Result := imgFmtRec.Obj; |
| 2368 |
break; |
| 2369 |
end; |
| 2370 |
end; |
| 2371 |
//------------------------------------------------------------------------------ |
| 2372 |
|
| 2373 |
class function TImage32.GetImageFormatClass(stream: TStream): TImageFormatClass; |
| 2374 |
var |
| 2375 |
i: integer; |
| 2376 |
begin |
| 2377 |
Result := nil; |
| 2378 |
for i := 0 to imageFormatClassList.count -1 do |
| 2379 |
with PImgFmtRec(imageFormatClassList[i])^ do |
| 2380 |
if Obj.IsValidImageStream(stream) then |
| 2381 |
begin |
| 2382 |
Result := Obj; |
| 2383 |
break; |
| 2384 |
end; |
| 2385 |
end; |
| 2386 |
//------------------------------------------------------------------------------ |
| 2387 |
|
| 2388 |
procedure TImage32.Assign(src: TImage32); |
| 2389 |
begin |
| 2390 |
if assigned(src) then |
| 2391 |
src.AssignTo(self); |
| 2392 |
end; |
| 2393 |
//------------------------------------------------------------------------------ |
| 2394 |
|
| 2395 |
procedure TImage32.AssignTo(dst: TImage32); |
| 2396 |
begin |
| 2397 |
if dst = self then Exit; |
| 2398 |
dst.BeginUpdate; |
| 2399 |
try |
| 2400 |
dst.AssignSettings(Self); |
| 2401 |
try |
| 2402 |
dst.fPixels := System.Copy(fPixels, 0, Length(fPixels)); |
| 2403 |
dst.fWidth := fWidth; |
| 2404 |
dst.fHeight := fHeight; |
| 2405 |
dst.Resized; |
| 2406 |
except |
| 2407 |
dst.SetSize(0,0); |
| 2408 |
end; |
| 2409 |
finally |
| 2410 |
dst.EndUpdate; |
| 2411 |
end; |
| 2412 |
dst.fColorCount := fColorCount; // dst.EndUpdate called ResetColorCount |
| 2413 |
end; |
| 2414 |
//------------------------------------------------------------------------------ |
| 2415 |
|
| 2416 |
procedure TImage32.AssignSettings(src: TImage32); |
| 2417 |
begin |
| 2418 |
if assigned(src) and (src <> Self) then |
| 2419 |
begin |
| 2420 |
BeginUpdate; |
| 2421 |
try |
| 2422 |
fResampler := src.fResampler; |
| 2423 |
fIsPremultiplied := src.fIsPremultiplied; |
| 2424 |
fAntiAliased := src.fAntiAliased; |
| 2425 |
ResetColorCount; |
| 2426 |
finally |
| 2427 |
EndUpdate; |
| 2428 |
end; |
| 2429 |
end; |
| 2430 |
end; |
| 2431 |
//------------------------------------------------------------------------------ |
| 2432 |
|
| 2433 |
procedure TImage32.AssignPixelArray(const src: TArrayOfColor32; width: Integer; height: Integer); |
| 2434 |
var |
| 2435 |
wasResized: Boolean; |
| 2436 |
begin |
| 2437 |
width := Max(0, width); |
| 2438 |
height := Max(0, height); |
| 2439 |
if Length(src) <> width * height then |
| 2440 |
raise Exception.Create(rsInvalidImageArrayData); |
| 2441 |
|
| 2442 |
wasResized := (fWidth <> width) or (fHeight <> height); |
| 2443 |
|
| 2444 |
BeginUpdate; |
| 2445 |
try |
| 2446 |
fWidth := width; |
| 2447 |
fHeight := height; |
| 2448 |
fPixels := src; |
| 2449 |
finally |
| 2450 |
EndUpdate; |
| 2451 |
end; |
| 2452 |
|
| 2453 |
if wasResized then |
| 2454 |
Resized; |
| 2455 |
end; |
| 2456 |
//------------------------------------------------------------------------------ |
| 2457 |
|
| 2458 |
procedure TImage32.Changed; |
| 2459 |
begin |
| 2460 |
if fUpdateCnt <> 0 then Exit; |
| 2461 |
ResetColorCount; |
| 2462 |
if Assigned(fOnChange) then fOnChange(Self); |
| 2463 |
end; |
| 2464 |
//------------------------------------------------------------------------------ |
| 2465 |
|
| 2466 |
procedure TImage32.Resized; |
| 2467 |
begin |
| 2468 |
if fUpdateCnt <> 0 then Exit |
| 2469 |
else if Assigned(fOnResize) then fOnResize(Self) |
| 2470 |
else Changed; |
| 2471 |
end; |
| 2472 |
//------------------------------------------------------------------------------ |
| 2473 |
|
| 2474 |
function TImage32.SetPixels(const newPixels: TArrayOfColor32): Boolean; |
| 2475 |
var |
| 2476 |
len: integer; |
| 2477 |
begin |
| 2478 |
len := Length(newPixels); |
| 2479 |
Result := (len > 0)and (len = Width * height); |
| 2480 |
if Result then fPixels := System.Copy(newPixels, 0, len); |
| 2481 |
end; |
| 2482 |
//------------------------------------------------------------------------------ |
| 2483 |
|
| 2484 |
procedure TImage32.BeginUpdate; |
| 2485 |
begin |
| 2486 |
if fNotifyBlockCnt > 0 then Exit; |
| 2487 |
inc(fUpdateCnt); |
| 2488 |
end; |
| 2489 |
//------------------------------------------------------------------------------ |
| 2490 |
|
| 2491 |
procedure TImage32.EndUpdate; |
| 2492 |
begin |
| 2493 |
if fNotifyBlockCnt > 0 then Exit; |
| 2494 |
dec(fUpdateCnt); |
| 2495 |
if fUpdateCnt = 0 then Changed; |
| 2496 |
end; |
| 2497 |
//------------------------------------------------------------------------------ |
| 2498 |
|
| 2499 |
procedure TImage32.BlockNotify; |
| 2500 |
begin |
| 2501 |
inc(fNotifyBlockCnt); |
| 2502 |
inc(fUpdateCnt); |
| 2503 |
end; |
| 2504 |
//------------------------------------------------------------------------------ |
| 2505 |
|
| 2506 |
procedure TImage32.UnblockNotify; |
| 2507 |
begin |
| 2508 |
dec(fNotifyBlockCnt); |
| 2509 |
dec(fUpdateCnt); |
| 2510 |
end; |
| 2511 |
//------------------------------------------------------------------------------ |
| 2512 |
|
| 2513 |
procedure TImage32.SetBackgroundColor(bgColor: TColor32); |
| 2514 |
var |
| 2515 |
i: Integer; |
| 2516 |
pc: PColor32; |
| 2517 |
begin |
| 2518 |
pc := Pixelbase; |
| 2519 |
for i := 0 to high(fPixels) do |
| 2520 |
begin |
| 2521 |
pc^ := BlendToOpaque(bgColor, pc^); |
| 2522 |
inc(pc); |
| 2523 |
end; |
| 2524 |
Changed; |
| 2525 |
end; |
| 2526 |
//------------------------------------------------------------------------------ |
| 2527 |
|
| 2528 |
procedure TImage32.Clear(color: TColor32); |
| 2529 |
var |
| 2530 |
i: Integer; |
| 2531 |
pc: PColor32; |
| 2532 |
begin |
| 2533 |
fIsPremultiplied := false; |
| 2534 |
if IsEmpty then Exit; |
| 2535 |
if color = clNone32 then |
| 2536 |
FillChar(fPixels[0], Width * Height * SizeOf(TColor32), 0) |
| 2537 |
else |
| 2538 |
begin |
| 2539 |
pc := PixelBase; |
| 2540 |
for i := 0 to Width * Height -1 do |
| 2541 |
begin |
| 2542 |
pc^ := color; |
| 2543 |
inc(pc); |
| 2544 |
end; |
| 2545 |
end; |
| 2546 |
Changed; |
| 2547 |
end; |
| 2548 |
//------------------------------------------------------------------------------ |
| 2549 |
|
| 2550 |
procedure TImage32.Clear(const rec: TRect; color: TColor32 = 0); |
| 2551 |
begin |
| 2552 |
FillRect(rec, color); |
| 2553 |
end; |
| 2554 |
//------------------------------------------------------------------------------ |
| 2555 |
|
| 2556 |
procedure TImage32.FillRect(const rec: TRect; color: TColor32); |
| 2557 |
var |
| 2558 |
i,j, rw, w: Integer; |
| 2559 |
c: PColor32; |
| 2560 |
r: TRect; |
| 2561 |
begin |
| 2562 |
Types.IntersectRect(r, rec, bounds); |
| 2563 |
if IsEmptyRect(r) then Exit; |
| 2564 |
rw := RectWidth(r); |
| 2565 |
w := Width; |
| 2566 |
c := @Pixels[r.Top * w + r.Left]; |
| 2567 |
|
| 2568 |
if (color = 0) and (w = rw) then |
| 2569 |
FillChar(c^, (r.Bottom - r.Top) * rw * SizeOf(TColor32), 0) |
| 2570 |
else if rw = 1 then |
| 2571 |
begin |
| 2572 |
for i := r.Top to r.Bottom -1 do |
| 2573 |
begin |
| 2574 |
c^ := color; |
| 2575 |
inc(c, w); |
| 2576 |
end; |
| 2577 |
end |
| 2578 |
else if (color = 0) and (rw > 15) then |
| 2579 |
begin |
| 2580 |
for i := r.Top to r.Bottom -1 do |
| 2581 |
begin |
| 2582 |
FillChar(c^, rw * SizeOf(TColor32), 0); |
| 2583 |
inc(c, w); |
| 2584 |
end; |
| 2585 |
end |
| 2586 |
else |
| 2587 |
begin |
| 2588 |
for i := r.Top to r.Bottom -1 do |
| 2589 |
begin |
| 2590 |
for j := 1 to rw do |
| 2591 |
begin |
| 2592 |
c^ := color; |
| 2593 |
inc(c); |
| 2594 |
end; |
| 2595 |
inc(c, w - rw); |
| 2596 |
end; |
| 2597 |
end; |
| 2598 |
Changed; |
| 2599 |
end; |
| 2600 |
//------------------------------------------------------------------------------ |
| 2601 |
|
| 2602 |
procedure TImage32.ResetColorCount; |
| 2603 |
begin |
| 2604 |
fColorCount := 0; |
| 2605 |
end; |
| 2606 |
//------------------------------------------------------------------------------ |
| 2607 |
|
| 2608 |
{$RANGECHECKS OFF} // negative array index is used |
| 2609 |
|
| 2610 |
function TImage32.RectHasTransparency(const rec: TRect): Boolean; |
| 2611 |
var |
| 2612 |
i, j, rw: Integer; |
| 2613 |
lineByteOffset: nativeint; |
| 2614 |
c: PARGB; |
| 2615 |
r: TRect; |
| 2616 |
begin |
| 2617 |
Result := True; |
| 2618 |
Types.IntersectRect(r, rec, bounds); |
| 2619 |
if IsEmptyRect(r) then Exit; |
| 2620 |
rw := RectWidth(r); |
| 2621 |
c := @Pixels[r.Top * Width + r.Left]; |
| 2622 |
|
| 2623 |
if rw = Width then // we can use one loop |
| 2624 |
begin |
| 2625 |
i := (r.Bottom - r.Top) * rw; |
| 2626 |
inc(c, i); |
| 2627 |
i := -i; |
| 2628 |
while i < 0 do |
| 2629 |
begin |
| 2630 |
if PARGBArray(c)[i].A < 254 then Exit; |
| 2631 |
inc(i); |
| 2632 |
end; |
| 2633 |
end |
| 2634 |
else |
| 2635 |
begin |
| 2636 |
lineByteOffset := (Width - rw) * SizeOf(TColor32); |
| 2637 |
for i := r.Top to r.Bottom -1 do |
| 2638 |
begin |
| 2639 |
for j := 1 to rw do |
| 2640 |
begin |
| 2641 |
if c.A < 254 then Exit; |
| 2642 |
inc(c); |
| 2643 |
end; |
| 2644 |
inc(PByte(c), lineByteOffset); |
| 2645 |
end; |
| 2646 |
end; |
| 2647 |
Result := False; |
| 2648 |
end; |
| 2649 |
{$IFDEF RANGECHECKS_ENABLED} |
| 2650 |
{$RANGECHECKS ON} |
| 2651 |
{$ENDIF} |
| 2652 |
//------------------------------------------------------------------------------ |
| 2653 |
|
| 2654 |
procedure CheckBlendFill(pc: PColor32; color: TColor32); |
| 2655 |
{$IFDEF INLINE} inline; {$ENDIF} |
| 2656 |
begin |
| 2657 |
if not assigned(pc) then Exit; |
| 2658 |
pc^ := BlendToAlpha(pc^, color); |
| 2659 |
end; |
| 2660 |
//------------------------------------------------------------------------------ |
| 2661 |
|
| 2662 |
function TImage32.CopyPixels(const rec: TRect): TArrayOfColor32; |
| 2663 |
var |
| 2664 |
i, clipW, w,h: Integer; |
| 2665 |
pSrc, pDst, pDst2: PColor32; |
| 2666 |
recClipped: TRect; |
| 2667 |
begin |
| 2668 |
RectWidthHeight(rec, w,h); |
| 2669 |
NewColor32Array(result, w * h, True); |
| 2670 |
|
| 2671 |
if w * h = 0 then Exit; |
| 2672 |
Types.IntersectRect(recClipped, rec, Bounds); |
| 2673 |
//if recClipped is wholely outside the bounds of the image ... |
| 2674 |
if IsEmptyRect(recClipped) then |
| 2675 |
begin |
| 2676 |
//rec is considered valid even when completely outside the image bounds, |
| 2677 |
//and so when that happens we simply return a fully transparent image ... |
| 2678 |
FillChar(Result[0], w * h * SizeOf(TColor32), 0); |
| 2679 |
Exit; |
| 2680 |
end; |
| 2681 |
|
| 2682 |
//if recClipped is wholely within the bounds of the image ... |
| 2683 |
if RectsEqual(recClipped, rec) then |
| 2684 |
begin |
| 2685 |
pDst := @Result[0]; |
| 2686 |
pSrc := @fPixels[recClipped.Top * Width + rec.Left]; |
| 2687 |
for i := recClipped.Top to recClipped.Bottom -1 do |
| 2688 |
begin |
| 2689 |
Move(pSrc^, pDst^, w * SizeOf(TColor32)); |
| 2690 |
inc(pSrc, Width); inc(pDst, w); |
| 2691 |
end; |
| 2692 |
Exit; |
| 2693 |
end; |
| 2694 |
|
| 2695 |
//a part of 'rec' must be outside the bounds of the image ... |
| 2696 |
|
| 2697 |
pDst := @Result[0]; |
| 2698 |
for i := rec.Top to -1 do |
| 2699 |
begin |
| 2700 |
FillChar(pDst^, w * SizeOf(TColor32), 0); |
| 2701 |
inc(pDst, w); |
| 2702 |
end; |
| 2703 |
pSrc := @fPixels[recClipped.Top * Width + Max(0,rec.Left)]; |
| 2704 |
if (rec.Left < 0) or (rec.Right > Width) then |
| 2705 |
begin |
| 2706 |
clipW := RectWidth(recClipped); |
| 2707 |
pDst2 := IncPColor32(pDst, -Min(0, rec.Left)); |
| 2708 |
for i := recClipped.Top to recClipped.Bottom -1 do |
| 2709 |
begin |
| 2710 |
//when rec.left < 0 or rec.right > width it's simplest to |
| 2711 |
//start with a prefilled row of transparent pixels |
| 2712 |
FillChar(pDst^, w * SizeOf(TColor32), 0); |
| 2713 |
Move(pSrc^, pDst2^, clipW * SizeOf(TColor32)); |
| 2714 |
inc(pDst, w); inc(pDst2, w); inc(pSrc, Width); |
| 2715 |
end; |
| 2716 |
end else |
| 2717 |
begin |
| 2718 |
//things are simpler when there's no part of 'rec' is |
| 2719 |
//outside the image, at least not on the left or right sides ... |
| 2720 |
for i := recClipped.Top to recClipped.Bottom -1 do |
| 2721 |
begin |
| 2722 |
Move(pSrc^, pDst^, w * SizeOf(TColor32)); |
| 2723 |
inc(pSrc, Width); inc(pDst, w); |
| 2724 |
end; |
| 2725 |
end; |
| 2726 |
for i := Height to rec.Bottom -1 do |
| 2727 |
begin |
| 2728 |
FillChar(pDst^, w * SizeOf(TColor32), 0); |
| 2729 |
inc(pDst, w); |
| 2730 |
end; |
| 2731 |
end; |
| 2732 |
//------------------------------------------------------------------------------ |
| 2733 |
|
| 2734 |
procedure TImage32.Crop(const rec: TRect); |
| 2735 |
var |
| 2736 |
newPixels: TArrayOfColor32; |
| 2737 |
w,h: integer; |
| 2738 |
begin |
| 2739 |
RectWidthHeight(rec, w, h); |
| 2740 |
if (w = Width) and (h = Height) then Exit; |
| 2741 |
newPixels := CopyPixels(rec); // get pixels **before** resizing |
| 2742 |
BlockNotify; |
| 2743 |
try |
| 2744 |
SetSize(w, h); |
| 2745 |
if not IsEmptyRect(rec) then |
| 2746 |
fPixels := newPixels; |
| 2747 |
finally |
| 2748 |
UnblockNotify; |
| 2749 |
end; |
| 2750 |
Resized; |
| 2751 |
end; |
| 2752 |
//------------------------------------------------------------------------------ |
| 2753 |
|
| 2754 |
function TImage32.GetBounds: TRect; |
| 2755 |
begin |
| 2756 |
result := Types.Rect(0, 0, Width, Height); |
| 2757 |
end; |
| 2758 |
//------------------------------------------------------------------------------ |
| 2759 |
|
| 2760 |
function TImage32.GetMidPoint: TPointD; |
| 2761 |
begin |
| 2762 |
Result := PointD(fWidth * 0.5, fHeight * 0.5); |
| 2763 |
end; |
| 2764 |
//------------------------------------------------------------------------------ |
| 2765 |
|
| 2766 |
procedure TImage32.SetSize(newWidth, newHeight: Integer; color: TColor32); |
| 2767 |
begin |
| 2768 |
//very large images are usually due to a bug |
| 2769 |
if (newWidth > 20000) or (newHeight > 20000) then |
| 2770 |
raise Exception.Create(rsImageTooLarge); |
| 2771 |
fwidth := Max(0, newWidth); |
| 2772 |
fheight := Max(0, newHeight); |
| 2773 |
fPixels := nil; //forces a blank image |
| 2774 |
NewColor32Array(fPixels, fwidth * fheight, True); |
| 2775 |
fIsPremultiplied := false; |
| 2776 |
BlockNotify; |
| 2777 |
Clear(color); |
| 2778 |
UnblockNotify; |
| 2779 |
Resized; |
| 2780 |
end; |
| 2781 |
//------------------------------------------------------------------------------ |
| 2782 |
|
| 2783 |
procedure TImage32.Resize(newWidth, newHeight: Integer); |
| 2784 |
begin |
| 2785 |
ResizeTo(Self, newWidth, newHeight); |
| 2786 |
end; |
| 2787 |
//------------------------------------------------------------------------------ |
| 2788 |
|
| 2789 |
procedure TImage32.ResizeTo(targetImg: TImage32; newWidth, newHeight: Integer); |
| 2790 |
begin |
| 2791 |
if (newWidth <= 0) or (newHeight <= 0) then |
| 2792 |
begin |
| 2793 |
targetImg.SetSize(0, 0); |
| 2794 |
Exit; |
| 2795 |
end |
| 2796 |
else if (newWidth = fwidth) and (newHeight = fheight) then |
| 2797 |
begin |
| 2798 |
if targetImg <> Self then targetImg.Assign(Self); |
| 2799 |
Exit |
| 2800 |
end |
| 2801 |
else if IsEmpty then |
| 2802 |
begin |
| 2803 |
targetImg.SetSize(newWidth, newHeight); |
| 2804 |
Exit; |
| 2805 |
end; |
| 2806 |
|
| 2807 |
targetImg.BlockNotify; |
| 2808 |
try |
| 2809 |
if targetImg.fResampler <= rNearestResampler then |
| 2810 |
NearestNeighborResize(Self, targetImg, newWidth, newHeight) |
| 2811 |
else |
| 2812 |
ResamplerResize(Self, targetImg, newWidth, newHeight); |
| 2813 |
finally |
| 2814 |
targetImg.UnblockNotify; |
| 2815 |
end; |
| 2816 |
targetImg.Resized; |
| 2817 |
end; |
| 2818 |
//------------------------------------------------------------------------------ |
| 2819 |
|
| 2820 |
procedure TImage32.Scale(s: double); |
| 2821 |
begin |
| 2822 |
Scale(s, s); |
| 2823 |
end; |
| 2824 |
//------------------------------------------------------------------------------ |
| 2825 |
|
| 2826 |
procedure TImage32.ScaleTo(targetImg: TImage32; s: double); |
| 2827 |
begin |
| 2828 |
ScaleTo(targetImg, s, s); |
| 2829 |
end; |
| 2830 |
//------------------------------------------------------------------------------ |
| 2831 |
|
| 2832 |
procedure TImage32.Scale(sx, sy: double); |
| 2833 |
begin |
| 2834 |
if (sx > 0) and (sy > 0) then |
| 2835 |
Resize(Round(width * sx), Round(height * sy)); |
| 2836 |
end; |
| 2837 |
//------------------------------------------------------------------------------ |
| 2838 |
|
| 2839 |
procedure TImage32.ScaleTo(targetImg: TImage32; sx, sy: double); |
| 2840 |
begin |
| 2841 |
if (sx > 0) and (sy > 0) then |
| 2842 |
ResizeTo(targetImg, Round(width * sx), Round(height * sy)); |
| 2843 |
end; |
| 2844 |
//------------------------------------------------------------------------------ |
| 2845 |
|
| 2846 |
procedure TImage32.ScaleToFit(width, height: integer); |
| 2847 |
var |
| 2848 |
sx, sy: double; |
| 2849 |
begin |
| 2850 |
if IsEmpty or (width < 2) or (height < 2) then Exit; |
| 2851 |
sx := width / self.Width; |
| 2852 |
sy := height / self.Height; |
| 2853 |
if sx <= sy then |
| 2854 |
Scale(sx) else |
| 2855 |
Scale(sy); |
| 2856 |
end; |
| 2857 |
//------------------------------------------------------------------------------ |
| 2858 |
|
| 2859 |
procedure TImage32.ScaleToFitCentered(const rect: TRect); |
| 2860 |
begin |
| 2861 |
ScaleToFitCentered(RectWidth(rect), RectHeight(rect)); |
| 2862 |
end; |
| 2863 |
//------------------------------------------------------------------------------ |
| 2864 |
|
| 2865 |
procedure TImage32.ScaleToFitCentered(width, height: integer); |
| 2866 |
var |
| 2867 |
sx, sy: double; |
| 2868 |
tmp: TImage32; |
| 2869 |
rec2: TRect; |
| 2870 |
begin |
| 2871 |
if IsEmpty or (width <= 0) or (height <= 0) or |
| 2872 |
((width = self.Width) and (height = self.Height)) then Exit; |
| 2873 |
|
| 2874 |
sx := width / self.Width; |
| 2875 |
sy := height / self.Height; |
| 2876 |
BlockNotify; |
| 2877 |
try |
| 2878 |
if sx <= sy then |
| 2879 |
begin |
| 2880 |
Scale(sx); |
| 2881 |
if height = self.Height then Exit; |
| 2882 |
rec2 := Bounds; |
| 2883 |
TranslateRect(rec2, 0, (height - self.Height) div 2); |
| 2884 |
tmp := TImage32.Create(self); |
| 2885 |
try |
| 2886 |
SetSize(width, height); |
| 2887 |
CopyInternal(tmp, tmp.Bounds, rec2, nil); |
| 2888 |
finally |
| 2889 |
tmp.Free; |
| 2890 |
end; |
| 2891 |
end else |
| 2892 |
begin |
| 2893 |
Scale(sy); |
| 2894 |
if width = self.Width then Exit; |
| 2895 |
rec2 := Bounds; |
| 2896 |
TranslateRect(rec2, (width - self.Width) div 2, 0); |
| 2897 |
tmp := TImage32.Create(self); |
| 2898 |
try |
| 2899 |
SetSize(width, height); |
| 2900 |
CopyInternal(tmp, tmp.Bounds, rec2, nil); |
| 2901 |
finally |
| 2902 |
tmp.Free; |
| 2903 |
end; |
| 2904 |
end; |
| 2905 |
finally |
| 2906 |
UnblockNotify; |
| 2907 |
end; |
| 2908 |
Resized; |
| 2909 |
end; |
| 2910 |
//------------------------------------------------------------------------------ |
| 2911 |
|
| 2912 |
procedure TImage32.RotateLeft90; |
| 2913 |
var |
| 2914 |
x,y, xx: Integer; |
| 2915 |
src, dst: PColor32; |
| 2916 |
tmp: TImage32; |
| 2917 |
begin |
| 2918 |
if IsEmpty then Exit; |
| 2919 |
|
| 2920 |
BeginUpdate; |
| 2921 |
tmp := TImage32.create(Self); |
| 2922 |
try |
| 2923 |
SetSize(Height, Width); |
| 2924 |
xx := (width - 1) * Height; |
| 2925 |
dst := PixelBase; |
| 2926 |
for y := 0 to Height -1 do |
| 2927 |
begin |
| 2928 |
src := @tmp.Pixels[xx + y]; |
| 2929 |
for x := 0 to Width -1 do |
| 2930 |
begin |
| 2931 |
dst^ := src^; |
| 2932 |
inc(dst); dec(src, Height); |
| 2933 |
end; |
| 2934 |
end; |
| 2935 |
finally |
| 2936 |
tmp.Free; |
| 2937 |
EndUpdate; |
| 2938 |
end; |
| 2939 |
end; |
| 2940 |
//------------------------------------------------------------------------------ |
| 2941 |
|
| 2942 |
procedure TImage32.RotateRight90; |
| 2943 |
var |
| 2944 |
x,y: Integer; |
| 2945 |
src, dst: PColor32; |
| 2946 |
tmp: TImage32; |
| 2947 |
begin |
| 2948 |
if IsEmpty then Exit; |
| 2949 |
|
| 2950 |
BeginUpdate; |
| 2951 |
tmp := TImage32.create(Self); |
| 2952 |
try |
| 2953 |
SetSize(Height, Width); |
| 2954 |
dst := PixelBase; |
| 2955 |
for y := 0 to Height -1 do |
| 2956 |
begin |
| 2957 |
src := @tmp.Pixels[Height -1 - y]; |
| 2958 |
for x := 0 to Width -1 do |
| 2959 |
begin |
| 2960 |
dst^ := src^; |
| 2961 |
inc(dst); inc(src, Height); |
| 2962 |
end; |
| 2963 |
end; |
| 2964 |
finally |
| 2965 |
tmp.Free; |
| 2966 |
EndUpdate; |
| 2967 |
end; |
| 2968 |
end; |
| 2969 |
//------------------------------------------------------------------------------ |
| 2970 |
|
| 2971 |
procedure TImage32.Rotate180; |
| 2972 |
var |
| 2973 |
x,y: Integer; |
| 2974 |
src, dst: PColor32; |
| 2975 |
tmp: TImage32; |
| 2976 |
begin |
| 2977 |
if IsEmpty then Exit; |
| 2978 |
tmp := TImage32.create(Self); |
| 2979 |
try |
| 2980 |
dst := PixelBase; |
| 2981 |
src := @tmp.Pixels[Width * Height -1]; |
| 2982 |
for y := 0 to Height -1 do |
| 2983 |
begin |
| 2984 |
for x := 0 to Width -1 do |
| 2985 |
begin |
| 2986 |
dst^ := src^; |
| 2987 |
inc(dst); dec(src); |
| 2988 |
end; |
| 2989 |
end; |
| 2990 |
finally |
| 2991 |
tmp.Free; |
| 2992 |
end; |
| 2993 |
Changed; |
| 2994 |
end; |
| 2995 |
//------------------------------------------------------------------------------ |
| 2996 |
|
| 2997 |
function TImage32.GetColorCount: Integer; |
| 2998 |
var |
| 2999 |
allColors: PByteArray; |
| 3000 |
i: Integer; |
| 3001 |
c: PColor32; |
| 3002 |
const |
| 3003 |
cube256 = 256 * 256 * 256; |
| 3004 |
begin |
| 3005 |
result := 0; |
| 3006 |
if IsEmpty then Exit; |
| 3007 |
if fColorCount > 0 then |
| 3008 |
begin |
| 3009 |
result := fColorCount; |
| 3010 |
Exit; |
| 3011 |
end; |
| 3012 |
//because 'allColors' uses quite a chunk of memory, it's |
| 3013 |
//allocated on the heap rather than the stack |
| 3014 |
allColors := AllocMem(cube256); //nb: zero initialized |
| 3015 |
try |
| 3016 |
c := PixelBase; |
| 3017 |
for i := 0 to Width * Height -1 do |
| 3018 |
begin |
| 3019 |
//ignore colors with signifcant transparency |
| 3020 |
if GetAlpha(c^) > $80 then |
| 3021 |
allColors[c^ and $FFFFFF] := 1; |
| 3022 |
inc(c); |
| 3023 |
end; |
| 3024 |
for i := 0 to cube256 -1 do |
| 3025 |
if allColors[i] = 1 then inc(Result); |
| 3026 |
finally |
| 3027 |
FreeMem(allColors); |
| 3028 |
end; |
| 3029 |
fColorCount := Result; //avoids repeating the above unnecessarily |
| 3030 |
end; |
| 3031 |
//------------------------------------------------------------------------------ |
| 3032 |
|
| 3033 |
function TImage32.GetHasTransparency: Boolean; |
| 3034 |
var |
| 3035 |
i: Integer; |
| 3036 |
pc: PARGB; |
| 3037 |
begin |
| 3038 |
result := true; |
| 3039 |
If IsEmpty then Exit; |
| 3040 |
pc := PARGB(PixelBase); |
| 3041 |
for i := 0 to Width * Height -1 do |
| 3042 |
begin |
| 3043 |
if pc.A < 128 then Exit; |
| 3044 |
inc(pc); |
| 3045 |
end; |
| 3046 |
result := false; |
| 3047 |
end; |
| 3048 |
//------------------------------------------------------------------------------ |
| 3049 |
|
| 3050 |
function TImage32.SaveToFile(filename: string; compressionQuality: integer): Boolean; |
| 3051 |
var |
| 3052 |
fileFormatClass: TImageFormatClass; |
| 3053 |
begin |
| 3054 |
result := false; |
| 3055 |
if IsEmpty or (length(filename) < 5) then Exit; |
| 3056 |
//use the process's current working directory if no path supplied ... |
| 3057 |
if ExtractFilePath(filename) = '' then |
| 3058 |
filename := GetCurrentDir + '\'+ filename; |
| 3059 |
fileFormatClass := GetImageFormatClass(ExtractFileExt(filename)); |
| 3060 |
if assigned(fileFormatClass) then |
| 3061 |
with fileFormatClass.Create do |
| 3062 |
try |
| 3063 |
result := SaveToFile(filename, self, compressionQuality); |
| 3064 |
finally |
| 3065 |
free; |
| 3066 |
end; |
| 3067 |
end; |
| 3068 |
//------------------------------------------------------------------------------ |
| 3069 |
|
| 3070 |
function TImage32.SaveToStream(stream: TStream; |
| 3071 |
const FmtExt: string; compressionQuality: integer): Boolean; |
| 3072 |
var |
| 3073 |
fileFormatClass: TImageFormatClass; |
| 3074 |
begin |
| 3075 |
result := false; |
| 3076 |
fileFormatClass := GetImageFormatClass(FmtExt); |
| 3077 |
if assigned(fileFormatClass) then |
| 3078 |
with fileFormatClass.Create do |
| 3079 |
try |
| 3080 |
SaveToStream(stream, self, compressionQuality); |
| 3081 |
result := true; |
| 3082 |
finally |
| 3083 |
free; |
| 3084 |
end; |
| 3085 |
end; |
| 3086 |
//------------------------------------------------------------------------------ |
| 3087 |
|
| 3088 |
function TImage32.LoadFromFile(const filename: string): Boolean; |
| 3089 |
var |
| 3090 |
stream: TFileStream; |
| 3091 |
begin |
| 3092 |
Result := false; |
| 3093 |
if not FileExists(filename) then Exit; |
| 3094 |
|
| 3095 |
stream := TFileStream.Create(filename, fmOpenRead or fmShareDenyNone); |
| 3096 |
try |
| 3097 |
result := LoadFromStream(stream); |
| 3098 |
finally |
| 3099 |
stream.Free; |
| 3100 |
end; |
| 3101 |
end; |
| 3102 |
//------------------------------------------------------------------------------ |
| 3103 |
|
| 3104 |
function TImage32.LoadFromStream(stream: TStream; imgIdx: integer): Boolean; |
| 3105 |
var |
| 3106 |
ifc: TImageFormatClass; |
| 3107 |
begin |
| 3108 |
ifc := GetImageFormatClass(stream); |
| 3109 |
Result := Assigned(ifc); |
| 3110 |
if not Result then Exit; |
| 3111 |
|
| 3112 |
with ifc.Create do |
| 3113 |
try |
| 3114 |
result := LoadFromStream(stream, self, imgIdx); |
| 3115 |
finally |
| 3116 |
free; |
| 3117 |
end; |
| 3118 |
end; |
| 3119 |
//------------------------------------------------------------------------------ |
| 3120 |
|
| 3121 |
function TImage32.GetPixel(x, y: Integer): TColor32; |
| 3122 |
begin |
| 3123 |
if (x < 0) or (x >= Width) or (y < 0) or (y >= Height) then |
| 3124 |
result := clNone32 else |
| 3125 |
result := fPixels[y * width + x]; |
| 3126 |
end; |
| 3127 |
//------------------------------------------------------------------------------ |
| 3128 |
|
| 3129 |
procedure TImage32.SetPixel(x,y: Integer; color: TColor32); |
| 3130 |
begin |
| 3131 |
if (x < 0) or (x >= Width) or (y < 0) or (y >= Height) then Exit; |
| 3132 |
fPixels[y * width + x] := color; |
| 3133 |
//nb: no notify event here |
| 3134 |
end; |
| 3135 |
//------------------------------------------------------------------------------ |
| 3136 |
|
| 3137 |
function TImage32.GetIsBlank: Boolean; |
| 3138 |
var |
| 3139 |
i: integer; |
| 3140 |
pc: PARGB; |
| 3141 |
begin |
| 3142 |
result := IsEmpty; |
| 3143 |
if result then Exit; |
| 3144 |
pc := PARGB(PixelBase); |
| 3145 |
for i := 0 to width * height -1 do |
| 3146 |
begin |
| 3147 |
if pc.A > 0 then Exit; |
| 3148 |
inc(pc); |
| 3149 |
end; |
| 3150 |
result := true; |
| 3151 |
end; |
| 3152 |
//------------------------------------------------------------------------------ |
| 3153 |
|
| 3154 |
function TImage32.GetIsEmpty: Boolean; |
| 3155 |
begin |
| 3156 |
result := fPixels = nil; |
| 3157 |
end; |
| 3158 |
//------------------------------------------------------------------------------ |
| 3159 |
|
| 3160 |
function TImage32.GetPixelBase: PColor32; |
| 3161 |
begin |
| 3162 |
if IsEmpty then result := nil |
| 3163 |
else result := @fPixels[0]; |
| 3164 |
end; |
| 3165 |
//------------------------------------------------------------------------------ |
| 3166 |
|
| 3167 |
function TImage32.GetPixelRow(row: Integer): PColor32; |
| 3168 |
begin |
| 3169 |
if IsEmpty then result := nil |
| 3170 |
else result := @fPixels[row * Width]; |
| 3171 |
end; |
| 3172 |
//------------------------------------------------------------------------------ |
| 3173 |
|
| 3174 |
procedure TImage32.CopyInternal(src: TImage32; |
| 3175 |
const srcRec, dstRec: TRect; blendFunc: TBlendFunction); |
| 3176 |
var |
| 3177 |
i, j: integer; |
| 3178 |
srcRecWidth, srcRecHeight: nativeint; |
| 3179 |
srcWidth, dstWidth: nativeint; |
| 3180 |
s, d: PColor32; |
| 3181 |
begin |
| 3182 |
// occasionally, due to rounding, srcRec and dstRec |
| 3183 |
// don't have exactly the same widths and heights, so ... |
| 3184 |
srcRecWidth := |
| 3185 |
Min(srcRec.Right - srcRec.Left, dstRec.Right - dstRec.Left); |
| 3186 |
srcRecHeight := |
| 3187 |
Min(srcRec.Bottom - srcRec.Top, dstRec.Bottom - dstRec.Top); |
| 3188 |
|
| 3189 |
srcWidth := src.Width; |
| 3190 |
dstWidth := Width; |
| 3191 |
|
| 3192 |
s := @src.Pixels[srcRec.Top * srcWidth + srcRec.Left]; |
| 3193 |
d := @Pixels[dstRec.top * dstWidth + dstRec.Left]; |
| 3194 |
|
| 3195 |
if assigned(blendFunc) then |
| 3196 |
begin |
| 3197 |
srcWidth := (srcWidth - srcRecWidth) * SizeOf(TColor32); |
| 3198 |
dstWidth := (dstWidth - srcRecWidth) * SizeOf(TColor32); |
| 3199 |
for i := 1 to srcRecHeight do |
| 3200 |
begin |
| 3201 |
for j := 1 to srcRecWidth do |
| 3202 |
begin |
| 3203 |
d^ := blendFunc(d^, s^); |
| 3204 |
inc(s); inc(d); |
| 3205 |
end; |
| 3206 |
inc(PByte(s), srcWidth); // byte offset to the next s line |
| 3207 |
inc(PByte(d), dstWidth); // byte offset to the next d line |
| 3208 |
end; |
| 3209 |
end |
| 3210 |
//simply overwrite src with dst (ie without blending) |
| 3211 |
else if (srcRecWidth = dstWidth) and (srcWidth = dstWidth) then |
| 3212 |
move(s^, d^, srcRecWidth * srcRecHeight * SizeOf(TColor32)) |
| 3213 |
else |
| 3214 |
begin |
| 3215 |
srcWidth := srcWidth * SizeOf(TColor32); |
| 3216 |
dstWidth := dstWidth * SizeOf(TColor32); |
| 3217 |
srcRecWidth := srcRecWidth * SizeOf(TColor32); |
| 3218 |
for i := 1 to srcRecHeight do |
| 3219 |
begin |
| 3220 |
move(s^, d^, srcRecWidth); |
| 3221 |
inc(PByte(s), srcWidth); // srcWidth is in bytes |
| 3222 |
inc(PByte(d), dstWidth); // dstWidth is in bytes |
| 3223 |
end; |
| 3224 |
end; |
| 3225 |
end; |
| 3226 |
//------------------------------------------------------------------------------ |
| 3227 |
procedure TImage32.CopyInternalLine(src: TImage32; |
| 3228 |
const srcRec, dstRec: TRect; blendLineFunc: TBlendLineFunction); |
| 3229 |
var |
| 3230 |
i: integer; |
| 3231 |
srcRecWidth, srcRecHeight: nativeint; |
| 3232 |
srcWidth, dstWidth: nativeint; |
| 3233 |
s, d: PColor32; |
| 3234 |
begin |
| 3235 |
if not Assigned(blendLineFunc) then |
| 3236 |
begin |
| 3237 |
CopyInternal(src, srcRec, dstRec, nil); |
| 3238 |
Exit; |
| 3239 |
end; |
| 3240 |
|
| 3241 |
// occasionally, due to rounding, srcRec and dstRec |
| 3242 |
// don't have exactly the same widths and heights, so ... |
| 3243 |
srcRecWidth := |
| 3244 |
Min(srcRec.Right - srcRec.Left, dstRec.Right - dstRec.Left); |
| 3245 |
srcRecHeight := |
| 3246 |
Min(srcRec.Bottom - srcRec.Top, dstRec.Bottom - dstRec.Top); |
| 3247 |
|
| 3248 |
srcWidth := src.Width; |
| 3249 |
dstWidth := Width; |
| 3250 |
|
| 3251 |
s := @src.Pixels[srcRec.Top * srcWidth + srcRec.Left]; |
| 3252 |
d := @Pixels[dstRec.top * dstWidth + dstRec.Left]; |
| 3253 |
|
| 3254 |
if (srcRecWidth = dstWidth) and (srcWidth = dstWidth) then |
| 3255 |
blendLineFunc(d, s, srcRecWidth * srcRecHeight) |
| 3256 |
else |
| 3257 |
begin |
| 3258 |
srcWidth := srcWidth * SizeOf(TColor32); |
| 3259 |
dstWidth := dstWidth * SizeOf(TColor32); |
| 3260 |
for i := 1 to srcRecHeight do |
| 3261 |
begin |
| 3262 |
blendLineFunc(d, s, srcRecWidth); |
| 3263 |
inc(PByte(s), srcWidth); // srcWidth is in bytes |
| 3264 |
inc(PByte(d), dstWidth); // dstWidth is in bytes |
| 3265 |
end; |
| 3266 |
end; |
| 3267 |
end; |
| 3268 |
//------------------------------------------------------------------------------ |
| 3269 |
|
| 3270 |
function TImage32.Copy(src: TImage32; srcRec, dstRec: TRect): Boolean; |
| 3271 |
begin |
| 3272 |
Result := CopyBlendInternal(src, srcRec, dstRec, nil, nil); |
| 3273 |
end; |
| 3274 |
//------------------------------------------------------------------------------ |
| 3275 |
|
| 3276 |
function TImage32.CopyBlend(src: TImage32; const srcRec, dstRec: TRect; |
| 3277 |
blendFunc: TBlendFunction): Boolean; |
| 3278 |
begin |
| 3279 |
Result := CopyBlendInternal(src, srcRec, dstRec, blendFunc, nil); |
| 3280 |
end; |
| 3281 |
//------------------------------------------------------------------------------ |
| 3282 |
|
| 3283 |
function TImage32.CopyBlend(src: TImage32; const srcRec, dstRec: TRect; |
| 3284 |
blendLineFunc: TBlendLineFunction): Boolean; |
| 3285 |
begin |
| 3286 |
Result := CopyBlendInternal(src, srcRec, dstRec, nil, blendLineFunc); |
| 3287 |
end; |
| 3288 |
//------------------------------------------------------------------------------ |
| 3289 |
|
| 3290 |
function TImage32.CopyBlendInternal(src: TImage32; const srcRec: TRect; dstRec: TRect; |
| 3291 |
blendFunc: TBlendFunction; blendLineFunc: TBlendLineFunction): Boolean; |
| 3292 |
var |
| 3293 |
tmp: TImage32; |
| 3294 |
srcRecClipped, dstRecClipped, r: TRect; |
| 3295 |
scaleX, scaleY: double; |
| 3296 |
w,h, dstW,dstH, srcW,srcH: integer; |
| 3297 |
begin |
| 3298 |
result := false; |
| 3299 |
if IsEmptyRect(srcRec) or IsEmptyRect(dstRec) then Exit; |
| 3300 |
Types.IntersectRect(srcRecClipped, srcRec, src.Bounds); |
| 3301 |
|
| 3302 |
//get the scaling amount (if any) before |
| 3303 |
//dstRec might be adjusted due to clipping ... |
| 3304 |
RectWidthHeight(dstRec, dstW, dstH); |
| 3305 |
RectWidthHeight(srcRec, srcW, srcH); |
| 3306 |
|
| 3307 |
//watching out for insignificant scaling |
| 3308 |
if Abs(dstW - srcW) < 2 then |
| 3309 |
scaleX := 1 else |
| 3310 |
scaleX := dstW / srcW; |
| 3311 |
if Abs(dstH - srcH) < 2 then |
| 3312 |
scaleY := 1 else |
| 3313 |
scaleY := dstH / srcH; |
| 3314 |
|
| 3315 |
//check if the source rec has been clipped ... |
| 3316 |
if not RectsEqual(srcRecClipped, srcRec) then |
| 3317 |
begin |
| 3318 |
if IsEmptyRect(srcRecClipped) then Exit; |
| 3319 |
//the source has been clipped so clip the destination too ... |
| 3320 |
RectWidthHeight(srcRecClipped, w, h); |
| 3321 |
RectWidthHeight(srcRec, srcW, srcH); |
| 3322 |
ScaleRect(dstRec, w / srcW, h / srcH); |
| 3323 |
TranslateRect(dstRec, |
| 3324 |
srcRecClipped.Left - srcRec.Left, |
| 3325 |
srcRecClipped.Top - srcRec.Top); |
| 3326 |
end; |
| 3327 |
|
| 3328 |
if (scaleX <> 1.0) or (scaleY <> 1.0) then |
| 3329 |
begin |
| 3330 |
//scale source (tmp) to the destination then call CopyBlend() again ...^ |
| 3331 |
tmp := TImage32.Create; |
| 3332 |
try |
| 3333 |
tmp.AssignSettings(src); |
| 3334 |
src.ScaleTo(tmp, scaleX, scaleY); |
| 3335 |
ScaleRect(srcRecClipped, scaleX, scaleY); |
| 3336 |
result := CopyBlendInternal(tmp, srcRecClipped, dstRec, blendFunc, blendLineFunc); |
| 3337 |
finally |
| 3338 |
tmp.Free; |
| 3339 |
end; |
| 3340 |
Exit; |
| 3341 |
end; |
| 3342 |
|
| 3343 |
Types.IntersectRect(dstRecClipped, dstRec, Bounds); |
| 3344 |
if IsEmptyRect(dstRecClipped) then Exit; |
| 3345 |
|
| 3346 |
//there's no scaling if we get here, but further clipping may be needed if |
| 3347 |
//the destination rec is partially outside the destination image's bounds |
| 3348 |
|
| 3349 |
if not RectsEqual(dstRecClipped, dstRec) then |
| 3350 |
begin |
| 3351 |
//the destination rec has been clipped so clip the source too ... |
| 3352 |
RectWidthHeight(dstRecClipped, w, h); |
| 3353 |
RectWidthHeight(dstRec, dstW, dstH); |
| 3354 |
ScaleRect(srcRecClipped, w / dstW, h / dstH); |
| 3355 |
TranslateRect(srcRecClipped, |
| 3356 |
dstRecClipped.Left - dstRec.Left, |
| 3357 |
dstRecClipped.Top - dstRec.Top); |
| 3358 |
end; |
| 3359 |
|
| 3360 |
//when copying to self and srcRec & dstRec overlap then |
| 3361 |
//copy srcRec to a temporary image and use it as the source ... |
| 3362 |
if (src = self) and Types.IntersectRect(r, srcRecClipped, dstRecClipped) then |
| 3363 |
begin |
| 3364 |
tmp := TImage32.Create(self, srcRecClipped); |
| 3365 |
try |
| 3366 |
result := src.CopyBlendInternal(tmp, tmp.Bounds, dstRecClipped, blendFunc, blendLineFunc); |
| 3367 |
finally |
| 3368 |
tmp.Free; |
| 3369 |
end; |
| 3370 |
Exit; |
| 3371 |
end; |
| 3372 |
|
| 3373 |
if Assigned(blendLineFunc) then |
| 3374 |
CopyInternalLine(src, srcRecClipped, dstRecClipped, blendLineFunc) |
| 3375 |
else |
| 3376 |
CopyInternal(src, srcRecClipped, dstRecClipped, blendFunc); |
| 3377 |
result := true; |
| 3378 |
Changed; |
| 3379 |
end; |
| 3380 |
//------------------------------------------------------------------------------ |
| 3381 |
|
| 3382 |
function TImage32.LoadFromResource(const resName: string; resType: PChar): Boolean; |
| 3383 |
var |
| 3384 |
resStream: TResourceStream; |
| 3385 |
begin |
| 3386 |
resStream := CreateResourceStream(resName, resType); |
| 3387 |
try |
| 3388 |
Result := assigned(resStream) and |
| 3389 |
LoadFromStream(resStream); |
| 3390 |
finally |
| 3391 |
resStream.Free; |
| 3392 |
end; |
| 3393 |
end; |
| 3394 |
//------------------------------------------------------------------------------ |
| 3395 |
|
| 3396 |
{$IF DEFINED (MSWINDOWS)} |
| 3397 |
procedure TImage32.CopyFromDC(srcDc: HDC; const srcRect: TRect); |
| 3398 |
var |
| 3399 |
bi: TBitmapInfoHeader; |
| 3400 |
bm, oldBm: HBitmap; |
| 3401 |
dc, memDc: HDC; |
| 3402 |
pixels: Pointer; |
| 3403 |
w,h: integer; |
| 3404 |
begin |
| 3405 |
BeginUpdate; |
| 3406 |
try |
| 3407 |
RectWidthHeight(srcRect, w,h); |
| 3408 |
SetSize(w, h); |
| 3409 |
bi := Get32bitBitmapInfoHeader(w, -h); // -h => avoids need to flip image |
| 3410 |
dc := GetDC(0); |
| 3411 |
memDc := CreateCompatibleDC(dc); |
| 3412 |
try |
| 3413 |
bm := CreateDIBSection(dc, |
| 3414 |
PBITMAPINFO(@bi)^, DIB_RGB_COLORS, pixels, 0, 0); |
| 3415 |
if bm = 0 then Exit; |
| 3416 |
try |
| 3417 |
oldBm := SelectObject(memDc, bm); |
| 3418 |
BitBlt(memDc, 0, 0, w, h, srcDc, srcRect.Left,srcRect.Top, SRCCOPY); |
| 3419 |
Move(pixels^, fPixels[0], w * h * sizeOf(TColor32)); |
| 3420 |
SelectObject(memDc, oldBm); |
| 3421 |
finally |
| 3422 |
DeleteObject(bm); |
| 3423 |
end; |
| 3424 |
finally |
| 3425 |
DeleteDc(memDc); |
| 3426 |
ReleaseDc(0, dc); |
| 3427 |
end; |
| 3428 |
if IsBlank then SetAlpha(255); |
| 3429 |
//FlipVertical; |
| 3430 |
finally |
| 3431 |
EndUpdate; |
| 3432 |
end; |
| 3433 |
end; |
| 3434 |
//------------------------------------------------------------------------------ |
| 3435 |
|
| 3436 |
procedure TImage32.CopyToDc(dstDc: HDC; x,y: Integer; transparent: Boolean); |
| 3437 |
begin |
| 3438 |
CopyToDc(Bounds, Types.Rect(x,y, x+Width, y+Height), |
| 3439 |
dstDc, transparent); |
| 3440 |
end; |
| 3441 |
//------------------------------------------------------------------------------ |
| 3442 |
|
| 3443 |
procedure TImage32.CopyToDc(const srcRect: TRect; dstDc: HDC; |
| 3444 |
x: Integer = 0; y: Integer = 0; transparent: Boolean = true); |
| 3445 |
var |
| 3446 |
recW, recH: integer; |
| 3447 |
begin |
| 3448 |
RectWidthHeight(srcRect, recW, recH); |
| 3449 |
CopyToDc(srcRect, Types.Rect(x,y, x+recW, y+recH), dstDc, transparent); |
| 3450 |
end; |
| 3451 |
//------------------------------------------------------------------------------ |
| 3452 |
|
| 3453 |
procedure TImage32.CopyToDc(const srcRect, dstRect: TRect; |
| 3454 |
dstDc: HDC; transparent: Boolean = true); |
| 3455 |
var |
| 3456 |
i, x,y, wSrc ,hSrc, wDest, hDest, wBytes: integer; |
| 3457 |
rec: TRect; |
| 3458 |
bi: TBitmapInfoHeader; |
| 3459 |
bm, oldBm: HBitmap; |
| 3460 |
dibBits: Pointer; |
| 3461 |
pDst, pSrc: PARGB; |
| 3462 |
memDc: HDC; |
| 3463 |
isTransparent: Boolean; |
| 3464 |
bf: BLENDFUNCTION; |
| 3465 |
oldStretchBltMode: integer; |
| 3466 |
begin |
| 3467 |
Types.IntersectRect(rec, srcRect, Bounds); |
| 3468 |
if IsEmpty or IsEmptyRect(rec) or IsEmptyRect(dstRect) then Exit; |
| 3469 |
RectWidthHeight(rec, wSrc, hSrc); |
| 3470 |
RectWidthHeight(dstRect, wDest, hDest); |
| 3471 |
x := dstRect.Left; |
| 3472 |
y := dstRect.Top; |
| 3473 |
inc(x, rec.Left - srcRect.Left); |
| 3474 |
inc(y, rec.Top - srcRect.Top); |
| 3475 |
|
| 3476 |
bi := Get32bitBitmapInfoHeader(wSrc, hSrc); |
| 3477 |
|
| 3478 |
isTransparent := transparent and RectHasTransparency(srcRect); |
| 3479 |
memDc := CreateCompatibleDC(dstDc); |
| 3480 |
try |
| 3481 |
bm := CreateDIBSection(memDc, PBITMAPINFO(@bi)^, |
| 3482 |
DIB_RGB_COLORS, dibBits, 0, 0); |
| 3483 |
if bm = 0 then Exit; |
| 3484 |
|
| 3485 |
try |
| 3486 |
//copy Image to dibBits (with vertical flip) |
| 3487 |
wBytes := wSrc * SizeOf(TColor32); |
| 3488 |
pDst := dibBits; |
| 3489 |
pSrc := PARGB(PixelRow[rec.Bottom -1]); |
| 3490 |
inc(pSrc, rec.Left); |
| 3491 |
if isTransparent and not IsPremultiplied then |
| 3492 |
begin |
| 3493 |
//premultiplied alphas are required when alpha blending |
| 3494 |
for i := rec.Bottom -1 downto rec.Top do |
| 3495 |
begin |
| 3496 |
PremultiplyAlpha(pSrc, pDst, wSrc); |
| 3497 |
dec(pSrc, Width); |
| 3498 |
inc(pDst, wSrc); |
| 3499 |
end; |
| 3500 |
end |
| 3501 |
else |
| 3502 |
begin |
| 3503 |
for i := rec.Bottom -1 downto rec.Top do |
| 3504 |
begin |
| 3505 |
Move(pSrc^, pDst^, wBytes); |
| 3506 |
dec(pSrc, Width); |
| 3507 |
inc(pDst, wSrc); |
| 3508 |
end; |
| 3509 |
end; |
| 3510 |
|
| 3511 |
oldBm := SelectObject(memDC, bm); |
| 3512 |
if isTransparent then |
| 3513 |
begin |
| 3514 |
//premultiplied alphas are required when alpha blending |
| 3515 |
bf.BlendOp := AC_SRC_OVER; |
| 3516 |
bf.BlendFlags := 0; |
| 3517 |
bf.SourceConstantAlpha := 255; |
| 3518 |
bf.AlphaFormat := AC_SRC_ALPHA; |
| 3519 |
AlphaBlend(dstDc, x,y, wDest,hDest, memDC, 0,0, wSrc,hSrc, bf); |
| 3520 |
end |
| 3521 |
else if (wDest = wSrc) and (hDest = hSrc) then |
| 3522 |
begin |
| 3523 |
BitBlt(dstDc, x,y, wSrc, hSrc, memDc, 0,0, SRCCOPY) |
| 3524 |
end else |
| 3525 |
begin |
| 3526 |
oldStretchBltMode := SetStretchBltMode(dstDc, COLORONCOLOR); |
| 3527 |
StretchBlt(dstDc, x,y, wDest, hDest, memDc, 0,0, wSrc,hSrc, SRCCOPY); |
| 3528 |
if oldStretchBltMode <> COLORONCOLOR then // restore mode |
| 3529 |
SetStretchBltMode(dstDc, oldStretchBltMode); |
| 3530 |
end; |
| 3531 |
SelectObject(memDC, oldBm); |
| 3532 |
finally |
| 3533 |
DeleteObject(bm); |
| 3534 |
end; |
| 3535 |
finally |
| 3536 |
DeleteDc(memDc); |
| 3537 |
end; |
| 3538 |
end; |
| 3539 |
{$IFEND} |
| 3540 |
//------------------------------------------------------------------------------ |
| 3541 |
|
| 3542 |
{$IF DEFINED(USING_VCL_LCL)} |
| 3543 |
procedure TImage32.CopyFromBitmap(bmp: TBitmap); |
| 3544 |
var |
| 3545 |
ms: TMemoryStream; |
| 3546 |
bmpFormat: TImageFormat_BMP; |
| 3547 |
begin |
| 3548 |
ms := TMemoryStream.create; |
| 3549 |
bmpFormat := TImageFormat_BMP.Create; |
| 3550 |
try |
| 3551 |
bmp.SaveToStream(ms); |
| 3552 |
ms.Position := 0; |
| 3553 |
bmpFormat.LoadFromStream(ms, self); |
| 3554 |
finally |
| 3555 |
ms.Free; |
| 3556 |
bmpFormat.Free; |
| 3557 |
end; |
| 3558 |
end; |
| 3559 |
//------------------------------------------------------------------------------ |
| 3560 |
|
| 3561 |
procedure TImage32.CopyToBitmap(bmp: TBitmap); |
| 3562 |
var |
| 3563 |
ms: TMemoryStream; |
| 3564 |
bmpFormat: TImageFormat_BMP; |
| 3565 |
begin |
| 3566 |
ms := TMemoryStream.create; |
| 3567 |
bmpFormat := TImageFormat_BMP.Create; |
| 3568 |
try |
| 3569 |
bmpFormat.IncludeFileHeaderInSaveStream := true; |
| 3570 |
bmpFormat.SaveToStream(ms, self); |
| 3571 |
ms.Position := 0; |
| 3572 |
bmp.PixelFormat := pf32bit; |
| 3573 |
{$IF DEFINED(USING_VCL) AND DEFINED(ALPHAFORMAT)} |
| 3574 |
bmp.AlphaFormat := afDefined; |
| 3575 |
{$IFEND} |
| 3576 |
bmp.LoadFromStream(ms); |
| 3577 |
finally |
| 3578 |
ms.Free; |
| 3579 |
bmpFormat.Free; |
| 3580 |
end; |
| 3581 |
end; |
| 3582 |
//------------------------------------------------------------------------------ |
| 3583 |
{$IFEND} |
| 3584 |
|
| 3585 |
function TImage32.CopyToClipBoard: Boolean; |
| 3586 |
var |
| 3587 |
i: Integer; |
| 3588 |
formatClass: TImageFormatClass; |
| 3589 |
begin |
| 3590 |
//Sadly with CF_DIB (and even CF_DIBV5) clipboard formats, transparency is |
| 3591 |
//usually lost, so we'll copy all available formats including CF_PNG, that |
| 3592 |
//is if it's registered. |
| 3593 |
result := not IsEmpty; |
| 3594 |
if not result then Exit; |
| 3595 |
result := false; |
| 3596 |
|
| 3597 |
for i := ImageFormatClassList.Count -1 downto 0 do |
| 3598 |
begin |
| 3599 |
formatClass := PImgFmtRec(ImageFormatClassList[i]).Obj; |
| 3600 |
if not formatClass.CanCopyToClipboard then Continue; |
| 3601 |
with formatClass.Create do |
| 3602 |
try |
| 3603 |
result := CopyToClipboard(self); |
| 3604 |
finally |
| 3605 |
free; |
| 3606 |
end; |
| 3607 |
end; |
| 3608 |
end; |
| 3609 |
//------------------------------------------------------------------------------ |
| 3610 |
|
| 3611 |
class function TImage32.CanPasteFromClipBoard: Boolean; |
| 3612 |
var |
| 3613 |
i: Integer; |
| 3614 |
formatClass: TImageFormatClass; |
| 3615 |
begin |
| 3616 |
result := false; |
| 3617 |
for i := ImageFormatClassList.Count -1 downto 0 do |
| 3618 |
begin |
| 3619 |
formatClass := PImgFmtRec(ImageFormatClassList[i]).Obj; |
| 3620 |
if formatClass.CanPasteFromClipboard then |
| 3621 |
begin |
| 3622 |
result := true; |
| 3623 |
Exit; |
| 3624 |
end; |
| 3625 |
end; |
| 3626 |
end; |
| 3627 |
//------------------------------------------------------------------------------ |
| 3628 |
|
| 3629 |
function TImage32.PasteFromClipBoard: Boolean; |
| 3630 |
var |
| 3631 |
i: Integer; |
| 3632 |
formatClass: TImageFormatClass; |
| 3633 |
begin |
| 3634 |
result := false; |
| 3635 |
for i := ImageFormatClassList.Count -1 downto 0 do |
| 3636 |
begin |
| 3637 |
formatClass := PImgFmtRec(ImageFormatClassList[i]).Obj; |
| 3638 |
if not formatClass.CanPasteFromClipboard then Continue; |
| 3639 |
|
| 3640 |
with formatClass.Create do |
| 3641 |
try |
| 3642 |
result := PasteFromClipboard(self); |
| 3643 |
if not Result then Continue; |
| 3644 |
finally |
| 3645 |
free; |
| 3646 |
end; |
| 3647 |
Changed; |
| 3648 |
Break; |
| 3649 |
end; |
| 3650 |
end; |
| 3651 |
//------------------------------------------------------------------------------ |
| 3652 |
|
| 3653 |
procedure TImage32.ConvertToBoolMask(reference: TColor32; tolerance: integer; |
| 3654 |
colorFunc: TCompareFunction; maskBg: TColor32; maskFg: TColor32); |
| 3655 |
var |
| 3656 |
i: Integer; |
| 3657 |
mask: TArrayOfByte; |
| 3658 |
c: PColor32; |
| 3659 |
b: PByte; |
| 3660 |
begin |
| 3661 |
if IsEmpty then Exit; |
| 3662 |
mask := GetBoolMask(self, reference, colorFunc, tolerance); |
| 3663 |
c := PixelBase; |
| 3664 |
b := @mask[0]; |
| 3665 |
for i := 0 to Width * Height -1 do |
| 3666 |
begin |
| 3667 |
{$IFDEF PBYTE} |
| 3668 |
if b^ = 0 then c^ := maskBg else c^ := maskFg; |
| 3669 |
{$ELSE} |
| 3670 |
if b^ = #0 then c^ := maskBg else c^ := maskFg; |
| 3671 |
{$ENDIF} |
| 3672 |
inc(c); inc(b); |
| 3673 |
end; |
| 3674 |
Changed; |
| 3675 |
end; |
| 3676 |
//------------------------------------------------------------------------------ |
| 3677 |
|
| 3678 |
procedure TImage32.ConvertToAlphaMask(reference: TColor32; |
| 3679 |
colorFunc: TCompareFunctionEx); |
| 3680 |
var |
| 3681 |
i: Integer; |
| 3682 |
mask: TArrayOfByte; |
| 3683 |
c: PColor32; |
| 3684 |
b: PByte; |
| 3685 |
begin |
| 3686 |
if IsEmpty then Exit; |
| 3687 |
mask := GetByteMask(self, reference, colorFunc); |
| 3688 |
c := PixelBase; |
| 3689 |
b := @mask[0]; |
| 3690 |
for i := 0 to Width * Height -1 do |
| 3691 |
begin |
| 3692 |
{$IFDEF PBYTE} |
| 3693 |
c^ := b^ shl 24; |
| 3694 |
{$ELSE} |
| 3695 |
c^ := Ord(b^) shl 24; |
| 3696 |
{$ENDIF} |
| 3697 |
inc(c); inc(b); |
| 3698 |
end; |
| 3699 |
Changed; |
| 3700 |
end; |
| 3701 |
//------------------------------------------------------------------------------ |
| 3702 |
|
| 3703 |
procedure TImage32.FlipVertical; |
| 3704 |
var |
| 3705 |
i: Integer; |
| 3706 |
a: TArrayOfColor32; |
| 3707 |
src, dst: PColor32; |
| 3708 |
begin |
| 3709 |
if IsEmpty then Exit; |
| 3710 |
NewColor32Array(a, fWidth * fHeight, True); |
| 3711 |
src := @fPixels[(height-1) * width]; |
| 3712 |
dst := @a[0]; |
| 3713 |
for i := 0 to fHeight -1 do |
| 3714 |
begin |
| 3715 |
move(src^, dst^, fWidth * SizeOf(TColor32)); |
| 3716 |
dec(src, fWidth); inc(dst, fWidth); |
| 3717 |
end; |
| 3718 |
fPixels := a; |
| 3719 |
Changed; |
| 3720 |
end; |
| 3721 |
//------------------------------------------------------------------------------ |
| 3722 |
|
| 3723 |
procedure TImage32.FlipHorizontal; |
| 3724 |
var |
| 3725 |
i,j, widthLess1: Integer; |
| 3726 |
a: TArrayOfColor32; |
| 3727 |
row: PColor32; |
| 3728 |
begin |
| 3729 |
if IsEmpty then Exit; |
| 3730 |
NewColor32Array(a, fWidth, True); |
| 3731 |
widthLess1 := fWidth -1; |
| 3732 |
row := @fPixels[(height-1) * width]; //top row |
| 3733 |
for i := 0 to fHeight -1 do |
| 3734 |
begin |
| 3735 |
move(row^, a[0], fWidth * SizeOf(TColor32)); |
| 3736 |
for j := 0 to widthLess1 do |
| 3737 |
begin |
| 3738 |
row^ := a[widthLess1 - j]; |
| 3739 |
inc(row); |
| 3740 |
end; |
| 3741 |
dec(row, fWidth *2); |
| 3742 |
end; |
| 3743 |
Changed; |
| 3744 |
end; |
| 3745 |
//------------------------------------------------------------------------------ |
| 3746 |
|
| 3747 |
procedure TImage32.PreMultiply; |
| 3748 |
begin |
| 3749 |
if IsEmpty or fIsPremultiplied then Exit; |
| 3750 |
fIsPremultiplied := true; |
| 3751 |
PremultiplyAlpha(PARGB(PixelBase), PARGB(PixelBase), Width * Height); |
| 3752 |
//nb: no OnChange notify event here |
| 3753 |
end; |
| 3754 |
//------------------------------------------------------------------------------ |
| 3755 |
|
| 3756 |
procedure TImage32.SetRGB(rgbColor: TColor32); |
| 3757 |
var |
| 3758 |
i: Integer; |
| 3759 |
pc: PColor32; |
| 3760 |
c: TColor32; |
| 3761 |
begin |
| 3762 |
//this method leaves the alpha channel untouched |
| 3763 |
if IsEmpty then Exit; |
| 3764 |
|
| 3765 |
pc := PixelBase; |
| 3766 |
rgbColor := rgbColor and $00FFFFFF; |
| 3767 |
for i := 0 to Width * Height - 1 do |
| 3768 |
begin |
| 3769 |
c := pc^; |
| 3770 |
if c and $FF000000 = 0 then |
| 3771 |
pc^ := 0 else |
| 3772 |
pc^ := c and $FF000000 or rgbColor; |
| 3773 |
inc(pc); |
| 3774 |
end; |
| 3775 |
Changed; |
| 3776 |
end; |
| 3777 |
//------------------------------------------------------------------------------ |
| 3778 |
|
| 3779 |
procedure TImage32.SetRGB(rgbColor: TColor32; rec: TRect); |
| 3780 |
var |
| 3781 |
i,j, dx: Integer; |
| 3782 |
pc: PColor32; |
| 3783 |
begin |
| 3784 |
Types.IntersectRect(rec, rec, bounds); |
| 3785 |
if IsEmptyRect(rec) then Exit; |
| 3786 |
rgbColor := rgbColor and $00FFFFFF; |
| 3787 |
pc := PixelBase; |
| 3788 |
inc(pc, rec.Left); |
| 3789 |
dx := Width - RectWidth(rec); |
| 3790 |
for i := rec.Top to rec.Bottom -1 do |
| 3791 |
begin |
| 3792 |
for j := rec.Left to rec.Right -1 do |
| 3793 |
begin |
| 3794 |
pc^ := pc^ and $FF000000 or rgbColor; |
| 3795 |
inc(pc); |
| 3796 |
end; |
| 3797 |
inc(pc, dx); |
| 3798 |
end; |
| 3799 |
Changed; |
| 3800 |
end; |
| 3801 |
//------------------------------------------------------------------------------ |
| 3802 |
|
| 3803 |
procedure TImage32.SetAlpha(alpha: Byte); |
| 3804 |
var |
| 3805 |
i: Integer; |
| 3806 |
c: PARGB; |
| 3807 |
begin |
| 3808 |
//this method only changes the alpha channel |
| 3809 |
if IsEmpty then Exit; |
| 3810 |
c := PARGB(PixelBase); |
| 3811 |
for i := 0 to Width * Height -1 do |
| 3812 |
begin |
| 3813 |
c.A := alpha; |
| 3814 |
inc(c); |
| 3815 |
end; |
| 3816 |
Changed; |
| 3817 |
end; |
| 3818 |
//------------------------------------------------------------------------------ |
| 3819 |
|
| 3820 |
procedure TImage32.ReduceOpacity(opacity: Byte); |
| 3821 |
var |
| 3822 |
i: Integer; |
| 3823 |
c: PARGB; |
| 3824 |
a: Byte; |
| 3825 |
begin |
| 3826 |
if opacity = 255 then Exit; |
| 3827 |
c := PARGB(PixelBase); |
| 3828 |
for i := 0 to Width * Height -1 do |
| 3829 |
begin |
| 3830 |
a := c.A; |
| 3831 |
if a <> 0 then |
| 3832 |
c.A := MulTable[a, opacity]; |
| 3833 |
inc(c); |
| 3834 |
end; |
| 3835 |
Changed; |
| 3836 |
end; |
| 3837 |
//------------------------------------------------------------------------------ |
| 3838 |
|
| 3839 |
procedure TImage32.ReduceOpacity(opacity: Byte; rec: TRect); |
| 3840 |
var |
| 3841 |
i,j, rw: Integer; |
| 3842 |
c: PARGB; |
| 3843 |
a: Byte; |
| 3844 |
lineOffsetInBytes: integer; |
| 3845 |
begin |
| 3846 |
Types.IntersectRect(rec, rec, bounds); |
| 3847 |
if IsEmptyRect(rec) then Exit; |
| 3848 |
rw := RectWidth(rec); |
| 3849 |
c := @Pixels[rec.Top * Width + rec.Left]; |
| 3850 |
lineOffsetInBytes := (Width - rw) * SizeOf(TARGB); |
| 3851 |
for i := rec.Top to rec.Bottom - 1 do |
| 3852 |
begin |
| 3853 |
for j := 1 to rw do |
| 3854 |
begin |
| 3855 |
a := c.A; |
| 3856 |
if a <> 0 then |
| 3857 |
c.A := MulTable[a, opacity]; |
| 3858 |
inc(c); |
| 3859 |
end; |
| 3860 |
inc(PByte(c), lineOffsetInBytes); |
| 3861 |
end; |
| 3862 |
Changed; |
| 3863 |
end; |
| 3864 |
//------------------------------------------------------------------------------ |
| 3865 |
|
| 3866 |
procedure TImage32.Grayscale(mode: TGrayscaleMode; |
| 3867 |
linearAmountPercentage: double); |
| 3868 |
var |
| 3869 |
i: SizeInt; |
| 3870 |
cLinear: double; |
| 3871 |
c, lastC, grayC: TColor32; |
| 3872 |
p: PColor32Array; |
| 3873 |
amountCalc: Boolean; |
| 3874 |
oneMinusAmount: double; |
| 3875 |
begin |
| 3876 |
if mode = gsmSaturation then |
| 3877 |
begin |
| 3878 |
// linearAmountPercentage has no effect here |
| 3879 |
AdjustSaturation(-100); |
| 3880 |
Exit; |
| 3881 |
end; |
| 3882 |
|
| 3883 |
// Colorimetric (perceptual luminance-preserving) conversion to grayscale |
| 3884 |
// See https://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale |
| 3885 |
if IsEmpty then Exit; |
| 3886 |
|
| 3887 |
if linearAmountPercentage <= 0.0 then Exit; |
| 3888 |
amountCalc := linearAmountPercentage < 1.0; |
| 3889 |
oneMinusAmount := 1.0 - linearAmountPercentage; |
| 3890 |
|
| 3891 |
p := PColor32Array(PixelBase); |
| 3892 |
lastC := 0; |
| 3893 |
grayC := 0; |
| 3894 |
for i := 0 to high(fPixels) do |
| 3895 |
begin |
| 3896 |
c := p[i] and $00FFFFFF; |
| 3897 |
if c <> 0 then |
| 3898 |
begin |
| 3899 |
if c <> lastC then // only do the calculation if the color channels changed |
| 3900 |
begin |
| 3901 |
lastC := c; |
| 3902 |
{$IF DEFINED(ANDROID)} |
| 3903 |
c := SwapRedBlue(c); |
| 3904 |
{$IFEND} |
| 3905 |
|
| 3906 |
// We don't divide by 255 here, so can skip some division and multiplications. |
| 3907 |
// That means cLinear is actually "cLinear * 255" |
| 3908 |
cLinear := (0.2126 * Byte(c shr 16)) + (0.7152 * Byte(c shr 8)) + (0.0722 * Byte(c)); |
| 3909 |
//cLinear := (0.2126 * TARGB(c).R) + (0.7152 * TARGB(c).G) + (0.0722 * TARGB(c).B); |
| 3910 |
|
| 3911 |
if mode = gsmLinear then |
| 3912 |
c := ClampByte(cLinear) |
| 3913 |
else //if mode = gsmColorimetric then |
| 3914 |
begin |
| 3915 |
if cLinear <= (0.0031308 * 255) then // adjust for cLinear being "cLiniear * 255" |
| 3916 |
c := ClampByte(Integer(Round(12.92 * cLinear))) |
| 3917 |
else // for Power we must divide by 255 and then later multipy by 255 |
| 3918 |
//c := ClampByte(Integer(Round((1.055 * 255) * Power(cLinear / 255, 1/2.4) - (0.055 * 255)))); |
| 3919 |
end; |
| 3920 |
|
| 3921 |
|
| 3922 |
if not amountCalc then |
| 3923 |
grayC := (c shl 16) or (c shl 8) or c |
| 3924 |
else |
| 3925 |
begin |
| 3926 |
cLinear := c * linearAmountPercentage; |
| 3927 |
grayC := ClampByte(Integer(Round(Byte(lastC shr 16) * oneMinusAmount + cLinear))) shl 16 or |
| 3928 |
ClampByte(Integer(Round(Byte(lastC shr 8) * oneMinusAmount + cLinear))) shl 8 or |
| 3929 |
ClampByte(Integer(Round(Byte(lastC ) * oneMinusAmount + cLinear))); |
| 3930 |
end; |
| 3931 |
|
| 3932 |
{$IF DEFINED(ANDROID)} |
| 3933 |
grayC := SwapRedBlue(grayC); |
| 3934 |
{$IFEND} |
| 3935 |
end; |
| 3936 |
p[i] := (p[i] and $FF000000) or grayC; |
| 3937 |
end; |
| 3938 |
end; |
| 3939 |
|
| 3940 |
Changed; |
| 3941 |
end; |
| 3942 |
//------------------------------------------------------------------------------ |
| 3943 |
|
| 3944 |
procedure TImage32.InvertColors; |
| 3945 |
var |
| 3946 |
pc: PColor32Array; |
| 3947 |
i: SizeInt; |
| 3948 |
begin |
| 3949 |
pc := PColor32Array(PixelBase); |
| 3950 |
for i := 0 to Width * Height -1 do |
| 3951 |
pc[i] := pc[i] xor $00FFFFFF; // keep the alpha channel untouched |
| 3952 |
Changed; |
| 3953 |
end; |
| 3954 |
//------------------------------------------------------------------------------ |
| 3955 |
|
| 3956 |
procedure TImage32.InvertAlphas; |
| 3957 |
var |
| 3958 |
pc: PColor32Array; |
| 3959 |
i: SizeInt; |
| 3960 |
begin |
| 3961 |
pc := PColor32Array(PixelBase); |
| 3962 |
for i := 0 to Width * Height -1 do |
| 3963 |
pc[i] := pc[i] xor $FF000000; // keep the color channels untouched |
| 3964 |
Changed; |
| 3965 |
end; |
| 3966 |
//------------------------------------------------------------------------------ |
| 3967 |
|
| 3968 |
procedure TImage32.AdjustHue(percent: Integer); |
| 3969 |
var |
| 3970 |
i: SizeInt; |
| 3971 |
hsl: THsl; |
| 3972 |
lut: array [byte] of byte; |
| 3973 |
c, lastC, newC: TColor32; |
| 3974 |
p: PColor32Array; |
| 3975 |
begin |
| 3976 |
percent := percent mod 100; |
| 3977 |
if percent < 0 then inc(percent, 100); |
| 3978 |
percent := Round(percent * 255 / 100); |
| 3979 |
if (percent = 0) or IsEmpty then Exit; |
| 3980 |
for i := 0 to 255 do lut[i] := (i + percent) mod 255; |
| 3981 |
|
| 3982 |
lastC := 0; |
| 3983 |
newC := 0; |
| 3984 |
p := PColor32Array(fPixels); |
| 3985 |
for i := 0 to high(fPixels) do |
| 3986 |
begin |
| 3987 |
c := p[i]; |
| 3988 |
c := c and $00FFFFFF; |
| 3989 |
if c <> 0 then |
| 3990 |
begin |
| 3991 |
if c <> lastC then // only do the calculation if the color channels changed |
| 3992 |
begin |
| 3993 |
lastC := C; |
| 3994 |
hsl := RgbToHsl(c); |
| 3995 |
hsl.hue := lut[hsl.hue]; |
| 3996 |
newC := HslToRgb(hsl); |
| 3997 |
end; |
| 3998 |
p[i] := (p[i] and $FF000000) or newC; // keep the alpha channel |
| 3999 |
end; |
| 4000 |
end; |
| 4001 |
|
| 4002 |
Changed; |
| 4003 |
end; |
| 4004 |
//------------------------------------------------------------------------------ |
| 4005 |
|
| 4006 |
procedure TImage32.AdjustLuminance(percent: Integer); |
| 4007 |
var |
| 4008 |
i: SizeInt; |
| 4009 |
hsl: THsl; |
| 4010 |
pc: double; |
| 4011 |
lut: array [byte] of byte; |
| 4012 |
c, lastC, newC: TColor32; |
| 4013 |
p: PColor32Array; |
| 4014 |
begin |
| 4015 |
if (percent = 0) or IsEmpty then Exit; |
| 4016 |
percent := percent mod 101; |
| 4017 |
pc := percent / 100; |
| 4018 |
if pc > 0 then |
| 4019 |
for i := 0 to 255 do lut[i] := Round(i + (255 - i) * pc) |
| 4020 |
else |
| 4021 |
for i := 0 to 255 do lut[i] := Round(i + (i * pc)); |
| 4022 |
|
| 4023 |
lastC := 0; |
| 4024 |
newC := 0; |
| 4025 |
p := PColor32Array(fPixels); |
| 4026 |
for i := 0 to high(fPixels) do |
| 4027 |
begin |
| 4028 |
c := p[i]; |
| 4029 |
c := c and $00FFFFFF; |
| 4030 |
if c <> 0 then |
| 4031 |
begin |
| 4032 |
if c <> lastC then // only do the calculation if the color channels changed |
| 4033 |
begin |
| 4034 |
lastC := C; |
| 4035 |
hsl := RgbToHsl(c); |
| 4036 |
hsl.lum := lut[hsl.lum]; |
| 4037 |
newC := HslToRgb(hsl); |
| 4038 |
end; |
| 4039 |
p[i] := (p[i] and $FF000000) or newC; // keep the alpha channel |
| 4040 |
end; |
| 4041 |
end; |
| 4042 |
|
| 4043 |
Changed; |
| 4044 |
end; |
| 4045 |
//------------------------------------------------------------------------------ |
| 4046 |
|
| 4047 |
procedure TImage32.AdjustSaturation(percent: Integer); |
| 4048 |
var |
| 4049 |
i: SizeInt; |
| 4050 |
hsl: THsl; |
| 4051 |
lut: array [byte] of byte; |
| 4052 |
pc: double; |
| 4053 |
c, lastC, newC: TColor32; |
| 4054 |
p: PColor32Array; |
| 4055 |
begin |
| 4056 |
if (percent = 0) or IsEmpty then Exit; |
| 4057 |
percent := percent mod 101; |
| 4058 |
pc := percent / 100; |
| 4059 |
if pc > 0 then |
| 4060 |
for i := 0 to 255 do lut[i] := Round(i + (255 - i) * pc) |
| 4061 |
else |
| 4062 |
for i := 0 to 255 do lut[i] := Round(i + (i * pc)); |
| 4063 |
|
| 4064 |
lastC := 0; |
| 4065 |
newC := 0; |
| 4066 |
p := PColor32Array(fPixels); |
| 4067 |
for i := 0 to high(fPixels) do |
| 4068 |
begin |
| 4069 |
c := p[i]; |
| 4070 |
c := c and $00FFFFFF; |
| 4071 |
if c <> 0 then |
| 4072 |
begin |
| 4073 |
if c <> lastC then // only do the calculation if the color channels changed |
| 4074 |
begin |
| 4075 |
lastC := C; |
| 4076 |
hsl := RgbToHsl(c); |
| 4077 |
hsl.sat := lut[hsl.sat]; |
| 4078 |
newC := HslToRgb(hsl); |
| 4079 |
end; |
| 4080 |
p[i] := (p[i] and $FF000000) or newC; // keep the alpha channel |
| 4081 |
end; |
| 4082 |
end; |
| 4083 |
|
| 4084 |
Changed; |
| 4085 |
end; |
| 4086 |
//------------------------------------------------------------------------------ |
| 4087 |
|
| 4088 |
function TImage32.GetOpaqueBounds: TRect; |
| 4089 |
var |
| 4090 |
x,y, x1,x2,y1,y2: Integer; |
| 4091 |
found: Boolean; |
| 4092 |
begin |
| 4093 |
y1 := 0; y2 := 0; |
| 4094 |
found := false; |
| 4095 |
Result := NullRect; |
| 4096 |
for y := 0 to Height -1 do |
| 4097 |
begin |
| 4098 |
for x := 0 to Width -1 do |
| 4099 |
if TARGB(fPixels[y * Width + x]).A > 0 then |
| 4100 |
begin |
| 4101 |
y1 := y; |
| 4102 |
found := true; |
| 4103 |
break; |
| 4104 |
end; |
| 4105 |
if found then break; |
| 4106 |
end; |
| 4107 |
|
| 4108 |
if not found then |
| 4109 |
Exit; |
| 4110 |
|
| 4111 |
found := false; |
| 4112 |
for y := Height -1 downto 0 do |
| 4113 |
begin |
| 4114 |
for x := 0 to Width -1 do |
| 4115 |
if TARGB(fPixels[y * Width + x]).A > 0 then |
| 4116 |
begin |
| 4117 |
y2 := y; |
| 4118 |
found := true; |
| 4119 |
break; |
| 4120 |
end; |
| 4121 |
if found then break; |
| 4122 |
end; |
| 4123 |
|
| 4124 |
x1 := Width; x2 := 0; |
| 4125 |
for y := y1 to y2 do |
| 4126 |
for x := 0 to Width -1 do |
| 4127 |
if TARGB(fPixels[y * Width + x]).A > 0 then |
| 4128 |
begin |
| 4129 |
if x < x1 then x1 := x; |
| 4130 |
if x > x2 then x2 := x; |
| 4131 |
end; |
| 4132 |
|
| 4133 |
Result := Types.Rect(x1, y1, x2+1, y2+1); |
| 4134 |
end; |
| 4135 |
//------------------------------------------------------------------------------ |
| 4136 |
|
| 4137 |
function TImage32.CropTransparentPixels: TRect; |
| 4138 |
begin |
| 4139 |
Result := GetOpaqueBounds; |
| 4140 |
if IsEmptyRect(Result) then |
| 4141 |
SetSize(0,0) else |
| 4142 |
Crop(Result); |
| 4143 |
end; |
| 4144 |
//------------------------------------------------------------------------------ |
| 4145 |
|
| 4146 |
procedure TImage32.Rotate(angleRads: double); |
| 4147 |
var |
| 4148 |
mat: TMatrixD; |
| 4149 |
begin |
| 4150 |
|
| 4151 |
{$IFDEF CLOCKWISE_ROTATION_WITH_NEGATIVE_ANGLES} |
| 4152 |
angleRads := -angleRads; |
| 4153 |
{$ENDIF} |
| 4154 |
|
| 4155 |
//nb: There's no point rotating about a specific point |
| 4156 |
//since the rotated image will be recentered. |
| 4157 |
|
| 4158 |
NormalizeAngle(angleRads); |
| 4159 |
if IsEmpty or (angleRads = 0) then Exit; |
| 4160 |
|
| 4161 |
if angleRads = angle180 then |
| 4162 |
begin |
| 4163 |
Rotate180; //because we've excluded 0 & 360 deg angles |
| 4164 |
end |
| 4165 |
else if angleRads = angle90 then |
| 4166 |
begin |
| 4167 |
RotateRight90; |
| 4168 |
end |
| 4169 |
else if angleRads = -angle90 then |
| 4170 |
begin |
| 4171 |
RotateLeft90; |
| 4172 |
end else |
| 4173 |
begin |
| 4174 |
mat := IdentityMatrix; |
| 4175 |
// the rotation point isn't important |
| 4176 |
// because AffineTransformImage() will |
| 4177 |
// will resize and recenter the image |
| 4178 |
MatrixRotate(mat, NullPointD, angleRads); |
| 4179 |
AffineTransformImage(self, mat); |
| 4180 |
end; |
| 4181 |
end; |
| 4182 |
//------------------------------------------------------------------------------ |
| 4183 |
|
| 4184 |
procedure TImage32.RotateRect(const rec: TRect; |
| 4185 |
angleRads: double; eraseColor: TColor32 = 0); |
| 4186 |
var |
| 4187 |
tmp: TImage32; |
| 4188 |
rec2: TRect; |
| 4189 |
recWidth, recHeight: integer; |
| 4190 |
begin |
| 4191 |
recWidth := rec.Right - rec.Left; |
| 4192 |
recHeight := rec.Bottom - rec.Top; |
| 4193 |
//create a tmp image with a copy of the pixels inside rec ... |
| 4194 |
tmp := TImage32.Create(self, rec); |
| 4195 |
try |
| 4196 |
tmp.Rotate(angleRads); |
| 4197 |
//since rotating also resizes, get a centered |
| 4198 |
//(clipped) rect of the rotated pixels ... |
| 4199 |
rec2.Left := (tmp.Width - recWidth) div 2; |
| 4200 |
rec2.Top := (tmp.Height - recHeight) div 2; |
| 4201 |
rec2.Right := rec2.Left + recWidth; |
| 4202 |
rec2.Bottom := rec2.Top + recHeight; |
| 4203 |
//finally move the rotated rec back to the image ... |
| 4204 |
FillRect(rec, eraseColor); |
| 4205 |
CopyBlend(tmp, rec2, rec); |
| 4206 |
finally |
| 4207 |
tmp.Free; |
| 4208 |
end; |
| 4209 |
end; |
| 4210 |
//------------------------------------------------------------------------------ |
| 4211 |
|
| 4212 |
procedure TImage32.Skew(dx,dy: double); |
| 4213 |
var |
| 4214 |
mat: TMatrixD; |
| 4215 |
begin |
| 4216 |
if IsEmpty or ((dx = 0) and (dy = 0)) then Exit; |
| 4217 |
//limit skewing to twice the image's width and/or height |
| 4218 |
dx := ClampRange(dx, -2.0, 2.0); |
| 4219 |
dy := ClampRange(dy, -2.0, 2.0); |
| 4220 |
mat := IdentityMatrix; |
| 4221 |
MatrixSkew(mat, dx, dy); |
| 4222 |
AffineTransformImage(self, mat); |
| 4223 |
end; |
| 4224 |
//------------------------------------------------------------------------------ |
| 4225 |
|
| 4226 |
procedure TImage32.ScaleAlpha(scale: double); |
| 4227 |
var |
| 4228 |
i: Integer; |
| 4229 |
pb: PARGB; |
| 4230 |
begin |
| 4231 |
pb := PARGB(PixelBase); |
| 4232 |
for i := 0 to Width * Height - 1 do |
| 4233 |
begin |
| 4234 |
pb.A := ClampByte(Integer(Round(pb.A * scale))); |
| 4235 |
inc(pb); |
| 4236 |
end; |
| 4237 |
Changed; |
| 4238 |
end; |
| 4239 |
|
| 4240 |
//------------------------------------------------------------------------------ |
| 4241 |
// TImageList32 |
| 4242 |
//------------------------------------------------------------------------------ |
| 4243 |
|
| 4244 |
constructor TImageList32.Create; |
| 4245 |
begin |
| 4246 |
{$IFDEF XPLAT_GENERICS} |
| 4247 |
fList := TList<TImage32>.Create; |
| 4248 |
{$ELSE} |
| 4249 |
fList := TList.Create; |
| 4250 |
{$ENDIF} |
| 4251 |
fIsImageOwner := true; |
| 4252 |
end; |
| 4253 |
//------------------------------------------------------------------------------ |
| 4254 |
|
| 4255 |
destructor TImageList32.Destroy; |
| 4256 |
begin |
| 4257 |
Clear; |
| 4258 |
fList.Free; |
| 4259 |
inherited; |
| 4260 |
end; |
| 4261 |
//------------------------------------------------------------------------------ |
| 4262 |
|
| 4263 |
function TImageList32.Count: integer; |
| 4264 |
begin |
| 4265 |
result := fList.Count; |
| 4266 |
end; |
| 4267 |
//------------------------------------------------------------------------------ |
| 4268 |
|
| 4269 |
procedure TImageList32.Clear; |
| 4270 |
var |
| 4271 |
i: integer; |
| 4272 |
begin |
| 4273 |
if IsImageOwner then |
| 4274 |
for i := 0 to fList.Count -1 do |
| 4275 |
TImage32(fList[i]).Free; |
| 4276 |
fList.Clear; |
| 4277 |
end; |
| 4278 |
//------------------------------------------------------------------------------ |
| 4279 |
|
| 4280 |
function TImageList32.GetImage(index: integer): TImage32; |
| 4281 |
begin |
| 4282 |
result := TImage32(fList[index]); |
| 4283 |
end; |
| 4284 |
//------------------------------------------------------------------------------ |
| 4285 |
|
| 4286 |
procedure TImageList32.SetImage(index: integer; img: TIMage32); |
| 4287 |
begin |
| 4288 |
if fIsImageOwner then TImage32(fList[index]).Free; |
| 4289 |
fList[index] := img; |
| 4290 |
end; |
| 4291 |
//------------------------------------------------------------------------------ |
| 4292 |
|
| 4293 |
function TImageList32.GetLast: TImage32; |
| 4294 |
begin |
| 4295 |
if Count = 0 then Result := nil |
| 4296 |
else Result := TImage32(fList[Count -1]); |
| 4297 |
end; |
| 4298 |
//------------------------------------------------------------------------------ |
| 4299 |
|
| 4300 |
procedure TImageList32.Add(image: TImage32); |
| 4301 |
begin |
| 4302 |
fList.Add(image); |
| 4303 |
end; |
| 4304 |
//------------------------------------------------------------------------------ |
| 4305 |
|
| 4306 |
function TImageList32.Add(width, height: integer): TImage32; |
| 4307 |
begin |
| 4308 |
Result := TImage32.create(width, height); |
| 4309 |
fList.Add(Result); |
| 4310 |
end; |
| 4311 |
//------------------------------------------------------------------------------ |
| 4312 |
|
| 4313 |
procedure TImageList32.Insert(index: integer; image: TImage32); |
| 4314 |
begin |
| 4315 |
fList.Insert(index, image); |
| 4316 |
end; |
| 4317 |
//------------------------------------------------------------------------------ |
| 4318 |
|
| 4319 |
procedure TImageList32.Move(currentIndex, newIndex: integer); |
| 4320 |
begin |
| 4321 |
fList.Move(currentIndex, newIndex); |
| 4322 |
end; |
| 4323 |
//------------------------------------------------------------------------------ |
| 4324 |
|
| 4325 |
procedure TImageList32.Delete(index: integer); |
| 4326 |
begin |
| 4327 |
if fIsImageOwner then TImage32(fList[index]).Free; |
| 4328 |
fList.Delete(index); |
| 4329 |
end; |
| 4330 |
|
| 4331 |
//------------------------------------------------------------------------------ |
| 4332 |
// TImageFormat methods |
| 4333 |
//------------------------------------------------------------------------------ |
| 4334 |
|
| 4335 |
function TImageFormat.LoadFromFile(const filename: string; |
| 4336 |
img32: TImage32): Boolean; |
| 4337 |
var |
| 4338 |
fs: TFileStream; |
| 4339 |
begin |
| 4340 |
result := FileExists(filename); |
| 4341 |
if not result then Exit; |
| 4342 |
fs := TFileStream.Create(filename, fmOpenRead or fmShareDenyWrite); |
| 4343 |
try |
| 4344 |
Result := LoadFromStream(fs, img32); |
| 4345 |
finally |
| 4346 |
fs.Free; |
| 4347 |
end; |
| 4348 |
end; |
| 4349 |
//------------------------------------------------------------------------------ |
| 4350 |
|
| 4351 |
function TImageFormat.SaveToFile(const filename: string; |
| 4352 |
img32: TImage32; quality: integer): Boolean; |
| 4353 |
var |
| 4354 |
fs: TFileStream; |
| 4355 |
begin |
| 4356 |
result := (pos('.', filename) = 1) or |
| 4357 |
DirectoryExists(ExtractFilePath(filename)); |
| 4358 |
if not result then Exit; |
| 4359 |
|
| 4360 |
fs := TFileStream.Create(filename, fmCreate); |
| 4361 |
try |
| 4362 |
SaveToStream(fs, img32, quality); |
| 4363 |
finally |
| 4364 |
fs.Free; |
| 4365 |
end; |
| 4366 |
end; |
| 4367 |
//------------------------------------------------------------------------------ |
| 4368 |
|
| 4369 |
class function TImageFormat.CanCopyToClipboard: Boolean; |
| 4370 |
begin |
| 4371 |
Result := false; |
| 4372 |
end; |
| 4373 |
//------------------------------------------------------------------------------ |
| 4374 |
|
| 4375 |
class function TImageFormat.GetImageCount(stream: TStream): integer; |
| 4376 |
begin |
| 4377 |
Result := 1; |
| 4378 |
end; |
| 4379 |
|
| 4380 |
//------------------------------------------------------------------------------ |
| 4381 |
// TInterfacedObj |
| 4382 |
//------------------------------------------------------------------------------ |
| 4383 |
|
| 4384 |
{$IFDEF FPC} |
| 4385 |
function TInterfacedObj._AddRef: Integer; |
| 4386 |
{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; |
| 4387 |
begin |
| 4388 |
Result := -1; |
| 4389 |
end; |
| 4390 |
//------------------------------------------------------------------------------ |
| 4391 |
|
| 4392 |
function TInterfacedObj._Release: Integer; |
| 4393 |
{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF}; |
| 4394 |
begin |
| 4395 |
Result := -1; |
| 4396 |
end; |
| 4397 |
//------------------------------------------------------------------------------ |
| 4398 |
|
| 4399 |
function TInterfacedObj.QueryInterface( |
| 4400 |
{$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid; |
| 4401 |
out obj) : longint; |
| 4402 |
begin |
| 4403 |
if GetInterface(IID, Obj) then Result := 0 |
| 4404 |
else Result := E_NOINTERFACE; |
| 4405 |
end; |
| 4406 |
|
| 4407 |
{$ELSE} |
| 4408 |
|
| 4409 |
function TInterfacedObj._AddRef: Integer; stdcall; |
| 4410 |
begin |
| 4411 |
Result := -1; |
| 4412 |
end; |
| 4413 |
//------------------------------------------------------------------------------ |
| 4414 |
|
| 4415 |
function TInterfacedObj._Release: Integer; stdcall; |
| 4416 |
begin |
| 4417 |
Result := -1; |
| 4418 |
end; |
| 4419 |
//------------------------------------------------------------------------------ |
| 4420 |
|
| 4421 |
function TInterfacedObj.QueryInterface(const IID: TGUID; |
| 4422 |
out Obj): HResult; |
| 4423 |
begin |
| 4424 |
if GetInterface(IID, Obj) then Result := 0 |
| 4425 |
else Result := E_NOINTERFACE; |
| 4426 |
end; |
| 4427 |
{$ENDIF} |
| 4428 |
|
| 4429 |
//------------------------------------------------------------------------------ |
| 4430 |
// Initialization and Finalization functions |
| 4431 |
//------------------------------------------------------------------------------ |
| 4432 |
|
| 4433 |
procedure MakeBlendTables; |
| 4434 |
var |
| 4435 |
i,j: Integer; |
| 4436 |
begin |
| 4437 |
for j := 0 to 255 do MulTable[0, j] := 0; |
| 4438 |
for i := 0 to 255 do MulTable[i, 0] := 0; |
| 4439 |
for j := 0 to 255 do DivTable[0, j] := 0; |
| 4440 |
for i := 0 to 255 do DivTable[i, 0] := 0; |
| 4441 |
for i := 1 to 255 do |
| 4442 |
begin |
| 4443 |
for j := 1 to 255 do |
| 4444 |
begin |
| 4445 |
MulTable[i, j] := Round(i * j * div255); |
| 4446 |
if i >= j then |
| 4447 |
DivTable[i, j] := 255 else |
| 4448 |
DivTable[i, j] := Round(i * $FF / j); |
| 4449 |
end; |
| 4450 |
end; |
| 4451 |
|
| 4452 |
Sigmoid[128] := 128; |
| 4453 |
for i := 1 to 127 do |
| 4454 |
Sigmoid[128+i] := 128 + Round(127 * sin(angle90 * i/127)); |
| 4455 |
for i := 0 to 127 do |
| 4456 |
Sigmoid[i] := 255- Sigmoid[255-i]; |
| 4457 |
end; |
| 4458 |
//------------------------------------------------------------------------------ |
| 4459 |
|
| 4460 |
{$IFDEF MSWINDOWS} |
| 4461 |
procedure GetScreenScale; |
| 4462 |
var |
| 4463 |
dc: HDC; |
| 4464 |
ScreenPixelsY: integer; |
| 4465 |
begin |
| 4466 |
dc := GetDC(0); |
| 4467 |
try |
| 4468 |
ScreenPixelsY := GetDeviceCaps(dc, LOGPIXELSY); |
| 4469 |
DpiAwareOne := ScreenPixelsY / 96; |
| 4470 |
finally |
| 4471 |
ReleaseDC(0, dc); |
| 4472 |
end; |
| 4473 |
dpiAware1 := Round(DpiAwareOne); |
| 4474 |
end; |
| 4475 |
{$ENDIF} |
| 4476 |
//------------------------------------------------------------------------------ |
| 4477 |
|
| 4478 |
procedure CleanUpImageFormatClassList; |
| 4479 |
var |
| 4480 |
i: integer; |
| 4481 |
begin |
| 4482 |
for i := ImageFormatClassList.Count -1 downto 0 do |
| 4483 |
Dispose(PImgFmtRec(ImageFormatClassList[i])); |
| 4484 |
ImageFormatClassList.Free; |
| 4485 |
end; |
| 4486 |
|
| 4487 |
//------------------------------------------------------------------------------ |
| 4488 |
//------------------------------------------------------------------------------ |
| 4489 |
|
| 4490 |
procedure CreateResamplerList; |
| 4491 |
begin |
| 4492 |
{$IFDEF XPLAT_GENERICS} |
| 4493 |
ResamplerList := TList<TResamplerObj>.Create; |
| 4494 |
{$ELSE} |
| 4495 |
ResamplerList := TList.Create; |
| 4496 |
{$ENDIF} |
| 4497 |
end; |
| 4498 |
//------------------------------------------------------------------------------ |
| 4499 |
|
| 4500 |
function GetResampler(id: integer): TResamplerFunction; |
| 4501 |
var |
| 4502 |
i: integer; |
| 4503 |
begin |
| 4504 |
result := nil; |
| 4505 |
if not Assigned(ResamplerList) then Exit; |
| 4506 |
|
| 4507 |
for i := ResamplerList.Count -1 downto 0 do |
| 4508 |
if TResamplerObj(ResamplerList[i]).id = id then |
| 4509 |
begin |
| 4510 |
Result := TResamplerObj(ResamplerList[i]).func; |
| 4511 |
Break; |
| 4512 |
end; |
| 4513 |
end; |
| 4514 |
//------------------------------------------------------------------------------ |
| 4515 |
|
| 4516 |
function RegisterResampler(func: TResamplerFunction; const name: string): integer; |
| 4517 |
var |
| 4518 |
resampleObj: TResamplerObj; |
| 4519 |
begin |
| 4520 |
if not Assigned(ResamplerList) then |
| 4521 |
CreateResamplerList; |
| 4522 |
|
| 4523 |
resampleObj := TResamplerObj.Create; |
| 4524 |
Result := ResamplerList.Add(resampleObj) +1; |
| 4525 |
resampleObj.id := Result; |
| 4526 |
resampleObj.name := name; |
| 4527 |
resampleObj.func := func; |
| 4528 |
end; |
| 4529 |
//------------------------------------------------------------------------------ |
| 4530 |
|
| 4531 |
procedure GetResamplerList(stringList: TStringList); |
| 4532 |
var |
| 4533 |
i: integer; |
| 4534 |
resampleObj: TResamplerObj; |
| 4535 |
begin |
| 4536 |
stringList.Clear; |
| 4537 |
stringList.Capacity := ResamplerList.Count; |
| 4538 |
for i := 0 to ResamplerList.Count -1 do |
| 4539 |
begin |
| 4540 |
resampleObj := ResamplerList[i]; |
| 4541 |
stringList.AddObject(resampleObj.name, resampleObj); |
| 4542 |
end; |
| 4543 |
end; |
| 4544 |
//------------------------------------------------------------------------------ |
| 4545 |
|
| 4546 |
procedure CleanUpResamplerClassList; |
| 4547 |
var |
| 4548 |
i: integer; |
| 4549 |
begin |
| 4550 |
if not Assigned(ResamplerList) then Exit; |
| 4551 |
for i := ResamplerList.Count -1 downto 0 do |
| 4552 |
TResamplerObj(ResamplerList[i]).Free; |
| 4553 |
ResamplerList.Free; |
| 4554 |
end; |
| 4555 |
//------------------------------------------------------------------------------ |
| 4556 |
|
| 4557 |
initialization |
| 4558 |
CreateImageFormatList; |
| 4559 |
MakeBlendTables; |
| 4560 |
|
| 4561 |
{$IFDEF MSWINDOWS} |
| 4562 |
GetScreenScale; |
| 4563 |
{$ENDIF} |
| 4564 |
|
| 4565 |
finalization |
| 4566 |
CleanUpImageFormatClassList; |
| 4567 |
CleanUpResamplerClassList; |
| 4568 |
|
| 4569 |
end. |