| 1 |
{%MainUnit castleimages.pas} |
| 2 |
{ |
| 3 |
Copyright 2001-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Implementations of DrawFrom, DrawTo and DrawFromCore methods. } |
| 18 |
|
| 19 |
{ TCastleImage --------------------------------------------------------------- } |
| 20 |
|
| 21 |
const |
| 22 |
SErrBlendModeNotImplemented = 'Blend mode not implemented (drawing from image class %s to %s)'; |
| 23 |
|
| 24 |
procedure TCastleImage.DrawFromCore(Source: TCastleImage; |
| 25 |
X, Y, SourceX, SourceY, SourceWidth, SourceHeight: Integer; |
| 26 |
const Mode: TDrawMode); |
| 27 |
var |
| 28 |
aLine: Integer; |
| 29 |
Ptr, SourcePtr: Pointer; |
| 30 |
RowWidth, SourceRowWidth, SourceCopyRowWidth: Cardinal; |
| 31 |
begin |
| 32 |
if Source.ClassType <> ClassType then |
| 33 |
raise EImageDrawError.CreateFmt('Blend mode for this source/destination combination may be not implemented. Cannot draw pixels from image class %s to %s', |
| 34 |
[Source.ClassName, ClassName]); |
| 35 |
|
| 36 |
if (Mode <> dmBlend) and (Mode <> dmOverwrite) then |
| 37 |
raise EImageDrawError.CreateFmt('Blend mode for this source/destination combination may be not implemented. Base TCastleImage.DrawFromCore cannot draw pixels with Mode <> dmBlend or dmOverwrite, override DrawFromCore to implement this correctly, from image class %s to %s', |
| 38 |
[Source.ClassName, ClassName]); |
| 39 |
|
| 40 |
Ptr := PixelPtr(X, Y); |
| 41 |
RowWidth := Width * PixelSize; |
| 42 |
|
| 43 |
SourcePtr := Source.PixelPtr(SourceX, SourceY); |
| 44 |
SourceRowWidth := Source.Width * Source.PixelSize; |
| 45 |
SourceCopyRowWidth := SourceWidth * Source.PixelSize; |
| 46 |
|
| 47 |
for aLine := 0 to Integer(SourceHeight) - 1 do |
| 48 |
begin |
| 49 |
Move(SourcePtr^, Ptr^, SourceCopyRowWidth); |
| 50 |
PtrUInt(Ptr) := PtrUInt(Ptr) + RowWidth; |
| 51 |
PtrUInt(SourcePtr) := PtrUInt(SourcePtr) + SourceRowWidth; |
| 52 |
end; |
| 53 |
end; |
| 54 |
|
| 55 |
procedure TCastleImage.DrawFrom(Source: TCastleImage; |
| 56 |
X, Y, SourceX, SourceY, SourceWidth, SourceHeight: Integer; |
| 57 |
const Mode: TDrawMode); |
| 58 |
begin |
| 59 |
if X < 0 then |
| 60 |
begin |
| 61 |
Dec(SourceX, X); |
| 62 |
Inc(SourceWidth, X); |
| 63 |
X := 0; |
| 64 |
end; |
| 65 |
|
| 66 |
if Y < 0 then |
| 67 |
begin |
| 68 |
Dec(SourceY, Y); |
| 69 |
Inc(SourceHeight, Y); |
| 70 |
Y := 0; |
| 71 |
end; |
| 72 |
|
| 73 |
if SourceX < 0 then |
| 74 |
begin |
| 75 |
Dec(X, SourceX); |
| 76 |
Inc(SourceWidth, SourceX); |
| 77 |
SourceX := 0; |
| 78 |
end; |
| 79 |
|
| 80 |
if SourceY < 0 then |
| 81 |
begin |
| 82 |
Dec(Y, SourceY); |
| 83 |
Inc(SourceHeight, SourceY); |
| 84 |
SourceY := 0; |
| 85 |
end; |
| 86 |
|
| 87 |
SourceWidth := MinIntValue([SourceWidth , Width - X, Source.Width ]); |
| 88 |
SourceHeight := MinIntValue([SourceHeight, Height - Y, Source.Height]); |
| 89 |
|
| 90 |
if (SourceWidth > 0) and |
| 91 |
(SourceHeight > 0) and |
| 92 |
(SourceX < Source.Width) and |
| 93 |
(SourceY < Source.Height) then |
| 94 |
DrawFromCore(Source, X, Y, SourceX, SourceY, SourceWidth, SourceHeight, Mode); |
| 95 |
end; |
| 96 |
|
| 97 |
procedure TCastleImage.DrawFrom(Source: TCastleImage; const X, Y: Integer; |
| 98 |
const Mode: TDrawMode); |
| 99 |
begin |
| 100 |
DrawFrom(Source, X, Y, 0, 0, Source.Width, Source.Height, Mode); |
| 101 |
end; |
| 102 |
|
| 103 |
procedure TCastleImage.DrawTo(Destination: TCastleImage; const X, Y: Integer; |
| 104 |
const Mode: TDrawMode); |
| 105 |
begin |
| 106 |
Destination.DrawFrom(Self, X, Y, Mode); |
| 107 |
end; |
| 108 |
|
| 109 |
{ Helper routines ------------------------------------------------------------ } |
| 110 |
|
| 111 |
function BlendBytes(const Dest, Source, Opacity: Byte): Byte; {$ifdef SUPPORTS_INLINE} inline; {$endif} |
| 112 |
var |
| 113 |
W: Word; |
| 114 |
begin |
| 115 |
W := |
| 116 |
Word(Dest ) * (255 - Opacity) div 255 + |
| 117 |
Word(Source) * Opacity div 255; |
| 118 |
if W > 255 then W := 255; |
| 119 |
Result := W; |
| 120 |
end; |
| 121 |
|
| 122 |
function AddBytes(const Dest, Source, Opacity: Byte): Byte; {$ifdef SUPPORTS_INLINE} inline; {$endif} |
| 123 |
var |
| 124 |
W: Word; |
| 125 |
begin |
| 126 |
W := Dest + Word(Source) * Opacity div 255; |
| 127 |
if W > 255 then W := 255; |
| 128 |
Result := W; |
| 129 |
end; |
| 130 |
|
| 131 |
function AddBytesPremultiplied(const Dest, Source: Byte): Byte; {$ifdef SUPPORTS_INLINE} inline; {$endif} |
| 132 |
var |
| 133 |
W: Word; |
| 134 |
begin |
| 135 |
W := Dest + Source; |
| 136 |
if W > 255 then W := 255; |
| 137 |
Result := W; |
| 138 |
end; |
| 139 |
|
| 140 |
function MultiplyBytes(const Dest, Source: Byte): Byte; {$ifdef SUPPORTS_INLINE} inline; {$endif} |
| 141 |
var |
| 142 |
W: Word; |
| 143 |
begin |
| 144 |
W := Dest * Word(Source) div 255; |
| 145 |
if W > 255 then W := 255; |
| 146 |
Result := W; |
| 147 |
end; |
| 148 |
|
| 149 |
{ Used by dmMultiply when source image has alpha, but destination doesn't. |
| 150 |
If SourceOpacity = 255, the result must be equal to MultiplyBytes(Dest, Source) |
| 151 |
result (following the rule "image without alpha always works like image with opaque alpha"), |
| 152 |
but we can do something more useful in case SourceOpacity < 255. } |
| 153 |
function MultiplyBytesOpaque(const Dest, Source, SourceOpacity: Byte): Byte; {$ifdef SUPPORTS_INLINE} inline; {$endif} |
| 154 |
var |
| 155 |
W: Word; |
| 156 |
begin |
| 157 |
W := Word(Dest) * (255 - SourceOpacity) div 255 + |
| 158 |
Word(Dest) * Source div 255 * SourceOpacity div 255; |
| 159 |
if W > 255 then W := 255; |
| 160 |
Result := W; |
| 161 |
end; |
| 162 |
|
| 163 |
procedure BlendSmartParameters(const DestAlpha, SourceAlpha: Byte; |
| 164 |
out Alpha1d, Alpha2, AlphaSum: Single); {$ifdef SUPPORTS_INLINE} inline; {$endif} |
| 165 |
var |
| 166 |
// variables for smart-blending mode |
| 167 |
Alpha1: Single; |
| 168 |
begin |
| 169 |
// get alpha in 0..1 range |
| 170 |
Alpha1 := DestAlpha / 255; |
| 171 |
Alpha2 := SourceAlpha / 255; |
| 172 |
// calculate alpha-sums according to https://en.wikipedia.org/wiki/Alpha_compositing |
| 173 |
Alpha1d := Alpha1 * (1 - Alpha2); |
| 174 |
AlphaSum := Alpha1 + (1 - Alpha1) * Alpha2; |
| 175 |
end; |
| 176 |
|
| 177 |
{ Drawing when TRGBAlphaImage is a destination ------------------------------- } |
| 178 |
|
| 179 |
procedure TRGBAlphaImage.DrawFromCore(Source: TCastleImage; |
| 180 |
X, Y, SourceX, SourceY, SourceWidth, SourceHeight: Integer; |
| 181 |
const Mode: TDrawMode); |
| 182 |
|
| 183 |
procedure SourceRGB(Source: TRGBImage); |
| 184 |
var |
| 185 |
PSource: PVector3Byte; |
| 186 |
PDest: PVector4Byte; |
| 187 |
DestX, DestY: Integer; |
| 188 |
begin |
| 189 |
case Mode of |
| 190 |
dmBlend: |
| 191 |
for DestY := Y to Y + SourceHeight - 1 do |
| 192 |
begin |
| 193 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 194 |
PDest := PixelPtr(X, DestY); |
| 195 |
for DestX := X to X + SourceWidth - 1 do |
| 196 |
begin |
| 197 |
PVector3Byte(PDest)^ := PSource^; |
| 198 |
Inc(PSource); |
| 199 |
Inc(PDest); |
| 200 |
end; |
| 201 |
end; |
| 202 |
dmMultiply: |
| 203 |
begin |
| 204 |
for DestY := Y to Y + SourceHeight - 1 do |
| 205 |
begin |
| 206 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 207 |
PDest := PixelPtr(X, DestY); |
| 208 |
for DestX := X to X + SourceWidth - 1 do |
| 209 |
begin |
| 210 |
PDest^.X := MultiplyBytes(PDest^.X, PSource^.X); |
| 211 |
PDest^.Y := MultiplyBytes(PDest^.Y, PSource^.Y); |
| 212 |
PDest^.Z := MultiplyBytes(PDest^.Z, PSource^.Z); |
| 213 |
Inc(PSource); |
| 214 |
Inc(PDest); |
| 215 |
end; |
| 216 |
end; |
| 217 |
end; |
| 218 |
dmOverwrite: |
| 219 |
begin |
| 220 |
for DestY := Y to Y + SourceHeight - 1 do |
| 221 |
begin |
| 222 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 223 |
PDest := PixelPtr(X, DestY); |
| 224 |
for DestX := X to X + SourceWidth - 1 do |
| 225 |
begin |
| 226 |
PDest^.X := PSource^.X; |
| 227 |
PDest^.Y := PSource^.Y; |
| 228 |
PDest^.Z := PSource^.Z; |
| 229 |
PDest^.W := 255; |
| 230 |
Inc(PSource); |
| 231 |
Inc(PDest); |
| 232 |
end; |
| 233 |
end; |
| 234 |
end; |
| 235 |
dmAdd: |
| 236 |
for DestY := Y to Y + SourceHeight - 1 do |
| 237 |
begin |
| 238 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 239 |
PDest := PixelPtr(X, DestY); |
| 240 |
for DestX := X to X + SourceWidth - 1 do |
| 241 |
begin |
| 242 |
PDest^.X := AddBytesPremultiplied(PDest^.X, PSource^.X); |
| 243 |
PDest^.Y := AddBytesPremultiplied(PDest^.Y, PSource^.Y); |
| 244 |
PDest^.Z := AddBytesPremultiplied(PDest^.Z, PSource^.Z); |
| 245 |
Inc(PSource); |
| 246 |
Inc(PDest); |
| 247 |
end; |
| 248 |
end; |
| 249 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 250 |
end; |
| 251 |
end; |
| 252 |
|
| 253 |
procedure SourceRGBAlpha(Source: TRGBAlphaImage); |
| 254 |
var |
| 255 |
PSource: PVector4Byte; |
| 256 |
PDest: PVector4Byte; |
| 257 |
DestX, DestY: Integer; |
| 258 |
// variables for smart-blending mode |
| 259 |
Alpha1d, Alpha2, AlphaSum: Single; |
| 260 |
begin |
| 261 |
case Mode of |
| 262 |
dmBlend: |
| 263 |
for DestY := Y to Y + SourceHeight - 1 do |
| 264 |
begin |
| 265 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 266 |
PDest := PixelPtr(X, DestY); |
| 267 |
for DestX := X to X + SourceWidth - 1 do |
| 268 |
begin |
| 269 |
PDest^.X := BlendBytes(PDest^.X, PSource^.X, PSource^.W); |
| 270 |
PDest^.Y := BlendBytes(PDest^.Y, PSource^.Y, PSource^.W); |
| 271 |
PDest^.Z := BlendBytes(PDest^.Z, PSource^.Z, PSource^.W); |
| 272 |
Inc(PSource); |
| 273 |
Inc(PDest); |
| 274 |
end; |
| 275 |
end; |
| 276 |
dmBlendSmart: |
| 277 |
for DestY := Y to Y + SourceHeight - 1 do |
| 278 |
begin |
| 279 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 280 |
PDest := PixelPtr(X, DestY); |
| 281 |
for DestX := X to X + SourceWidth - 1 do |
| 282 |
begin |
| 283 |
BlendSmartParameters(PDest^.W, PSource^.W, Alpha1d, Alpha2, AlphaSum); |
| 284 |
if AlphaSum > 0 then |
| 285 |
begin |
| 286 |
PDest^.X := Round((PDest^.X * Alpha1d + PSource^.X * Alpha2) / AlphaSum); |
| 287 |
PDest^.Y := Round((PDest^.Y * Alpha1d + PSource^.Y * Alpha2) / AlphaSum); |
| 288 |
PDest^.Z := Round((PDest^.Z * Alpha1d + PSource^.Z * Alpha2) / AlphaSum); |
| 289 |
PDest^.W := Round(255 * AlphaSum); |
| 290 |
end; |
| 291 |
Inc(PSource); |
| 292 |
Inc(PDest); |
| 293 |
end; |
| 294 |
end; |
| 295 |
dmMultiply: |
| 296 |
begin |
| 297 |
for DestY := Y to Y + SourceHeight - 1 do |
| 298 |
begin |
| 299 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 300 |
PDest := PixelPtr(X, DestY); |
| 301 |
for DestX := X to X + SourceWidth - 1 do |
| 302 |
begin |
| 303 |
PDest^.X := MultiplyBytes(PDest^.X, PSource^.X); |
| 304 |
PDest^.Y := MultiplyBytes(PDest^.Y, PSource^.Y); |
| 305 |
PDest^.Z := MultiplyBytes(PDest^.Z, PSource^.Z); |
| 306 |
PDest^.W := MultiplyBytes(PDest^.W, PSource^.W); |
| 307 |
Inc(PSource); |
| 308 |
Inc(PDest); |
| 309 |
end; |
| 310 |
end; |
| 311 |
end; |
| 312 |
dmOverwrite: inherited DrawFromCore(Source, X, Y, SourceX, SourceY, SourceWidth, SourceHeight, Mode); //handled by TCastleImage.DrawFromCore |
| 313 |
dmAdd: |
| 314 |
if Source.PremultipliedAlpha then |
| 315 |
begin |
| 316 |
for DestY := Y to Y + SourceHeight - 1 do |
| 317 |
begin |
| 318 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 319 |
PDest := PixelPtr(X, DestY); |
| 320 |
for DestX := X to X + SourceWidth - 1 do |
| 321 |
begin |
| 322 |
PDest^.X := AddBytesPremultiplied(PDest^.X, PSource^.X); |
| 323 |
PDest^.Y := AddBytesPremultiplied(PDest^.Y, PSource^.Y); |
| 324 |
PDest^.Z := AddBytesPremultiplied(PDest^.Z, PSource^.Z); |
| 325 |
Inc(PSource); |
| 326 |
Inc(PDest); |
| 327 |
end; |
| 328 |
end; |
| 329 |
end else |
| 330 |
begin |
| 331 |
for DestY := Y to Y + SourceHeight - 1 do |
| 332 |
begin |
| 333 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 334 |
PDest := PixelPtr(X, DestY); |
| 335 |
for DestX := X to X + SourceWidth - 1 do |
| 336 |
begin |
| 337 |
PDest^.X := AddBytes(PDest^.X, PSource^.X, PSource^.W); |
| 338 |
PDest^.Y := AddBytes(PDest^.Y, PSource^.Y, PSource^.W); |
| 339 |
PDest^.Z := AddBytes(PDest^.Z, PSource^.Z, PSource^.W); |
| 340 |
Inc(PSource); |
| 341 |
Inc(PDest); |
| 342 |
end; |
| 343 |
end; |
| 344 |
end; |
| 345 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 346 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 347 |
{$endif} |
| 348 |
end; |
| 349 |
end; |
| 350 |
|
| 351 |
procedure SourceGrayscaleTreatedAsAlpha(Source: TGrayscaleImage); |
| 352 |
var |
| 353 |
PSource: PByte; |
| 354 |
PDest: PVector4Byte; |
| 355 |
DestX, DestY: Integer; |
| 356 |
// variables for smart-blending mode |
| 357 |
Alpha1d, Alpha2, AlphaSum: Single; |
| 358 |
begin |
| 359 |
case Mode of |
| 360 |
dmBlend: |
| 361 |
for DestY := Y to Y + SourceHeight - 1 do |
| 362 |
begin |
| 363 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 364 |
PDest := PixelPtr(X, DestY); |
| 365 |
for DestX := X to X + SourceWidth - 1 do |
| 366 |
begin |
| 367 |
PDest^.X := BlendBytes(PDest^.X, Source.ColorWhenTreatedAsAlpha.X, PSource^); |
| 368 |
PDest^.Y := BlendBytes(PDest^.Y, Source.ColorWhenTreatedAsAlpha.Y, PSource^); |
| 369 |
PDest^.Z := BlendBytes(PDest^.Z, Source.ColorWhenTreatedAsAlpha.Z, PSource^); |
| 370 |
Inc(PSource); |
| 371 |
Inc(PDest); |
| 372 |
end; |
| 373 |
end; |
| 374 |
dmBlendSmart: |
| 375 |
for DestY := Y to Y + SourceHeight - 1 do |
| 376 |
begin |
| 377 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 378 |
PDest := PixelPtr(X, DestY); |
| 379 |
for DestX := X to X + SourceWidth - 1 do |
| 380 |
begin |
| 381 |
BlendSmartParameters(PDest^.W, PSource^, Alpha1d, Alpha2, AlphaSum); |
| 382 |
if AlphaSum > 0 then |
| 383 |
begin |
| 384 |
PDest^.X := Round((PDest^.X * Alpha1d + Source.ColorWhenTreatedAsAlpha.X * Alpha2) / AlphaSum); |
| 385 |
PDest^.Y := Round((PDest^.Y * Alpha1d + Source.ColorWhenTreatedAsAlpha.Y * Alpha2) / AlphaSum); |
| 386 |
PDest^.Z := Round((PDest^.Z * Alpha1d + Source.ColorWhenTreatedAsAlpha.Z * Alpha2) / AlphaSum); |
| 387 |
PDest^.W := Round(255 * AlphaSum); |
| 388 |
end; |
| 389 |
Inc(PSource); |
| 390 |
Inc(PDest); |
| 391 |
end; |
| 392 |
end; |
| 393 |
dmMultiply: |
| 394 |
begin |
| 395 |
for DestY := Y to Y + SourceHeight - 1 do |
| 396 |
begin |
| 397 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 398 |
PDest := PixelPtr(X, DestY); |
| 399 |
for DestX := X to X + SourceWidth - 1 do |
| 400 |
begin |
| 401 |
PDest^.X := MultiplyBytes(PDest^.X, Source.ColorWhenTreatedAsAlpha.X); |
| 402 |
PDest^.Y := MultiplyBytes(PDest^.Y, Source.ColorWhenTreatedAsAlpha.Y); |
| 403 |
PDest^.Z := MultiplyBytes(PDest^.Z, Source.ColorWhenTreatedAsAlpha.Z); |
| 404 |
PDest^.W := MultiplyBytes(PDest^.W, PSource^); |
| 405 |
Inc(PSource); |
| 406 |
Inc(PDest); |
| 407 |
end; |
| 408 |
end; |
| 409 |
end; |
| 410 |
dmOverwrite: |
| 411 |
begin |
| 412 |
for DestY := Y to Y + SourceHeight - 1 do |
| 413 |
begin |
| 414 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 415 |
PDest := PixelPtr(X, DestY); |
| 416 |
for DestX := X to X + SourceWidth - 1 do |
| 417 |
begin |
| 418 |
PDest^.X := Source.ColorWhenTreatedAsAlpha.X; |
| 419 |
PDest^.Y := Source.ColorWhenTreatedAsAlpha.Y; |
| 420 |
PDest^.Z := Source.ColorWhenTreatedAsAlpha.Z; |
| 421 |
PDest^.W := PSource^; |
| 422 |
Inc(PSource); |
| 423 |
Inc(PDest); |
| 424 |
end; |
| 425 |
end; |
| 426 |
end; |
| 427 |
dmAdd: |
| 428 |
begin |
| 429 |
for DestY := Y to Y + SourceHeight - 1 do |
| 430 |
begin |
| 431 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 432 |
PDest := PixelPtr(X, DestY); |
| 433 |
for DestX := X to X + SourceWidth - 1 do |
| 434 |
begin |
| 435 |
PDest^.X := AddBytes(PDest^.X, Source.ColorWhenTreatedAsAlpha.X, PSource^); |
| 436 |
PDest^.Y := AddBytes(PDest^.Y, Source.ColorWhenTreatedAsAlpha.Y, PSource^); |
| 437 |
PDest^.Z := AddBytes(PDest^.Z, Source.ColorWhenTreatedAsAlpha.Z, PSource^); |
| 438 |
Inc(PSource); |
| 439 |
Inc(PDest); |
| 440 |
end; |
| 441 |
end; |
| 442 |
end; |
| 443 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 444 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 445 |
{$endif} |
| 446 |
end; |
| 447 |
end; |
| 448 |
|
| 449 |
procedure SourceGrayscaleAlpha(Source: TGrayscaleAlphaImage); |
| 450 |
var |
| 451 |
PSource: PVector2Byte; |
| 452 |
PDest: PVector4Byte; |
| 453 |
DestX, DestY: Integer; |
| 454 |
// variables for smart-blending mode |
| 455 |
Alpha1d, Alpha2, AlphaSum: Single; |
| 456 |
begin |
| 457 |
case Mode of |
| 458 |
dmBlend: |
| 459 |
for DestY := Y to Y + SourceHeight - 1 do |
| 460 |
begin |
| 461 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 462 |
PDest := PixelPtr(X, DestY); |
| 463 |
for DestX := X to X + SourceWidth - 1 do |
| 464 |
begin |
| 465 |
PDest^.X := BlendBytes(PDest^.X, PSource^.X, PSource^.Y); |
| 466 |
PDest^.Y := BlendBytes(PDest^.Y, PSource^.X, PSource^.Y); |
| 467 |
PDest^.Z := BlendBytes(PDest^.Z, PSource^.X, PSource^.Y); |
| 468 |
Inc(PSource); |
| 469 |
Inc(PDest); |
| 470 |
end; |
| 471 |
end; |
| 472 |
dmBlendSmart: |
| 473 |
for DestY := Y to Y + SourceHeight - 1 do |
| 474 |
begin |
| 475 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 476 |
PDest := PixelPtr(X, DestY); |
| 477 |
for DestX := X to X + SourceWidth - 1 do |
| 478 |
begin |
| 479 |
BlendSmartParameters(PDest^.W, PSource^.Y, Alpha1d, Alpha2, AlphaSum); |
| 480 |
if AlphaSum > 0 then |
| 481 |
begin |
| 482 |
PDest^.X := Round((PDest^.X * Alpha1d + PSource^.X * Alpha2) / AlphaSum); |
| 483 |
PDest^.Y := Round((PDest^.Y * Alpha1d + PSource^.X * Alpha2) / AlphaSum); |
| 484 |
PDest^.Z := Round((PDest^.Z * Alpha1d + PSource^.X * Alpha2) / AlphaSum); |
| 485 |
PDest^.W := Round(255 * AlphaSum); |
| 486 |
end; |
| 487 |
Inc(PSource); |
| 488 |
Inc(PDest); |
| 489 |
end; |
| 490 |
end; |
| 491 |
dmMultiply: |
| 492 |
begin |
| 493 |
for DestY := Y to Y + SourceHeight - 1 do |
| 494 |
begin |
| 495 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 496 |
PDest := PixelPtr(X, DestY); |
| 497 |
for DestX := X to X + SourceWidth - 1 do |
| 498 |
begin |
| 499 |
PDest^.X := MultiplyBytes(PDest^.X, PSource^.X); |
| 500 |
PDest^.Y := MultiplyBytes(PDest^.Y, PSource^.X); |
| 501 |
PDest^.Z := MultiplyBytes(PDest^.Z, PSource^.X); |
| 502 |
PDest^.W := MultiplyBytes(PDest^.W, PSource^.Y); |
| 503 |
Inc(PSource); |
| 504 |
Inc(PDest); |
| 505 |
end; |
| 506 |
end; |
| 507 |
end; |
| 508 |
dmOverwrite: |
| 509 |
begin |
| 510 |
for DestY := Y to Y + SourceHeight - 1 do |
| 511 |
begin |
| 512 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 513 |
PDest := PixelPtr(X, DestY); |
| 514 |
for DestX := X to X + SourceWidth - 1 do |
| 515 |
begin |
| 516 |
PDest^.X := PSource^.X; |
| 517 |
PDest^.Y := PSource^.X; |
| 518 |
PDest^.Z := PSource^.X; |
| 519 |
PDest^.W := PSource^.Y; |
| 520 |
Inc(PSource); |
| 521 |
Inc(PDest); |
| 522 |
end; |
| 523 |
end; |
| 524 |
end; |
| 525 |
dmAdd: |
| 526 |
{if Source.PremultipliedAlpha then |
| 527 |
begin |
| 528 |
for DestY := Y to Y + SourceHeight - 1 do |
| 529 |
begin |
| 530 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 531 |
PDest := PixelPtr(X, DestY); |
| 532 |
for DestX := X to X + SourceWidth - 1 do |
| 533 |
begin |
| 534 |
PDest^.X := AddBytesPremultiplied(PDest^.X, PSource^.X); |
| 535 |
PDest^.Y := AddBytesPremultiplied(PDest^.Y, PSource^.X); |
| 536 |
PDest^.Z := AddBytesPremultiplied(PDest^.Z, PSource^.X); |
| 537 |
Inc(PSource); |
| 538 |
Inc(PDest); |
| 539 |
end; |
| 540 |
end; |
| 541 |
end else} |
| 542 |
begin |
| 543 |
for DestY := Y to Y + SourceHeight - 1 do |
| 544 |
begin |
| 545 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 546 |
PDest := PixelPtr(X, DestY); |
| 547 |
for DestX := X to X + SourceWidth - 1 do |
| 548 |
begin |
| 549 |
PDest^.X := AddBytes(PDest^.X, PSource^.X, PSource^.Y); |
| 550 |
PDest^.Y := AddBytes(PDest^.Y, PSource^.X, PSource^.Y); |
| 551 |
PDest^.Z := AddBytes(PDest^.Z, PSource^.X, PSource^.Y); |
| 552 |
Inc(PSource); |
| 553 |
Inc(PDest); |
| 554 |
end; |
| 555 |
end; |
| 556 |
end; |
| 557 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 558 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 559 |
{$endif} |
| 560 |
end; |
| 561 |
end; |
| 562 |
|
| 563 |
procedure SourceGrayscale(Source: TGrayscaleImage); |
| 564 |
var |
| 565 |
PSource: PByte; |
| 566 |
PDest: PVector4Byte; |
| 567 |
DestX, DestY: Integer; |
| 568 |
begin |
| 569 |
case Mode of |
| 570 |
dmBlend: |
| 571 |
for DestY := Y to Y + SourceHeight - 1 do |
| 572 |
begin |
| 573 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 574 |
PDest := PixelPtr(X, DestY); |
| 575 |
for DestX := X to X + SourceWidth - 1 do |
| 576 |
begin |
| 577 |
PDest^.X := PSource^; |
| 578 |
PDest^.Y := PSource^; |
| 579 |
PDest^.Z := PSource^; |
| 580 |
Inc(PSource); |
| 581 |
Inc(PDest); |
| 582 |
end; |
| 583 |
end; |
| 584 |
dmMultiply: |
| 585 |
begin |
| 586 |
for DestY := Y to Y + SourceHeight - 1 do |
| 587 |
begin |
| 588 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 589 |
PDest := PixelPtr(X, DestY); |
| 590 |
for DestX := X to X + SourceWidth - 1 do |
| 591 |
begin |
| 592 |
PDest^.X := MultiplyBytes(PDest^.X, PSource^); |
| 593 |
PDest^.Y := MultiplyBytes(PDest^.Y, PSource^); |
| 594 |
PDest^.Z := MultiplyBytes(PDest^.Z, PSource^); |
| 595 |
Inc(PSource); |
| 596 |
Inc(PDest); |
| 597 |
end; |
| 598 |
end; |
| 599 |
end; |
| 600 |
dmOverwrite: |
| 601 |
begin |
| 602 |
for DestY := Y to Y + SourceHeight - 1 do |
| 603 |
begin |
| 604 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 605 |
PDest := PixelPtr(X, DestY); |
| 606 |
for DestX := X to X + SourceWidth - 1 do |
| 607 |
begin |
| 608 |
PDest^.X := PSource^; |
| 609 |
PDest^.Y := PSource^; |
| 610 |
PDest^.Z := PSource^; |
| 611 |
PDest^.W := 255; |
| 612 |
Inc(PSource); |
| 613 |
Inc(PDest); |
| 614 |
end; |
| 615 |
end; |
| 616 |
end; |
| 617 |
dmAdd: |
| 618 |
for DestY := Y to Y + SourceHeight - 1 do |
| 619 |
begin |
| 620 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 621 |
PDest := PixelPtr(X, DestY); |
| 622 |
for DestX := X to X + SourceWidth - 1 do |
| 623 |
begin |
| 624 |
PDest^.X := AddBytesPremultiplied(PDest^.X, PSource^); |
| 625 |
PDest^.Y := AddBytesPremultiplied(PDest^.Y, PSource^); |
| 626 |
PDest^.Z := AddBytesPremultiplied(PDest^.Z, PSource^); |
| 627 |
Inc(PSource); |
| 628 |
Inc(PDest); |
| 629 |
end; |
| 630 |
end; |
| 631 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 632 |
end; |
| 633 |
end; |
| 634 |
|
| 635 |
begin |
| 636 |
if Source is TRGBImage then |
| 637 |
SourceRGB(TRGBImage(Source)) else |
| 638 |
if Source is TRGBAlphaImage then |
| 639 |
SourceRGBAlpha(TRGBAlphaImage(Source)) else |
| 640 |
if Source is TGrayscaleAlphaImage then |
| 641 |
SourceGrayscaleAlpha(TGrayscaleAlphaImage(Source)) else |
| 642 |
if Source is TGrayscaleImage then |
| 643 |
begin |
| 644 |
if TGrayscaleImage(Source).TreatAsAlpha then |
| 645 |
SourceGrayscaleTreatedAsAlpha(TGrayscaleImage(Source)) |
| 646 |
else |
| 647 |
SourceGrayscale(TGrayscaleImage(Source)); |
| 648 |
end else |
| 649 |
inherited; |
| 650 |
end; |
| 651 |
|
| 652 |
{ Drawing when TRGBImage is a destination ------------------------------------ } |
| 653 |
|
| 654 |
procedure TRGBImage.DrawFromCore(Source: TCastleImage; |
| 655 |
X, Y, SourceX, SourceY, SourceWidth, SourceHeight: Integer; |
| 656 |
const Mode: TDrawMode); |
| 657 |
|
| 658 |
procedure SourceRGB(Source: TRGBImage); |
| 659 |
var |
| 660 |
PSource: PVector3Byte; |
| 661 |
PDest: PVector3Byte; |
| 662 |
DestX, DestY: Integer; |
| 663 |
begin |
| 664 |
case Mode of |
| 665 |
{ dmBlend is handled by inherited } |
| 666 |
dmMultiply: |
| 667 |
begin |
| 668 |
for DestY := Y to Y + SourceHeight - 1 do |
| 669 |
begin |
| 670 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 671 |
PDest := PixelPtr(X, DestY); |
| 672 |
for DestX := X to X + SourceWidth - 1 do |
| 673 |
begin |
| 674 |
PDest^.X := MultiplyBytes(PDest^.X, PSource^.X); |
| 675 |
PDest^.Y := MultiplyBytes(PDest^.Y, PSource^.Y); |
| 676 |
PDest^.Z := MultiplyBytes(PDest^.Z, PSource^.Z); |
| 677 |
Inc(PSource); |
| 678 |
Inc(PDest); |
| 679 |
end; |
| 680 |
end; |
| 681 |
end; |
| 682 |
dmOverwrite: inherited DrawFromCore(Source, X, Y, SourceX, SourceY, SourceWidth, SourceHeight, Mode); //handled by TCastleImage.DrawFromCore |
| 683 |
dmAdd: |
| 684 |
for DestY := Y to Y + SourceHeight - 1 do |
| 685 |
begin |
| 686 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 687 |
PDest := PixelPtr(X, DestY); |
| 688 |
for DestX := X to X + SourceWidth - 1 do |
| 689 |
begin |
| 690 |
PDest^.X := AddBytesPremultiplied(PDest^.X, PSource^.X); |
| 691 |
PDest^.Y := AddBytesPremultiplied(PDest^.Y, PSource^.Y); |
| 692 |
PDest^.Z := AddBytesPremultiplied(PDest^.Z, PSource^.Z); |
| 693 |
Inc(PSource); |
| 694 |
Inc(PDest); |
| 695 |
end; |
| 696 |
end; |
| 697 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 698 |
end; |
| 699 |
end; |
| 700 |
|
| 701 |
procedure SourceRGBAlpha(Source: TRGBAlphaImage); |
| 702 |
var |
| 703 |
PSource: PVector4Byte; |
| 704 |
PDest: PVector3Byte; |
| 705 |
DestX, DestY: Integer; |
| 706 |
begin |
| 707 |
case Mode of |
| 708 |
dmBlend, dmBlendSmart: |
| 709 |
for DestY := Y to Y + SourceHeight - 1 do |
| 710 |
begin |
| 711 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 712 |
PDest := PixelPtr(X, DestY); |
| 713 |
for DestX := X to X + SourceWidth - 1 do |
| 714 |
begin |
| 715 |
PDest^.X := BlendBytes(PDest^.X, PSource^.X, PSource^.W); |
| 716 |
PDest^.Y := BlendBytes(PDest^.Y, PSource^.Y, PSource^.W); |
| 717 |
PDest^.Z := BlendBytes(PDest^.Z, PSource^.Z, PSource^.W); |
| 718 |
Inc(PSource); |
| 719 |
Inc(PDest); |
| 720 |
end; |
| 721 |
end; |
| 722 |
dmMultiply: |
| 723 |
begin |
| 724 |
for DestY := Y to Y + SourceHeight - 1 do |
| 725 |
begin |
| 726 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 727 |
PDest := PixelPtr(X, DestY); |
| 728 |
for DestX := X to X + SourceWidth - 1 do |
| 729 |
begin |
| 730 |
PDest^.X := MultiplyBytesOpaque(PDest^.X, PSource^.X, PSource^.W); |
| 731 |
PDest^.Y := MultiplyBytesOpaque(PDest^.Y, PSource^.Y, PSource^.W); |
| 732 |
PDest^.Z := MultiplyBytesOpaque(PDest^.Z, PSource^.Z, PSource^.W); |
| 733 |
Inc(PSource); |
| 734 |
Inc(PDest); |
| 735 |
end; |
| 736 |
end; |
| 737 |
end; |
| 738 |
dmOverwrite: |
| 739 |
begin |
| 740 |
for DestY := Y to Y + SourceHeight - 1 do |
| 741 |
begin |
| 742 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 743 |
PDest := PixelPtr(X, DestY); |
| 744 |
for DestX := X to X + SourceWidth - 1 do |
| 745 |
begin |
| 746 |
PDest^.X := PSource^.X; |
| 747 |
PDest^.Y := PSource^.Y; |
| 748 |
PDest^.Z := PSource^.Z; |
| 749 |
Inc(PSource); |
| 750 |
Inc(PDest); |
| 751 |
end; |
| 752 |
end; |
| 753 |
end; |
| 754 |
dmAdd: |
| 755 |
if Source.PremultipliedAlpha then |
| 756 |
begin |
| 757 |
for DestY := Y to Y + SourceHeight - 1 do |
| 758 |
begin |
| 759 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 760 |
PDest := PixelPtr(X, DestY); |
| 761 |
for DestX := X to X + SourceWidth - 1 do |
| 762 |
begin |
| 763 |
PDest^.X := AddBytesPremultiplied(PDest^.X, PSource^.X); |
| 764 |
PDest^.Y := AddBytesPremultiplied(PDest^.Y, PSource^.Y); |
| 765 |
PDest^.Z := AddBytesPremultiplied(PDest^.Z, PSource^.Z); |
| 766 |
Inc(PSource); |
| 767 |
Inc(PDest); |
| 768 |
end; |
| 769 |
end; |
| 770 |
end else |
| 771 |
begin |
| 772 |
for DestY := Y to Y + SourceHeight - 1 do |
| 773 |
begin |
| 774 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 775 |
PDest := PixelPtr(X, DestY); |
| 776 |
for DestX := X to X + SourceWidth - 1 do |
| 777 |
begin |
| 778 |
PDest^.X := AddBytes(PDest^.X, PSource^.X, PSource^.W); |
| 779 |
PDest^.Y := AddBytes(PDest^.Y, PSource^.Y, PSource^.W); |
| 780 |
PDest^.Z := AddBytes(PDest^.Z, PSource^.Z, PSource^.W); |
| 781 |
Inc(PSource); |
| 782 |
Inc(PDest); |
| 783 |
end; |
| 784 |
end; |
| 785 |
end; |
| 786 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 787 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 788 |
{$endif} |
| 789 |
end; |
| 790 |
end; |
| 791 |
|
| 792 |
procedure SourceGrayscaleTreatedAsAlpha(Source: TGrayscaleImage); |
| 793 |
var |
| 794 |
PSource: PByte; |
| 795 |
PDest: PVector3Byte; |
| 796 |
DestX, DestY: Integer; |
| 797 |
begin |
| 798 |
case Mode of |
| 799 |
dmBlend, dmBlendSmart: |
| 800 |
for DestY := Y to Y + SourceHeight - 1 do |
| 801 |
begin |
| 802 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 803 |
PDest := PixelPtr(X, DestY); |
| 804 |
for DestX := X to X + SourceWidth - 1 do |
| 805 |
begin |
| 806 |
PDest^.X := BlendBytes(PDest^.X, Source.ColorWhenTreatedAsAlpha.X, PSource^); |
| 807 |
PDest^.Y := BlendBytes(PDest^.Y, Source.ColorWhenTreatedAsAlpha.Y, PSource^); |
| 808 |
PDest^.Z := BlendBytes(PDest^.Z, Source.ColorWhenTreatedAsAlpha.Z, PSource^); |
| 809 |
Inc(PSource); |
| 810 |
Inc(PDest); |
| 811 |
end; |
| 812 |
end; |
| 813 |
dmMultiply: |
| 814 |
begin |
| 815 |
for DestY := Y to Y + SourceHeight - 1 do |
| 816 |
begin |
| 817 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 818 |
PDest := PixelPtr(X, DestY); |
| 819 |
for DestX := X to X + SourceWidth - 1 do |
| 820 |
begin |
| 821 |
PDest^.X := MultiplyBytesOpaque(PDest^.X, Source.ColorWhenTreatedAsAlpha.X, PSource^); |
| 822 |
PDest^.Y := MultiplyBytesOpaque(PDest^.Y, Source.ColorWhenTreatedAsAlpha.Y, PSource^); |
| 823 |
PDest^.Z := MultiplyBytesOpaque(PDest^.Z, Source.ColorWhenTreatedAsAlpha.Z, PSource^); |
| 824 |
Inc(PSource); |
| 825 |
Inc(PDest); |
| 826 |
end; |
| 827 |
end; |
| 828 |
end; |
| 829 |
dmOverwrite: |
| 830 |
begin |
| 831 |
for DestY := Y to Y + SourceHeight - 1 do |
| 832 |
begin |
| 833 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 834 |
PDest := PixelPtr(X, DestY); |
| 835 |
for DestX := X to X + SourceWidth - 1 do |
| 836 |
begin |
| 837 |
PDest^.X := Source.ColorWhenTreatedAsAlpha.X; |
| 838 |
PDest^.Y := Source.ColorWhenTreatedAsAlpha.Y; |
| 839 |
PDest^.Z := Source.ColorWhenTreatedAsAlpha.Z; |
| 840 |
Inc(PSource); |
| 841 |
Inc(PDest); |
| 842 |
end; |
| 843 |
end; |
| 844 |
end; |
| 845 |
dmAdd: |
| 846 |
begin |
| 847 |
for DestY := Y to Y + SourceHeight - 1 do |
| 848 |
begin |
| 849 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 850 |
PDest := PixelPtr(X, DestY); |
| 851 |
for DestX := X to X + SourceWidth - 1 do |
| 852 |
begin |
| 853 |
PDest^.X := AddBytes(PDest^.X, Source.ColorWhenTreatedAsAlpha.X, PSource^); |
| 854 |
PDest^.Y := AddBytes(PDest^.Y, Source.ColorWhenTreatedAsAlpha.Y, PSource^); |
| 855 |
PDest^.Z := AddBytes(PDest^.Z, Source.ColorWhenTreatedAsAlpha.Z, PSource^); |
| 856 |
Inc(PSource); |
| 857 |
Inc(PDest); |
| 858 |
end; |
| 859 |
end; |
| 860 |
end; |
| 861 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 862 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 863 |
{$endif} |
| 864 |
end; |
| 865 |
end; |
| 866 |
|
| 867 |
procedure SourceGrayscaleAlpha(Source: TGrayscaleAlphaImage); |
| 868 |
var |
| 869 |
PSource: PVector2Byte; |
| 870 |
PDest: PVector3Byte; |
| 871 |
DestX, DestY: Integer; |
| 872 |
begin |
| 873 |
case Mode of |
| 874 |
dmBlend, dmBlendSmart: |
| 875 |
for DestY := Y to Y + SourceHeight - 1 do |
| 876 |
begin |
| 877 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 878 |
PDest := PixelPtr(X, DestY); |
| 879 |
for DestX := X to X + SourceWidth - 1 do |
| 880 |
begin |
| 881 |
PDest^.X := BlendBytes(PDest^.X, PSource^.X, PSource^.Y); |
| 882 |
PDest^.Y := BlendBytes(PDest^.Y, PSource^.X, PSource^.Y); |
| 883 |
PDest^.Z := BlendBytes(PDest^.Z, PSource^.X, PSource^.Y); |
| 884 |
Inc(PSource); |
| 885 |
Inc(PDest); |
| 886 |
end; |
| 887 |
end; |
| 888 |
dmMultiply: |
| 889 |
begin |
| 890 |
for DestY := Y to Y + SourceHeight - 1 do |
| 891 |
begin |
| 892 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 893 |
PDest := PixelPtr(X, DestY); |
| 894 |
for DestX := X to X + SourceWidth - 1 do |
| 895 |
begin |
| 896 |
PDest^.X := MultiplyBytesOpaque(PDest^.X, PSource^.X, PSource^.Y); |
| 897 |
PDest^.Y := MultiplyBytesOpaque(PDest^.Y, PSource^.X, PSource^.Y); |
| 898 |
PDest^.Z := MultiplyBytesOpaque(PDest^.Z, PSource^.X, PSource^.Y); |
| 899 |
Inc(PSource); |
| 900 |
Inc(PDest); |
| 901 |
end; |
| 902 |
end; |
| 903 |
end; |
| 904 |
dmOverwrite: |
| 905 |
begin |
| 906 |
for DestY := Y to Y + SourceHeight - 1 do |
| 907 |
begin |
| 908 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 909 |
PDest := PixelPtr(X, DestY); |
| 910 |
for DestX := X to X + SourceWidth - 1 do |
| 911 |
begin |
| 912 |
PDest^.X := PSource^.X; |
| 913 |
PDest^.Y := PSource^.X; |
| 914 |
PDest^.Z := PSource^.X; |
| 915 |
Inc(PSource); |
| 916 |
Inc(PDest); |
| 917 |
end; |
| 918 |
end; |
| 919 |
end; |
| 920 |
dmAdd: |
| 921 |
{if Source.PremultipliedAlpha then |
| 922 |
begin |
| 923 |
for DestY := Y to Y + SourceHeight - 1 do |
| 924 |
begin |
| 925 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 926 |
PDest := PixelPtr(X, DestY); |
| 927 |
for DestX := X to X + SourceWidth - 1 do |
| 928 |
begin |
| 929 |
PDest^.X := AddBytesPremultiplied(PDest^.X, PSource^.X); |
| 930 |
PDest^.Y := AddBytesPremultiplied(PDest^.Y, PSource^.X); |
| 931 |
PDest^.Z := AddBytesPremultiplied(PDest^.Z, PSource^.X); |
| 932 |
Inc(PSource); |
| 933 |
Inc(PDest); |
| 934 |
end; |
| 935 |
end; |
| 936 |
end else} |
| 937 |
begin |
| 938 |
for DestY := Y to Y + SourceHeight - 1 do |
| 939 |
begin |
| 940 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 941 |
PDest := PixelPtr(X, DestY); |
| 942 |
for DestX := X to X + SourceWidth - 1 do |
| 943 |
begin |
| 944 |
PDest^.X := AddBytes(PDest^.X, PSource^.X, PSource^.Y); |
| 945 |
PDest^.Y := AddBytes(PDest^.Y, PSource^.X, PSource^.Y); |
| 946 |
PDest^.Z := AddBytes(PDest^.Z, PSource^.X, PSource^.Y); |
| 947 |
Inc(PSource); |
| 948 |
Inc(PDest); |
| 949 |
end; |
| 950 |
end; |
| 951 |
end; |
| 952 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 953 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 954 |
{$endif} |
| 955 |
end; |
| 956 |
end; |
| 957 |
|
| 958 |
procedure SourceGrayscale(Source: TGrayscaleImage); |
| 959 |
var |
| 960 |
PSource: PByte; |
| 961 |
PDest: PVector3Byte; |
| 962 |
DestX, DestY: Integer; |
| 963 |
begin |
| 964 |
case Mode of |
| 965 |
dmBlend: |
| 966 |
for DestY := Y to Y + SourceHeight - 1 do |
| 967 |
begin |
| 968 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 969 |
PDest := PixelPtr(X, DestY); |
| 970 |
for DestX := X to X + SourceWidth - 1 do |
| 971 |
begin |
| 972 |
PDest^.X := PSource^; |
| 973 |
PDest^.Y := PSource^; |
| 974 |
PDest^.Z := PSource^; |
| 975 |
Inc(PSource); |
| 976 |
Inc(PDest); |
| 977 |
end; |
| 978 |
end; |
| 979 |
dmMultiply: |
| 980 |
begin |
| 981 |
for DestY := Y to Y + SourceHeight - 1 do |
| 982 |
begin |
| 983 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 984 |
PDest := PixelPtr(X, DestY); |
| 985 |
for DestX := X to X + SourceWidth - 1 do |
| 986 |
begin |
| 987 |
PDest^.X := MultiplyBytes(PDest^.X, PSource^); |
| 988 |
PDest^.Y := MultiplyBytes(PDest^.Y, PSource^); |
| 989 |
PDest^.Z := MultiplyBytes(PDest^.Z, PSource^); |
| 990 |
Inc(PSource); |
| 991 |
Inc(PDest); |
| 992 |
end; |
| 993 |
end; |
| 994 |
end; |
| 995 |
dmOverwrite: |
| 996 |
begin |
| 997 |
for DestY := Y to Y + SourceHeight - 1 do |
| 998 |
begin |
| 999 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1000 |
PDest := PixelPtr(X, DestY); |
| 1001 |
for DestX := X to X + SourceWidth - 1 do |
| 1002 |
begin |
| 1003 |
PDest^.X := PSource^; |
| 1004 |
PDest^.Y := PSource^; |
| 1005 |
PDest^.Z := PSource^; |
| 1006 |
Inc(PSource); |
| 1007 |
Inc(PDest); |
| 1008 |
end; |
| 1009 |
end; |
| 1010 |
end; |
| 1011 |
dmAdd: |
| 1012 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1013 |
begin |
| 1014 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1015 |
PDest := PixelPtr(X, DestY); |
| 1016 |
for DestX := X to X + SourceWidth - 1 do |
| 1017 |
begin |
| 1018 |
PDest^.X := AddBytesPremultiplied(PDest^.X, PSource^); |
| 1019 |
PDest^.Y := AddBytesPremultiplied(PDest^.Y, PSource^); |
| 1020 |
PDest^.Z := AddBytesPremultiplied(PDest^.Z, PSource^); |
| 1021 |
Inc(PSource); |
| 1022 |
Inc(PDest); |
| 1023 |
end; |
| 1024 |
end; |
| 1025 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 1026 |
end; |
| 1027 |
end; |
| 1028 |
|
| 1029 |
begin |
| 1030 |
if (Source is TRGBImage) and ((Mode = dmAdd) or (Mode = dmMultiply)) then |
| 1031 |
SourceRGB(TRGBImage(Source)) else |
| 1032 |
if Source is TRGBAlphaImage then |
| 1033 |
SourceRGBAlpha(TRGBAlphaImage(Source)) else |
| 1034 |
if Source is TGrayscaleAlphaImage then |
| 1035 |
SourceGrayscaleAlpha(TGrayscaleAlphaImage(Source)) else |
| 1036 |
if Source is TGrayscaleImage then |
| 1037 |
begin |
| 1038 |
if TGrayscaleImage(Source).TreatAsAlpha then |
| 1039 |
SourceGrayscaleTreatedAsAlpha(TGrayscaleImage(Source)) |
| 1040 |
else |
| 1041 |
SourceGrayscale(TGrayscaleImage(Source)); |
| 1042 |
end else |
| 1043 |
inherited; |
| 1044 |
end; |
| 1045 |
|
| 1046 |
{ Drawing when TGrayscaleImage is a destination ------------------------------ } |
| 1047 |
|
| 1048 |
procedure TGrayscaleImage.DrawFromCore(Source: TCastleImage; |
| 1049 |
X, Y, SourceX, SourceY, SourceWidth, SourceHeight: Integer; |
| 1050 |
const Mode: TDrawMode); |
| 1051 |
|
| 1052 |
procedure SourceRGBAlpha(Source: TRGBAlphaImage); |
| 1053 |
var |
| 1054 |
PSource: PVector4Byte; |
| 1055 |
PDest: PByte; |
| 1056 |
DestX, DestY: Integer; |
| 1057 |
begin |
| 1058 |
case Mode of |
| 1059 |
dmBlend, dmBlendSmart: |
| 1060 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1061 |
begin |
| 1062 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1063 |
PDest := PixelPtr(X, DestY); |
| 1064 |
for DestX := X to X + SourceWidth - 1 do |
| 1065 |
begin |
| 1066 |
PDest^ := BlendBytes(PDest^, GrayscaleValue(PSource^), PSource^.W); |
| 1067 |
Inc(PSource); |
| 1068 |
Inc(PDest); |
| 1069 |
end; |
| 1070 |
end; |
| 1071 |
dmMultiply: |
| 1072 |
begin |
| 1073 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1074 |
begin |
| 1075 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1076 |
PDest := PixelPtr(X, DestY); |
| 1077 |
for DestX := X to X + SourceWidth - 1 do |
| 1078 |
begin |
| 1079 |
PDest^ := MultiplyBytesOpaque(PDest^, GrayscaleValue(PSource^), PSource^.W); |
| 1080 |
Inc(PSource); |
| 1081 |
Inc(PDest); |
| 1082 |
end; |
| 1083 |
end; |
| 1084 |
end; |
| 1085 |
dmOverwrite: |
| 1086 |
begin |
| 1087 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1088 |
begin |
| 1089 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1090 |
PDest := PixelPtr(X, DestY); |
| 1091 |
for DestX := X to X + SourceWidth - 1 do |
| 1092 |
begin |
| 1093 |
PDest^ := GrayscaleValue(PSource^); |
| 1094 |
Inc(PSource); |
| 1095 |
Inc(PDest); |
| 1096 |
end; |
| 1097 |
end; |
| 1098 |
end; |
| 1099 |
dmAdd: |
| 1100 |
if Source.PremultipliedAlpha then |
| 1101 |
begin |
| 1102 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1103 |
begin |
| 1104 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1105 |
PDest := PixelPtr(X, DestY); |
| 1106 |
for DestX := X to X + SourceWidth - 1 do |
| 1107 |
begin |
| 1108 |
PDest^ := AddBytesPremultiplied(PDest^, GrayscaleValue(PSource^)); |
| 1109 |
Inc(PSource); |
| 1110 |
Inc(PDest); |
| 1111 |
end; |
| 1112 |
end; |
| 1113 |
end else |
| 1114 |
begin |
| 1115 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1116 |
begin |
| 1117 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1118 |
PDest := PixelPtr(X, DestY); |
| 1119 |
for DestX := X to X + SourceWidth - 1 do |
| 1120 |
begin |
| 1121 |
PDest^ := AddBytes(PDest^, GrayscaleValue(PSource^), PSource^.W); |
| 1122 |
Inc(PSource); |
| 1123 |
Inc(PDest); |
| 1124 |
end; |
| 1125 |
end; |
| 1126 |
end; |
| 1127 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 1128 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 1129 |
{$endif} |
| 1130 |
end; |
| 1131 |
end; |
| 1132 |
|
| 1133 |
procedure SourceGrayscaleTreatedAsAlpha(Source: TGrayscaleImage); |
| 1134 |
var |
| 1135 |
PSource: PByte; |
| 1136 |
PDest: PByte; |
| 1137 |
DestX, DestY: Integer; |
| 1138 |
begin |
| 1139 |
case Mode of |
| 1140 |
dmBlend, dmBlendSmart: |
| 1141 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1142 |
begin |
| 1143 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1144 |
PDest := PixelPtr(X, DestY); |
| 1145 |
for DestX := X to X + SourceWidth - 1 do |
| 1146 |
begin |
| 1147 |
PDest^ := BlendBytes(PDest^, GrayscaleValue(Source.ColorWhenTreatedAsAlpha), PSource^); |
| 1148 |
Inc(PSource); |
| 1149 |
Inc(PDest); |
| 1150 |
end; |
| 1151 |
end; |
| 1152 |
dmMultiply: |
| 1153 |
begin |
| 1154 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1155 |
begin |
| 1156 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1157 |
PDest := PixelPtr(X, DestY); |
| 1158 |
for DestX := X to X + SourceWidth - 1 do |
| 1159 |
begin |
| 1160 |
PDest^ := MultiplyBytesOpaque(PDest^, GrayscaleValue(Source.ColorWhenTreatedAsAlpha), PSource^); |
| 1161 |
Inc(PSource); |
| 1162 |
Inc(PDest); |
| 1163 |
end; |
| 1164 |
end; |
| 1165 |
end; |
| 1166 |
dmOverwrite: |
| 1167 |
begin |
| 1168 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1169 |
begin |
| 1170 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1171 |
PDest := PixelPtr(X, DestY); |
| 1172 |
for DestX := X to X + SourceWidth - 1 do |
| 1173 |
begin |
| 1174 |
PDest^ := GrayscaleValue(Source.ColorWhenTreatedAsAlpha); |
| 1175 |
Inc(PSource); |
| 1176 |
Inc(PDest); |
| 1177 |
end; |
| 1178 |
end; |
| 1179 |
end; |
| 1180 |
dmAdd: |
| 1181 |
begin |
| 1182 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1183 |
begin |
| 1184 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1185 |
PDest := PixelPtr(X, DestY); |
| 1186 |
for DestX := X to X + SourceWidth - 1 do |
| 1187 |
begin |
| 1188 |
PDest^ := AddBytes(PDest^, GrayscaleValue(Source.ColorWhenTreatedAsAlpha), PSource^); |
| 1189 |
Inc(PSource); |
| 1190 |
Inc(PDest); |
| 1191 |
end; |
| 1192 |
end; |
| 1193 |
end; |
| 1194 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 1195 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 1196 |
{$endif} |
| 1197 |
end; |
| 1198 |
end; |
| 1199 |
|
| 1200 |
procedure SourceGrayscaleAlpha(Source: TGrayscaleAlphaImage); |
| 1201 |
var |
| 1202 |
PSource: PVector2Byte; |
| 1203 |
PDest: PByte; |
| 1204 |
DestX, DestY: Integer; |
| 1205 |
begin |
| 1206 |
case Mode of |
| 1207 |
dmBlend, dmBlendSmart: |
| 1208 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1209 |
begin |
| 1210 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1211 |
PDest := PixelPtr(X, DestY); |
| 1212 |
for DestX := X to X + SourceWidth - 1 do |
| 1213 |
begin |
| 1214 |
PDest^ := BlendBytes(PDest^, PSource^.X, PSource^.Y); |
| 1215 |
Inc(PSource); |
| 1216 |
Inc(PDest); |
| 1217 |
end; |
| 1218 |
end; |
| 1219 |
dmMultiply: |
| 1220 |
begin |
| 1221 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1222 |
begin |
| 1223 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1224 |
PDest := PixelPtr(X, DestY); |
| 1225 |
for DestX := X to X + SourceWidth - 1 do |
| 1226 |
begin |
| 1227 |
PDest^ := MultiplyBytesOpaque(PDest^, PSource^.X, PSource^.Y); |
| 1228 |
Inc(PSource); |
| 1229 |
Inc(PDest); |
| 1230 |
end; |
| 1231 |
end; |
| 1232 |
end; |
| 1233 |
dmOverwrite: |
| 1234 |
begin |
| 1235 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1236 |
begin |
| 1237 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1238 |
PDest := PixelPtr(X, DestY); |
| 1239 |
for DestX := X to X + SourceWidth - 1 do |
| 1240 |
begin |
| 1241 |
PDest^ := PSource^.X; |
| 1242 |
Inc(PSource); |
| 1243 |
Inc(PDest); |
| 1244 |
end; |
| 1245 |
end; |
| 1246 |
end; |
| 1247 |
dmAdd: |
| 1248 |
{if Source.PremultipliedAlpha then |
| 1249 |
begin |
| 1250 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1251 |
begin |
| 1252 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1253 |
PDest := PixelPtr(X, DestY); |
| 1254 |
for DestX := X to X + SourceWidth - 1 do |
| 1255 |
begin |
| 1256 |
PDest^ := AddBytesPremultiplied(PDest^, PSource^.X); |
| 1257 |
Inc(PSource); |
| 1258 |
Inc(PDest); |
| 1259 |
end; |
| 1260 |
end; |
| 1261 |
end else} |
| 1262 |
begin |
| 1263 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1264 |
begin |
| 1265 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1266 |
PDest := PixelPtr(X, DestY); |
| 1267 |
for DestX := X to X + SourceWidth - 1 do |
| 1268 |
begin |
| 1269 |
PDest^ := AddBytes(PDest^, PSource^.X, PSource^.Y); |
| 1270 |
Inc(PSource); |
| 1271 |
Inc(PDest); |
| 1272 |
end; |
| 1273 |
end; |
| 1274 |
end; |
| 1275 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 1276 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 1277 |
{$endif} |
| 1278 |
end; |
| 1279 |
end; |
| 1280 |
|
| 1281 |
procedure SourceGrayscale(Source: TGrayscaleImage); |
| 1282 |
var |
| 1283 |
PSource: PByte; |
| 1284 |
PDest: PByte; |
| 1285 |
DestX, DestY: Integer; |
| 1286 |
begin |
| 1287 |
case Mode of |
| 1288 |
dmBlend, dmBlendSmart: |
| 1289 |
inherited DrawFromCore(Source, X, Y, SourceX, SourceY, SourceWidth, SourceHeight, Mode); |
| 1290 |
{ For some reason, simply stating "inherited;" does something different? |
| 1291 |
Uncovered by tests/testcastleimagesdraw.pas . } |
| 1292 |
//inherited; |
| 1293 |
dmMultiply: |
| 1294 |
begin |
| 1295 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1296 |
begin |
| 1297 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1298 |
PDest := PixelPtr(X, DestY); |
| 1299 |
for DestX := X to X + SourceWidth - 1 do |
| 1300 |
begin |
| 1301 |
PDest^ := MultiplyBytes(PDest^, PSource^); |
| 1302 |
Inc(PSource); |
| 1303 |
Inc(PDest); |
| 1304 |
end; |
| 1305 |
end; |
| 1306 |
end; |
| 1307 |
dmOverwrite: inherited DrawFromCore(Source, X, Y, SourceX, SourceY, SourceWidth, SourceHeight, Mode); //handled by TCastleImage.DrawFromCore |
| 1308 |
dmAdd: |
| 1309 |
{if Source.PremultipliedAlpha then |
| 1310 |
begin |
| 1311 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1312 |
begin |
| 1313 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1314 |
PDest := PixelPtr(X, DestY); |
| 1315 |
for DestX := X to X + SourceWidth - 1 do |
| 1316 |
begin |
| 1317 |
PDest^ := AddBytesPremultiplied(PDest^, PSource^.X); |
| 1318 |
Inc(PSource); |
| 1319 |
Inc(PDest); |
| 1320 |
end; |
| 1321 |
end; |
| 1322 |
end else} |
| 1323 |
begin |
| 1324 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1325 |
begin |
| 1326 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1327 |
PDest := PixelPtr(X, DestY); |
| 1328 |
for DestX := X to X + SourceWidth - 1 do |
| 1329 |
begin |
| 1330 |
PDest^ := AddBytesPremultiplied(PDest^, PSource^); |
| 1331 |
Inc(PSource); |
| 1332 |
Inc(PDest); |
| 1333 |
end; |
| 1334 |
end; |
| 1335 |
end; |
| 1336 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 1337 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 1338 |
{$endif} |
| 1339 |
end; |
| 1340 |
end; |
| 1341 |
|
| 1342 |
procedure SourceRGB(Source: TRGBImage); |
| 1343 |
var |
| 1344 |
PSource: PVector3Byte; |
| 1345 |
PDest: PByte; |
| 1346 |
DestX, DestY: Integer; |
| 1347 |
begin |
| 1348 |
case Mode of |
| 1349 |
dmBlend: |
| 1350 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1351 |
begin |
| 1352 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1353 |
PDest := PixelPtr(X, DestY); |
| 1354 |
for DestX := X to X + SourceWidth - 1 do |
| 1355 |
begin |
| 1356 |
PDest^ := GrayscaleValue(PSource^); |
| 1357 |
Inc(PSource); |
| 1358 |
Inc(PDest); |
| 1359 |
end; |
| 1360 |
end; |
| 1361 |
dmMultiply: |
| 1362 |
begin |
| 1363 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1364 |
begin |
| 1365 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1366 |
PDest := PixelPtr(X, DestY); |
| 1367 |
for DestX := X to X + SourceWidth - 1 do |
| 1368 |
begin |
| 1369 |
PDest^ := MultiplyBytes(PDest^, GrayscaleValue(PSource^)); |
| 1370 |
Inc(PSource); |
| 1371 |
Inc(PDest); |
| 1372 |
end; |
| 1373 |
end; |
| 1374 |
end; |
| 1375 |
dmOverwrite: |
| 1376 |
begin |
| 1377 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1378 |
begin |
| 1379 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1380 |
PDest := PixelPtr(X, DestY); |
| 1381 |
for DestX := X to X + SourceWidth - 1 do |
| 1382 |
begin |
| 1383 |
PDest^ := GrayscaleValue(PSource^); |
| 1384 |
Inc(PSource); |
| 1385 |
Inc(PDest); |
| 1386 |
end; |
| 1387 |
end; |
| 1388 |
end; |
| 1389 |
dmAdd: |
| 1390 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1391 |
begin |
| 1392 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1393 |
PDest := PixelPtr(X, DestY); |
| 1394 |
for DestX := X to X + SourceWidth - 1 do |
| 1395 |
begin |
| 1396 |
PDest^ := AddBytesPremultiplied(PDest^, GrayscaleValue(PSource^)); |
| 1397 |
Inc(PSource); |
| 1398 |
Inc(PDest); |
| 1399 |
end; |
| 1400 |
end; |
| 1401 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 1402 |
end; |
| 1403 |
end; |
| 1404 |
|
| 1405 |
begin |
| 1406 |
if Source is TRGBAlphaImage then |
| 1407 |
SourceRGBAlpha(TRGBAlphaImage(Source)) else |
| 1408 |
if Source is TGrayscaleImage then |
| 1409 |
begin |
| 1410 |
if TGrayscaleImage(Source).TreatAsAlpha then |
| 1411 |
SourceGrayscaleTreatedAsAlpha(TGrayscaleImage(Source)) |
| 1412 |
else |
| 1413 |
SourceGrayscale(TGrayscaleImage(Source)); |
| 1414 |
end else |
| 1415 |
if Source is TGrayscaleAlphaImage then |
| 1416 |
SourceGrayscaleAlpha(TGrayscaleAlphaImage(Source)) else |
| 1417 |
if Source is TRGBImage then |
| 1418 |
SourceRGB(TRGBImage(Source)) else |
| 1419 |
inherited; |
| 1420 |
end; |
| 1421 |
|
| 1422 |
{ Drawing when TGrayscaleAlphaImage is a destination ------------------------------- } |
| 1423 |
|
| 1424 |
procedure TGrayscaleAlphaImage.DrawFromCore(Source: TCastleImage; |
| 1425 |
X, Y, SourceX, SourceY, SourceWidth, SourceHeight: Integer; |
| 1426 |
const Mode: TDrawMode); |
| 1427 |
|
| 1428 |
procedure SourceRGB(Source: TRGBImage); |
| 1429 |
var |
| 1430 |
PSource: PVector3Byte; |
| 1431 |
PDest: PVector2Byte; |
| 1432 |
DestX, DestY: Integer; |
| 1433 |
begin |
| 1434 |
case Mode of |
| 1435 |
dmBlend: |
| 1436 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1437 |
begin |
| 1438 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1439 |
PDest := PixelPtr(X, DestY); |
| 1440 |
for DestX := X to X + SourceWidth - 1 do |
| 1441 |
begin |
| 1442 |
PDest^.X := GrayscaleValue(PSource^); |
| 1443 |
Inc(PSource); |
| 1444 |
Inc(PDest); |
| 1445 |
end; |
| 1446 |
end; |
| 1447 |
dmMultiply: |
| 1448 |
begin |
| 1449 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1450 |
begin |
| 1451 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1452 |
PDest := PixelPtr(X, DestY); |
| 1453 |
for DestX := X to X + SourceWidth - 1 do |
| 1454 |
begin |
| 1455 |
PDest^.X := MultiplyBytes(PDest^.X, GrayscaleValue(PSource^)); |
| 1456 |
Inc(PSource); |
| 1457 |
Inc(PDest); |
| 1458 |
end; |
| 1459 |
end; |
| 1460 |
end; |
| 1461 |
dmOverwrite: |
| 1462 |
begin |
| 1463 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1464 |
begin |
| 1465 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1466 |
PDest := PixelPtr(X, DestY); |
| 1467 |
for DestX := X to X + SourceWidth - 1 do |
| 1468 |
begin |
| 1469 |
PDest^.X := GrayscaleValue(PSource^); |
| 1470 |
PDest^.Y := 255; |
| 1471 |
Inc(PSource); |
| 1472 |
Inc(PDest); |
| 1473 |
end; |
| 1474 |
end; |
| 1475 |
end; |
| 1476 |
dmAdd: |
| 1477 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1478 |
begin |
| 1479 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1480 |
PDest := PixelPtr(X, DestY); |
| 1481 |
for DestX := X to X + SourceWidth - 1 do |
| 1482 |
begin |
| 1483 |
PDest^.X := AddBytesPremultiplied(PDest^.X, GrayscaleValue(PSource^)); |
| 1484 |
Inc(PSource); |
| 1485 |
Inc(PDest); |
| 1486 |
end; |
| 1487 |
end; |
| 1488 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 1489 |
end; |
| 1490 |
end; |
| 1491 |
|
| 1492 |
procedure SourceRGBAlpha(Source: TRGBAlphaImage); |
| 1493 |
var |
| 1494 |
PSource: PVector4Byte; |
| 1495 |
PDest: PVector2Byte; |
| 1496 |
DestX, DestY: Integer; |
| 1497 |
// variables for smart-blending mode |
| 1498 |
Alpha1d, Alpha2, AlphaSum: Single; |
| 1499 |
begin |
| 1500 |
case Mode of |
| 1501 |
dmBlend: |
| 1502 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1503 |
begin |
| 1504 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1505 |
PDest := PixelPtr(X, DestY); |
| 1506 |
for DestX := X to X + SourceWidth - 1 do |
| 1507 |
begin |
| 1508 |
PDest^.X := BlendBytes(PDest^.X, GrayscaleValue(PSource^), PSource^.W); |
| 1509 |
Inc(PSource); |
| 1510 |
Inc(PDest); |
| 1511 |
end; |
| 1512 |
end; |
| 1513 |
dmBlendSmart: |
| 1514 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1515 |
begin |
| 1516 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1517 |
PDest := PixelPtr(X, DestY); |
| 1518 |
for DestX := X to X + SourceWidth - 1 do |
| 1519 |
begin |
| 1520 |
BlendSmartParameters(PDest^.Y, PSource^.W, Alpha1d, Alpha2, AlphaSum); |
| 1521 |
if AlphaSum > 0 then |
| 1522 |
begin |
| 1523 |
PDest^.X := Round((PDest^.X * Alpha1d + GrayscaleValue(PSource^) * Alpha2) / AlphaSum); |
| 1524 |
PDest^.Y := Round(255 * AlphaSum); |
| 1525 |
end; |
| 1526 |
Inc(PSource); |
| 1527 |
Inc(PDest); |
| 1528 |
end; |
| 1529 |
end; |
| 1530 |
dmMultiply: |
| 1531 |
begin |
| 1532 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1533 |
begin |
| 1534 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1535 |
PDest := PixelPtr(X, DestY); |
| 1536 |
for DestX := X to X + SourceWidth - 1 do |
| 1537 |
begin |
| 1538 |
PDest^.X := MultiplyBytes(PDest^.X, GrayscaleValue(PSource^)); |
| 1539 |
PDest^.Y := MultiplyBytes(PDest^.Y, PSource^.W); |
| 1540 |
Inc(PSource); |
| 1541 |
Inc(PDest); |
| 1542 |
end; |
| 1543 |
end; |
| 1544 |
end; |
| 1545 |
dmOverwrite: |
| 1546 |
begin |
| 1547 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1548 |
begin |
| 1549 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1550 |
PDest := PixelPtr(X, DestY); |
| 1551 |
for DestX := X to X + SourceWidth - 1 do |
| 1552 |
begin |
| 1553 |
PDest^.X := GrayscaleValue(PSource^); |
| 1554 |
PDest^.Y := PSource^.W; |
| 1555 |
Inc(PSource); |
| 1556 |
Inc(PDest); |
| 1557 |
end; |
| 1558 |
end; |
| 1559 |
end; |
| 1560 |
dmAdd: |
| 1561 |
if Source.PremultipliedAlpha then |
| 1562 |
begin |
| 1563 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1564 |
begin |
| 1565 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1566 |
PDest := PixelPtr(X, DestY); |
| 1567 |
for DestX := X to X + SourceWidth - 1 do |
| 1568 |
begin |
| 1569 |
PDest^.X := AddBytesPremultiplied(PDest^.X, GrayscaleValue(PSource^)); |
| 1570 |
Inc(PSource); |
| 1571 |
Inc(PDest); |
| 1572 |
end; |
| 1573 |
end; |
| 1574 |
end else |
| 1575 |
begin |
| 1576 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1577 |
begin |
| 1578 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1579 |
PDest := PixelPtr(X, DestY); |
| 1580 |
for DestX := X to X + SourceWidth - 1 do |
| 1581 |
begin |
| 1582 |
PDest^.X := AddBytes(PDest^.X, GrayscaleValue(PSource^), PSource^.W); |
| 1583 |
Inc(PSource); |
| 1584 |
Inc(PDest); |
| 1585 |
end; |
| 1586 |
end; |
| 1587 |
end; |
| 1588 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 1589 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 1590 |
{$endif} |
| 1591 |
end; |
| 1592 |
end; |
| 1593 |
|
| 1594 |
procedure SourceGrayscaleAlpha(Source: TGrayscaleAlphaImage); |
| 1595 |
var |
| 1596 |
PSource: PVector2Byte; |
| 1597 |
PDest: PVector2Byte; |
| 1598 |
DestX, DestY: Integer; |
| 1599 |
// variables for smart-blending mode |
| 1600 |
Alpha1d, Alpha2, AlphaSum: Single; |
| 1601 |
begin |
| 1602 |
case Mode of |
| 1603 |
dmBlend: |
| 1604 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1605 |
begin |
| 1606 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1607 |
PDest := PixelPtr(X, DestY); |
| 1608 |
for DestX := X to X + SourceWidth - 1 do |
| 1609 |
begin |
| 1610 |
PDest^.X := BlendBytes(PDest^.X, PSource^.X, PSource^.Y); |
| 1611 |
Inc(PSource); |
| 1612 |
Inc(PDest); |
| 1613 |
end; |
| 1614 |
end; |
| 1615 |
dmBlendSmart: |
| 1616 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1617 |
begin |
| 1618 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1619 |
PDest := PixelPtr(X, DestY); |
| 1620 |
for DestX := X to X + SourceWidth - 1 do |
| 1621 |
begin |
| 1622 |
BlendSmartParameters(PDest^.Y, PSource^.Y, Alpha1d, Alpha2, AlphaSum); |
| 1623 |
if AlphaSum > 0 then |
| 1624 |
begin |
| 1625 |
PDest^.X := Round((PDest^.X * Alpha1d + PSource^.X * Alpha2) / AlphaSum); |
| 1626 |
PDest^.Y := Round(255 * AlphaSum); |
| 1627 |
end; |
| 1628 |
Inc(PSource); |
| 1629 |
Inc(PDest); |
| 1630 |
end; |
| 1631 |
end; |
| 1632 |
dmMultiply: |
| 1633 |
begin |
| 1634 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1635 |
begin |
| 1636 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1637 |
PDest := PixelPtr(X, DestY); |
| 1638 |
for DestX := X to X + SourceWidth - 1 do |
| 1639 |
begin |
| 1640 |
PDest^.X := MultiplyBytes(PDest^.X, PSource^.X); |
| 1641 |
PDest^.Y := MultiplyBytes(PDest^.Y, PSource^.Y); |
| 1642 |
Inc(PSource); |
| 1643 |
Inc(PDest); |
| 1644 |
end; |
| 1645 |
end; |
| 1646 |
end; |
| 1647 |
dmOverwrite: inherited DrawFromCore(Source, X, Y, SourceX, SourceY, SourceWidth, SourceHeight, Mode); //handled by TCastleImage.DrawFromCore |
| 1648 |
dmAdd: |
| 1649 |
{if Source.PremultipliedAlpha then |
| 1650 |
begin |
| 1651 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1652 |
begin |
| 1653 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1654 |
PDest := PixelPtr(X, DestY); |
| 1655 |
for DestX := X to X + SourceWidth - 1 do |
| 1656 |
begin |
| 1657 |
PDest^.X := AddBytesPremultiplied(PDest^.X, PSource^.X); |
| 1658 |
Inc(PSource); |
| 1659 |
Inc(PDest); |
| 1660 |
end; |
| 1661 |
end; |
| 1662 |
end else} |
| 1663 |
begin |
| 1664 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1665 |
begin |
| 1666 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1667 |
PDest := PixelPtr(X, DestY); |
| 1668 |
for DestX := X to X + SourceWidth - 1 do |
| 1669 |
begin |
| 1670 |
PDest^.X := AddBytes(PDest^.X, PSource^.X, PSource^.Y); |
| 1671 |
Inc(PSource); |
| 1672 |
Inc(PDest); |
| 1673 |
end; |
| 1674 |
end; |
| 1675 |
end; |
| 1676 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 1677 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 1678 |
{$endif} |
| 1679 |
end; |
| 1680 |
end; |
| 1681 |
|
| 1682 |
procedure SourceGrayscaleTreatedAsAlpha(Source: TGrayscaleImage); |
| 1683 |
var |
| 1684 |
PSource: PByte; |
| 1685 |
PDest: PVector2Byte; |
| 1686 |
DestX, DestY: Integer; |
| 1687 |
// variables for smart-blending mode |
| 1688 |
Alpha1d, Alpha2, AlphaSum: Single; |
| 1689 |
begin |
| 1690 |
case Mode of |
| 1691 |
dmBlend: |
| 1692 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1693 |
begin |
| 1694 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1695 |
PDest := PixelPtr(X, DestY); |
| 1696 |
for DestX := X to X + SourceWidth - 1 do |
| 1697 |
begin |
| 1698 |
PDest^.X := BlendBytes(PDest^.X, GrayscaleValue(Source.ColorWhenTreatedAsAlpha), PSource^); |
| 1699 |
Inc(PSource); |
| 1700 |
Inc(PDest); |
| 1701 |
end; |
| 1702 |
end; |
| 1703 |
dmBlendSmart: |
| 1704 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1705 |
begin |
| 1706 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1707 |
PDest := PixelPtr(X, DestY); |
| 1708 |
for DestX := X to X + SourceWidth - 1 do |
| 1709 |
begin |
| 1710 |
BlendSmartParameters(PDest^.Y, PSource^, Alpha1d, Alpha2, AlphaSum); |
| 1711 |
if AlphaSum > 0 then |
| 1712 |
begin |
| 1713 |
PDest^.X := Round((PDest^.X * Alpha1d + GrayscaleValue(Source.ColorWhenTreatedAsAlpha) * Alpha2) / AlphaSum); |
| 1714 |
PDest^.Y := Round(255 * AlphaSum); |
| 1715 |
end; |
| 1716 |
Inc(PSource); |
| 1717 |
Inc(PDest); |
| 1718 |
end; |
| 1719 |
end; |
| 1720 |
dmMultiply: |
| 1721 |
begin |
| 1722 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1723 |
begin |
| 1724 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1725 |
PDest := PixelPtr(X, DestY); |
| 1726 |
for DestX := X to X + SourceWidth - 1 do |
| 1727 |
begin |
| 1728 |
PDest^.X := MultiplyBytes(PDest^.X, GrayscaleValue(Source.ColorWhenTreatedAsAlpha)); |
| 1729 |
PDest^.Y := MultiplyBytes(PDest^.Y, PSource^); |
| 1730 |
Inc(PSource); |
| 1731 |
Inc(PDest); |
| 1732 |
end; |
| 1733 |
end; |
| 1734 |
end; |
| 1735 |
dmOverwrite: |
| 1736 |
begin |
| 1737 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1738 |
begin |
| 1739 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1740 |
PDest := PixelPtr(X, DestY); |
| 1741 |
for DestX := X to X + SourceWidth - 1 do |
| 1742 |
begin |
| 1743 |
PDest^.X := GrayscaleValue(Source.ColorWhenTreatedAsAlpha); |
| 1744 |
PDest^.Y := PSource^; |
| 1745 |
Inc(PSource); |
| 1746 |
Inc(PDest); |
| 1747 |
end; |
| 1748 |
end; |
| 1749 |
end; |
| 1750 |
dmAdd: |
| 1751 |
begin |
| 1752 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1753 |
begin |
| 1754 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1755 |
PDest := PixelPtr(X, DestY); |
| 1756 |
for DestX := X to X + SourceWidth - 1 do |
| 1757 |
begin |
| 1758 |
PDest^.X := AddBytes(PDest^.X, GrayscaleValue(Source.ColorWhenTreatedAsAlpha), PSource^); |
| 1759 |
Inc(PSource); |
| 1760 |
Inc(PDest); |
| 1761 |
end; |
| 1762 |
end; |
| 1763 |
end; |
| 1764 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 1765 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 1766 |
{$endif} |
| 1767 |
end; |
| 1768 |
end; |
| 1769 |
|
| 1770 |
procedure SourceGrayscale(Source: TGrayscaleImage); |
| 1771 |
var |
| 1772 |
PSource: PByte; |
| 1773 |
PDest: PVector2Byte; |
| 1774 |
DestX, DestY: Integer; |
| 1775 |
begin |
| 1776 |
case Mode of |
| 1777 |
dmBlend: |
| 1778 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1779 |
begin |
| 1780 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1781 |
PDest := PixelPtr(X, DestY); |
| 1782 |
for DestX := X to X + SourceWidth - 1 do |
| 1783 |
begin |
| 1784 |
PDest^.X := PSource^; |
| 1785 |
Inc(PSource); |
| 1786 |
Inc(PDest); |
| 1787 |
end; |
| 1788 |
end; |
| 1789 |
dmMultiply: |
| 1790 |
begin |
| 1791 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1792 |
begin |
| 1793 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1794 |
PDest := PixelPtr(X, DestY); |
| 1795 |
for DestX := X to X + SourceWidth - 1 do |
| 1796 |
begin |
| 1797 |
PDest^.X := MultiplyBytes(PDest^.X, PSource^); |
| 1798 |
//PDest^.Y unchanged |
| 1799 |
Inc(PSource); |
| 1800 |
Inc(PDest); |
| 1801 |
end; |
| 1802 |
end; |
| 1803 |
end; |
| 1804 |
dmOverwrite: |
| 1805 |
begin |
| 1806 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1807 |
begin |
| 1808 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1809 |
PDest := PixelPtr(X, DestY); |
| 1810 |
for DestX := X to X + SourceWidth - 1 do |
| 1811 |
begin |
| 1812 |
PDest^.X := PSource^; |
| 1813 |
PDest^.Y := 255; |
| 1814 |
Inc(PSource); |
| 1815 |
Inc(PDest); |
| 1816 |
end; |
| 1817 |
end; |
| 1818 |
end; |
| 1819 |
dmAdd: |
| 1820 |
for DestY := Y to Y + SourceHeight - 1 do |
| 1821 |
begin |
| 1822 |
PSource := Source.PixelPtr(SourceX, SourceY + DestY - Y); |
| 1823 |
PDest := PixelPtr(X, DestY); |
| 1824 |
for DestX := X to X + SourceWidth - 1 do |
| 1825 |
begin |
| 1826 |
PDest^.X := AddBytesPremultiplied(PDest^.X, PSource^); |
| 1827 |
Inc(PSource); |
| 1828 |
Inc(PDest); |
| 1829 |
end; |
| 1830 |
end; |
| 1831 |
else raise EImageDrawError.CreateFmt(SErrBlendModeNotImplemented, [Source.ClassName, ClassName]); |
| 1832 |
end; |
| 1833 |
end; |
| 1834 |
|
| 1835 |
begin |
| 1836 |
if Source is TRGBImage then |
| 1837 |
SourceRGB(TRGBImage(Source)) else |
| 1838 |
if Source is TRGBAlphaImage then |
| 1839 |
SourceRGBAlpha(TRGBAlphaImage(Source)) else |
| 1840 |
if Source is TGrayscaleAlphaImage then |
| 1841 |
SourceGrayscaleAlpha(TGrayscaleAlphaImage(Source)) else |
| 1842 |
if Source is TGrayscaleImage then |
| 1843 |
begin |
| 1844 |
if TGrayscaleImage(Source).TreatAsAlpha then |
| 1845 |
SourceGrayscaleTreatedAsAlpha(TGrayscaleImage(Source)) |
| 1846 |
else |
| 1847 |
SourceGrayscale(TGrayscaleImage(Source)); |
| 1848 |
end else |
| 1849 |
inherited; |
| 1850 |
end; |