| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestImagesDraw" -*- |
| 2 |
{ |
| 3 |
Copyright 2015-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 |
unit TestCastleImagesDraw; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses CastleTester, CastleImages; |
| 22 |
|
| 23 |
type |
| 24 |
TTestImagesDraw = class(TCastleTestCase) |
| 25 |
private |
| 26 |
Rgb, Rgb2: TRGBImage; |
| 27 |
RgbAlpha, RgbAlpha2: TRGBAlphaImage; |
| 28 |
Gray, Gray2: TGrayscaleImage; |
| 29 |
GrayAlpha, GrayAlpha2: TGrayscaleAlphaImage; |
| 30 |
function BlendBytes(const Dest, Source, Opacity: Byte): Byte; |
| 31 |
//function AddBytes(const Dest, Source, Opacity: Byte): Byte; |
| 32 |
function AddBytesPremultiplied(const Dest, Source: Byte): Byte; |
| 33 |
procedure ResetImages; |
| 34 |
procedure FreeImages; |
| 35 |
protected |
| 36 |
procedure Setup; override; |
| 37 |
procedure TearDown; override; |
| 38 |
published |
| 39 |
procedure TestDrawToRgb; |
| 40 |
procedure TestDrawToRgbAlpha; |
| 41 |
procedure TestDrawToGrayscale; |
| 42 |
procedure TestDrawToGrayscaleAlpha; |
| 43 |
end; |
| 44 |
|
| 45 |
implementation |
| 46 |
|
| 47 |
uses SysUtils, CastleVectors, CastleColors; |
| 48 |
|
| 49 |
function TTestImagesDraw.BlendBytes(const Dest, Source, Opacity: Byte): Byte; {$ifdef FPC}{$ifdef SUPPORTS_INLINE} inline; {$endif}{$endif} |
| 50 |
var |
| 51 |
W: Word; |
| 52 |
begin |
| 53 |
W := |
| 54 |
Word(Dest ) * (255 - Opacity) div 255 + |
| 55 |
Word(Source) * Opacity div 255; |
| 56 |
if W > 255 then W := 255; |
| 57 |
Result := W; |
| 58 |
end; |
| 59 |
|
| 60 |
(* |
| 61 |
function TTestImagesDraw.AddBytes(const Dest, Source, Opacity: Byte): Byte; {$ifdef FPC}{$ifdef SUPPORTS_INLINE} inline; {$endif}{$endif} |
| 62 |
var |
| 63 |
W: Word; |
| 64 |
begin |
| 65 |
W := Dest + Word(Source) * Opacity div 255; |
| 66 |
if W > 255 then W := 255; |
| 67 |
Result := W; |
| 68 |
end; |
| 69 |
*) |
| 70 |
|
| 71 |
function TTestImagesDraw.AddBytesPremultiplied(const Dest, Source: Byte): Byte; {$ifdef FPC}{$ifdef SUPPORTS_INLINE} inline; {$endif}{$endif} |
| 72 |
var |
| 73 |
W: Word; |
| 74 |
begin |
| 75 |
W := Dest + Source; |
| 76 |
if W > 255 then W := 255; |
| 77 |
Result := W; |
| 78 |
end; |
| 79 |
|
| 80 |
procedure TTestImagesDraw.Setup; |
| 81 |
begin |
| 82 |
inherited; |
| 83 |
|
| 84 |
Rgb := nil; |
| 85 |
RgbAlpha := nil; |
| 86 |
Gray := nil; |
| 87 |
GrayAlpha := nil; |
| 88 |
|
| 89 |
ResetImages; |
| 90 |
end; |
| 91 |
|
| 92 |
procedure TTestImagesDraw.TearDown; |
| 93 |
begin |
| 94 |
FreeImages; |
| 95 |
inherited; |
| 96 |
end; |
| 97 |
|
| 98 |
procedure TTestImagesDraw.FreeImages; |
| 99 |
begin |
| 100 |
FreeAndNil(Rgb); |
| 101 |
FreeAndNil(Rgb2); |
| 102 |
FreeAndNil(RgbAlpha); |
| 103 |
FreeAndNil(RgbAlpha2); |
| 104 |
FreeAndNil(Gray); |
| 105 |
FreeAndNil(Gray2); |
| 106 |
FreeAndNil(GrayAlpha); |
| 107 |
FreeAndNil(GrayAlpha2); |
| 108 |
end; |
| 109 |
|
| 110 |
procedure TTestImagesDraw.ResetImages; |
| 111 |
begin |
| 112 |
FreeImages; |
| 113 |
|
| 114 |
Rgb := TRGBImage.Create(2, 2); |
| 115 |
Rgb.PixelPtr(0, 0)^ := Vector3Byte(1, 2, 3); |
| 116 |
Rgb.PixelPtr(0, 1)^ := Vector3Byte(4, 5, 6); |
| 117 |
Rgb.PixelPtr(1, 0)^ := Vector3Byte(11, 22, 33); |
| 118 |
Rgb.PixelPtr(1, 1)^ := Vector3Byte(44, 55, 66); |
| 119 |
|
| 120 |
Rgb2 := Rgb.MakeCopy as TRGBImage; |
| 121 |
|
| 122 |
RgbAlpha := TRGBAlphaImage.Create(2, 2); |
| 123 |
RgbAlpha.PixelPtr(0, 0)^ := Vector4Byte(7, 8, 9, 128); |
| 124 |
RgbAlpha.PixelPtr(0, 1)^ := Vector4Byte(3, 2, 1, 128); |
| 125 |
RgbAlpha.PixelPtr(1, 0)^ := Vector4Byte(77, 88, 99, 128); |
| 126 |
RgbAlpha.PixelPtr(1, 1)^ := Vector4Byte(33, 22, 11, 128); |
| 127 |
|
| 128 |
RgbAlpha2 := RgbAlpha.MakeCopy as TRGBAlphaImage; |
| 129 |
|
| 130 |
Gray := TGrayscaleImage.Create(2, 2); |
| 131 |
Gray.PixelPtr(0, 0)^ := 100; |
| 132 |
Gray.PixelPtr(0, 1)^ := 101; |
| 133 |
Gray.PixelPtr(1, 0)^ := 102; |
| 134 |
Gray.PixelPtr(1, 1)^ := 103; |
| 135 |
|
| 136 |
Gray2 := Gray.MakeCopy as TGrayscaleImage; |
| 137 |
|
| 138 |
GrayAlpha := TGrayscaleAlphaImage.Create(2, 2); |
| 139 |
GrayAlpha.PixelPtr(0, 0)^ := Vector2Byte(200, 128); |
| 140 |
GrayAlpha.PixelPtr(0, 1)^ := Vector2Byte(201, 128); |
| 141 |
GrayAlpha.PixelPtr(1, 0)^ := Vector2Byte(202, 128); |
| 142 |
GrayAlpha.PixelPtr(1, 1)^ := Vector2Byte(203, 128); |
| 143 |
|
| 144 |
GrayAlpha2 := GrayAlpha.MakeCopy as TGrayscaleAlphaImage; |
| 145 |
end; |
| 146 |
|
| 147 |
procedure TTestImagesDraw.TestDrawToRgb; |
| 148 |
begin |
| 149 |
{ draw from rgb } |
| 150 |
ResetImages; |
| 151 |
Rgb2.DrawTo(Rgb, 0, 1, dmBlend); |
| 152 |
AssertVectorEquals(Vector3Byte(1, 2, 3), Rgb.PixelPtr(0, 0)^); |
| 153 |
AssertVectorEquals(Vector3Byte(1, 2, 3), Rgb.PixelPtr(0, 1)^); |
| 154 |
AssertVectorEquals(Vector3Byte(11, 22, 33), Rgb.PixelPtr(1, 0)^); |
| 155 |
AssertVectorEquals(Vector3Byte(11, 22, 33), Rgb.PixelPtr(1, 1)^); |
| 156 |
|
| 157 |
ResetImages; |
| 158 |
Rgb2.DrawTo(Rgb, 0, 1, dmAdd); |
| 159 |
AssertVectorEquals(Vector3Byte(1, 2, 3), Rgb.PixelPtr(0, 0)^); |
| 160 |
AssertVectorEquals(Vector3Byte(4 + 1, 5 + 2, 6 + 3), Rgb.PixelPtr(0, 1)^); |
| 161 |
AssertVectorEquals(Vector3Byte(11, 22, 33), Rgb.PixelPtr(1, 0)^); |
| 162 |
AssertVectorEquals(Vector3Byte(44 + 11, 55 + 22, 66 + 33), Rgb.PixelPtr(1, 1)^); |
| 163 |
|
| 164 |
{ draw from rgb + alpha } |
| 165 |
ResetImages; |
| 166 |
RgbAlpha.DrawTo(Rgb, 0, 1, dmBlend); |
| 167 |
AssertVectorEquals(Vector3Byte(1, 2, 3), Rgb.PixelPtr(0, 0)^); |
| 168 |
AssertVectorEquals(Vector3Byte(BlendBytes(4, 7, 128), BlendBytes(5, 8, 128), BlendBytes(6, 9, 128)), Rgb.PixelPtr(0, 1)^); |
| 169 |
AssertVectorEquals(Vector3Byte(11, 22, 33), Rgb.PixelPtr(1, 0)^); |
| 170 |
AssertVectorEquals(Vector3Byte(BlendBytes(44, 77, 128), BlendBytes(55, 88, 128), BlendBytes(66, 99, 128)), Rgb.PixelPtr(1, 1)^); |
| 171 |
|
| 172 |
ResetImages; |
| 173 |
RgbAlpha.DrawTo(Rgb, 0, 1, dmAdd); |
| 174 |
AssertVectorEquals(Vector3Byte(1, 2, 3), Rgb.PixelPtr(0, 0)^); |
| 175 |
AssertVectorEquals(Vector3Byte(4 + 7 div 2, 5 + 8 div 2, 6 + 9 div 2), Rgb.PixelPtr(0, 1)^); |
| 176 |
AssertVectorEquals(Vector3Byte(11, 22, 33), Rgb.PixelPtr(1, 0)^); |
| 177 |
AssertVectorEquals(Vector3Byte(44 + 77 div 2, 55 + 88 div 2, 66 + 99 div 2), Rgb.PixelPtr(1, 1)^); |
| 178 |
|
| 179 |
{ draw from grayscale } |
| 180 |
ResetImages; |
| 181 |
Gray.DrawTo(Rgb, 0, 1, dmBlend); |
| 182 |
AssertVectorEquals(Vector3Byte(1, 2, 3), Rgb.PixelPtr(0, 0)^); |
| 183 |
AssertVectorEquals(Vector3Byte(100, 100, 100), Rgb.PixelPtr(0, 1)^); |
| 184 |
AssertVectorEquals(Vector3Byte(11, 22, 33), Rgb.PixelPtr(1, 0)^); |
| 185 |
AssertVectorEquals(Vector3Byte(102, 102, 102), Rgb.PixelPtr(1, 1)^); |
| 186 |
|
| 187 |
ResetImages; |
| 188 |
Gray.DrawTo(Rgb, 0, 1, dmAdd); |
| 189 |
AssertVectorEquals(Vector3Byte(1, 2, 3), Rgb.PixelPtr(0, 0)^); |
| 190 |
AssertVectorEquals(Vector3Byte(4 + 100, 5 + 100, 6 + 100), Rgb.PixelPtr(0, 1)^); |
| 191 |
AssertVectorEquals(Vector3Byte(11, 22, 33), Rgb.PixelPtr(1, 0)^); |
| 192 |
AssertVectorEquals(Vector3Byte(44 + 102, 55 + 102, 66 + 102), Rgb.PixelPtr(1, 1)^); |
| 193 |
|
| 194 |
{ draw from grayscale + alpha } |
| 195 |
ResetImages; |
| 196 |
GrayAlpha.DrawTo(Rgb, 0, 1, dmBlend); |
| 197 |
AssertVectorEquals(Vector3Byte(1, 2, 3), Rgb.PixelPtr(0, 0)^); |
| 198 |
AssertVectorEquals(Vector3Byte(BlendBytes(4, 200, 128), BlendBytes(5, 200, 128), BlendBytes(6, 200, 128)), Rgb.PixelPtr(0, 1)^); |
| 199 |
AssertVectorEquals(Vector3Byte(11, 22, 33), Rgb.PixelPtr(1, 0)^); |
| 200 |
AssertVectorEquals(Vector3Byte(BlendBytes(44, 202, 128), BlendBytes(55, 202, 128), BlendBytes(66, 202, 128)), Rgb.PixelPtr(1, 1)^); |
| 201 |
|
| 202 |
ResetImages; |
| 203 |
GrayAlpha.DrawTo(Rgb, 0, 1, dmAdd); |
| 204 |
AssertVectorEquals(Vector3Byte(1, 2, 3), Rgb.PixelPtr(0, 0)^); |
| 205 |
AssertVectorEquals(Vector3Byte(4 + 200 div 2, 5 + 200 div 2, 6 + 200 div 2), Rgb.PixelPtr(0, 1)^); |
| 206 |
AssertVectorEquals(Vector3Byte(11, 22, 33), Rgb.PixelPtr(1, 0)^); |
| 207 |
AssertVectorEquals(Vector3Byte(44 + 202 div 2, 55 + 202 div 2, 66 + 202 div 2), Rgb.PixelPtr(1, 1)^); |
| 208 |
end; |
| 209 |
|
| 210 |
procedure TTestImagesDraw.TestDrawToRgbAlpha; |
| 211 |
begin |
| 212 |
{ draw from rgb } |
| 213 |
ResetImages; |
| 214 |
Rgb.DrawTo(RgbAlpha, 0, 1, dmBlend); |
| 215 |
AssertVectorEquals(Vector4Byte(7, 8, 9, 128), RgbAlpha.PixelPtr(0, 0)^); |
| 216 |
AssertVectorEquals(Vector4Byte(1, 2, 3, 128), RgbAlpha.PixelPtr(0, 1)^); |
| 217 |
AssertVectorEquals(Vector4Byte(77, 88, 99, 128), RgbAlpha.PixelPtr(1, 0)^); |
| 218 |
AssertVectorEquals(Vector4Byte(11, 22, 33, 128), RgbAlpha.PixelPtr(1, 1)^); |
| 219 |
|
| 220 |
ResetImages; |
| 221 |
Rgb.DrawTo(RgbAlpha, 0, 1, dmAdd); |
| 222 |
AssertVectorEquals(Vector4Byte(7, 8, 9, 128), RgbAlpha.PixelPtr(0, 0)^); |
| 223 |
AssertVectorEquals(Vector4Byte(3 + 1, 2 + 2, 1 + 3, 128), RgbAlpha.PixelPtr(0, 1)^); |
| 224 |
AssertVectorEquals(Vector4Byte(77, 88, 99, 128), RgbAlpha.PixelPtr(1, 0)^); |
| 225 |
AssertVectorEquals(Vector4Byte(33 + 11, 22 + 22, 11 + 33, 128), RgbAlpha.PixelPtr(1, 1)^); |
| 226 |
|
| 227 |
{ draw from rgb + alpha } |
| 228 |
ResetImages; |
| 229 |
RgbAlpha2.DrawTo(RgbAlpha, 0, 1, dmBlend); |
| 230 |
AssertVectorEquals(Vector4Byte(7, 8, 9, 128), RgbAlpha.PixelPtr(0, 0)^); |
| 231 |
AssertVectorEquals(Vector4Byte(BlendBytes(3, 7, 128), BlendBytes(2, 8, 128), BlendBytes(1, 9, 128), 128), RgbAlpha.PixelPtr(0, 1)^); |
| 232 |
AssertVectorEquals(Vector4Byte(77, 88, 99, 128), RgbAlpha.PixelPtr(1, 0)^); |
| 233 |
AssertVectorEquals(Vector4Byte(BlendBytes(33, 77, 128), BlendBytes(22, 88, 128), BlendBytes(11, 99, 128), 128), RgbAlpha.PixelPtr(1, 1)^); |
| 234 |
|
| 235 |
ResetImages; |
| 236 |
RgbAlpha2.DrawTo(RgbAlpha, 0, 1, dmAdd); |
| 237 |
AssertVectorEquals(Vector4Byte(7, 8, 9, 128), RgbAlpha.PixelPtr(0, 0)^); |
| 238 |
AssertVectorEquals(Vector4Byte(3 + 7 div 2, 2 + 8 div 2, 1 + 9 div 2, 128), RgbAlpha.PixelPtr(0, 1)^); |
| 239 |
AssertVectorEquals(Vector4Byte(77, 88, 99, 128), RgbAlpha.PixelPtr(1, 0)^); |
| 240 |
AssertVectorEquals(Vector4Byte(33 + 77 div 2, 22 + 88 div 2, 11 + 99 div 2, 128), RgbAlpha.PixelPtr(1, 1)^); |
| 241 |
|
| 242 |
{ draw from grayscale } |
| 243 |
ResetImages; |
| 244 |
Gray.DrawTo(RgbAlpha, 0, 1, dmBlend); |
| 245 |
AssertVectorEquals(Vector4Byte(7, 8, 9, 128), RgbAlpha.PixelPtr(0, 0)^); |
| 246 |
AssertVectorEquals(Vector4Byte(100, 100, 100, 128), RgbAlpha.PixelPtr(0, 1)^); |
| 247 |
AssertVectorEquals(Vector4Byte(77, 88, 99, 128), RgbAlpha.PixelPtr(1, 0)^); |
| 248 |
AssertVectorEquals(Vector4Byte(102, 102, 102, 128), RgbAlpha.PixelPtr(1, 1)^); |
| 249 |
|
| 250 |
ResetImages; |
| 251 |
Gray.DrawTo(RgbAlpha, 0, 1, dmAdd); |
| 252 |
AssertVectorEquals(Vector4Byte(7, 8, 9, 128), RgbAlpha.PixelPtr(0, 0)^); |
| 253 |
AssertVectorEquals(Vector4Byte(3 + 100, 2 + 100, 1 + 100, 128), RgbAlpha.PixelPtr(0, 1)^); |
| 254 |
AssertVectorEquals(Vector4Byte(77, 88, 99, 128), RgbAlpha.PixelPtr(1, 0)^); |
| 255 |
AssertVectorEquals(Vector4Byte(33 + 102, 22 + 102, 11 + 102, 128), RgbAlpha.PixelPtr(1, 1)^); |
| 256 |
|
| 257 |
{ draw from grayscale + alpha } |
| 258 |
ResetImages; |
| 259 |
GrayAlpha.DrawTo(RgbAlpha, 0, 1, dmBlend); |
| 260 |
AssertVectorEquals(Vector4Byte(7, 8, 9, 128), RgbAlpha.PixelPtr(0, 0)^); |
| 261 |
AssertVectorEquals(Vector4Byte(BlendBytes(3, 200, 128), BlendBytes(2, 200, 128), BlendBytes(1, 200, 128), 128), RgbAlpha.PixelPtr(0, 1)^); |
| 262 |
AssertVectorEquals(Vector4Byte(77, 88, 99, 128), RgbAlpha.PixelPtr(1, 0)^); |
| 263 |
AssertVectorEquals(Vector4Byte(BlendBytes(33, 202, 128), BlendBytes(22, 202, 128), BlendBytes(11, 202, 128), 128), RgbAlpha.PixelPtr(1, 1)^); |
| 264 |
|
| 265 |
ResetImages; |
| 266 |
GrayAlpha.DrawTo(RgbAlpha, 0, 1, dmAdd); |
| 267 |
AssertVectorEquals(Vector4Byte(7, 8, 9, 128), RgbAlpha.PixelPtr(0, 0)^); |
| 268 |
AssertVectorEquals(Vector4Byte(3 + 200 div 2, 2 + 200 div 2, 1 + 200 div 2, 128), RgbAlpha.PixelPtr(0, 1)^); |
| 269 |
AssertVectorEquals(Vector4Byte(77, 88, 99, 128), RgbAlpha.PixelPtr(1, 0)^); |
| 270 |
AssertVectorEquals(Vector4Byte(33 + 202 div 2, 22 + 202 div 2, 11 + 202 div 2, 128), RgbAlpha.PixelPtr(1, 1)^); |
| 271 |
end; |
| 272 |
|
| 273 |
procedure TTestImagesDraw.TestDrawToGrayscale; |
| 274 |
begin |
| 275 |
{ draw from rgb + alpha } |
| 276 |
ResetImages; |
| 277 |
RgbAlpha.DrawTo(Gray, 0, 1, dmBlend); |
| 278 |
AssertEquals(100, Gray.PixelPtr(0, 0)^); |
| 279 |
AssertEquals(BlendBytes(101, GrayscaleValue(Vector3Byte(7, 8, 9)), 128), Gray.PixelPtr(0, 1)^); |
| 280 |
AssertEquals(102, Gray.PixelPtr(1, 0)^); |
| 281 |
AssertEquals(BlendBytes(103, GrayscaleValue(Vector3Byte(77, 88, 99)), 128), Gray.PixelPtr(1, 1)^); |
| 282 |
|
| 283 |
ResetImages; |
| 284 |
RgbAlpha.DrawTo(Gray, 0, 1, dmAdd); |
| 285 |
AssertEquals(100, Gray.PixelPtr(0, 0)^); |
| 286 |
AssertEquals(101 + GrayscaleValue(Vector3Byte(7, 8, 9)) div 2, Gray.PixelPtr(0, 1)^); |
| 287 |
AssertEquals(102, Gray.PixelPtr(1, 0)^); |
| 288 |
AssertEquals(103 + GrayscaleValue(Vector3Byte(77, 88, 99)) div 2, Gray.PixelPtr(1, 1)^); |
| 289 |
|
| 290 |
{ draw from rgb } |
| 291 |
ResetImages; |
| 292 |
Rgb.DrawTo(Gray, 0, 1, dmBlend); |
| 293 |
AssertEquals(100, Gray.PixelPtr(0, 0)^); |
| 294 |
AssertEquals(GrayscaleValue(Vector3Byte(1, 2, 3)), Gray.PixelPtr(0, 1)^); |
| 295 |
AssertEquals(102, Gray.PixelPtr(1, 0)^); |
| 296 |
AssertEquals(GrayscaleValue(Vector3Byte(11, 22, 33)), Gray.PixelPtr(1, 1)^); |
| 297 |
|
| 298 |
ResetImages; |
| 299 |
Rgb.DrawTo(Gray, 0, 1, dmAdd); |
| 300 |
AssertEquals(100, Gray.PixelPtr(0, 0)^); |
| 301 |
AssertEquals(101 + GrayscaleValue(Vector3Byte(1, 2, 3)), Gray.PixelPtr(0, 1)^); |
| 302 |
AssertEquals(102, Gray.PixelPtr(1, 0)^); |
| 303 |
AssertEquals(103 + GrayscaleValue(Vector3Byte(11, 22, 33)), Gray.PixelPtr(1, 1)^); |
| 304 |
|
| 305 |
{ draw from grayscale } |
| 306 |
ResetImages; |
| 307 |
Gray2.DrawTo(Gray, 0, 1, dmBlend); |
| 308 |
AssertEquals(100, Gray.PixelPtr(0, 0)^); |
| 309 |
AssertEquals(100, Gray.PixelPtr(0, 1)^); |
| 310 |
AssertEquals(102, Gray.PixelPtr(1, 0)^); |
| 311 |
AssertEquals(102, Gray.PixelPtr(1, 1)^); |
| 312 |
|
| 313 |
ResetImages; |
| 314 |
Gray2.DrawTo(Gray, 0, 1, dmAdd); |
| 315 |
AssertEquals(100, Gray.PixelPtr(0, 0)^); |
| 316 |
AssertEquals(101 + 100, Gray.PixelPtr(0, 1)^); |
| 317 |
AssertEquals(102, Gray.PixelPtr(1, 0)^); |
| 318 |
AssertEquals(103 + 102, Gray.PixelPtr(1, 1)^); |
| 319 |
|
| 320 |
{ draw from grayscale + alpha } |
| 321 |
ResetImages; |
| 322 |
GrayAlpha.DrawTo(Gray, 0, 1, dmBlend); |
| 323 |
AssertEquals(100, Gray.PixelPtr(0, 0)^); |
| 324 |
AssertEquals(BlendBytes(101, 200, 128), Gray.PixelPtr(0, 1)^); |
| 325 |
AssertEquals(102, Gray.PixelPtr(1, 0)^); |
| 326 |
AssertEquals(BlendBytes(103, 202, 128), Gray.PixelPtr(1, 1)^); |
| 327 |
|
| 328 |
ResetImages; |
| 329 |
GrayAlpha.DrawTo(Gray, 0, 1, dmAdd); |
| 330 |
AssertEquals(100, Gray.PixelPtr(0, 0)^); |
| 331 |
AssertEquals(101 + 200 div 2, Gray.PixelPtr(0, 1)^); |
| 332 |
AssertEquals(102, Gray.PixelPtr(1, 0)^); |
| 333 |
AssertEquals(103 + 202 div 2, Gray.PixelPtr(1, 1)^); |
| 334 |
end; |
| 335 |
|
| 336 |
procedure TTestImagesDraw.TestDrawToGrayscaleAlpha; |
| 337 |
begin |
| 338 |
{ draw from rgb } |
| 339 |
ResetImages; |
| 340 |
Rgb.DrawTo(GrayAlpha, 0, 1, dmBlend); |
| 341 |
AssertVectorEquals(Vector2Byte(200, 128), GrayAlpha.PixelPtr(0, 0)^); |
| 342 |
AssertVectorEquals(Vector2Byte(GrayscaleValue(Vector3Byte(1, 2, 3)), 128), GrayAlpha.PixelPtr(0, 1)^); |
| 343 |
AssertVectorEquals(Vector2Byte(202, 128), GrayAlpha.PixelPtr(1, 0)^); |
| 344 |
AssertVectorEquals(Vector2Byte(GrayscaleValue(Vector3Byte(11, 22, 33)), 128), GrayAlpha.PixelPtr(1, 1)^); |
| 345 |
|
| 346 |
ResetImages; |
| 347 |
Rgb.DrawTo(GrayAlpha, 0, 1, dmAdd); |
| 348 |
AssertVectorEquals(Vector2Byte(200, 128), GrayAlpha.PixelPtr(0, 0)^); |
| 349 |
AssertVectorEquals(Vector2Byte(201 + GrayscaleValue(Vector3Byte(1, 2, 3)), 128), GrayAlpha.PixelPtr(0, 1)^); |
| 350 |
AssertVectorEquals(Vector2Byte(202, 128), GrayAlpha.PixelPtr(1, 0)^); |
| 351 |
AssertVectorEquals(Vector2Byte(203 + GrayscaleValue(Vector3Byte(11, 22, 33)), 128), GrayAlpha.PixelPtr(1, 1)^); |
| 352 |
|
| 353 |
{ draw from rgb + alpha } |
| 354 |
ResetImages; |
| 355 |
RgbAlpha.DrawTo(GrayAlpha, 0, 1, dmBlend); |
| 356 |
AssertVectorEquals(Vector2Byte(200, 128), GrayAlpha.PixelPtr(0, 0)^); |
| 357 |
AssertVectorEquals(Vector2Byte(BlendBytes(201, GrayscaleValue(Vector3Byte(7, 8, 9)), 128), 128), GrayAlpha.PixelPtr(0, 1)^); |
| 358 |
AssertVectorEquals(Vector2Byte(202, 128), GrayAlpha.PixelPtr(1, 0)^); |
| 359 |
AssertVectorEquals(Vector2Byte(BlendBytes(203, GrayscaleValue(Vector3Byte(77, 88, 99)), 128), 128), GrayAlpha.PixelPtr(1, 1)^); |
| 360 |
|
| 361 |
ResetImages; |
| 362 |
RgbAlpha.DrawTo(GrayAlpha, 0, 1, dmAdd); |
| 363 |
AssertVectorEquals(Vector2Byte(200, 128), GrayAlpha.PixelPtr(0, 0)^); |
| 364 |
AssertVectorEquals(Vector2Byte(201 + GrayscaleValue(Vector3Byte(7, 8, 9)) div 2, 128), GrayAlpha.PixelPtr(0, 1)^); |
| 365 |
AssertVectorEquals(Vector2Byte(202, 128), GrayAlpha.PixelPtr(1, 0)^); |
| 366 |
AssertVectorEquals(Vector2Byte(203 + GrayscaleValue(Vector3Byte(77, 88, 99)) div 2, 128), GrayAlpha.PixelPtr(1, 1)^); |
| 367 |
|
| 368 |
{ draw from grayscale } |
| 369 |
ResetImages; |
| 370 |
Gray.DrawTo(GrayAlpha, 0, 1, dmBlend); |
| 371 |
AssertVectorEquals(Vector2Byte(200, 128), GrayAlpha.PixelPtr(0, 0)^); |
| 372 |
AssertVectorEquals(Vector2Byte(100, 128), GrayAlpha.PixelPtr(0, 1)^); |
| 373 |
AssertVectorEquals(Vector2Byte(202, 128), GrayAlpha.PixelPtr(1, 0)^); |
| 374 |
AssertVectorEquals(Vector2Byte(102, 128), GrayAlpha.PixelPtr(1, 1)^); |
| 375 |
|
| 376 |
ResetImages; |
| 377 |
Gray.DrawTo(GrayAlpha, 0, 1, dmAdd); |
| 378 |
AssertVectorEquals(Vector2Byte(200, 128), GrayAlpha.PixelPtr(0, 0)^); |
| 379 |
AssertVectorEquals(Vector2Byte(AddBytesPremultiplied(201, 100), 128), GrayAlpha.PixelPtr(0, 1)^); |
| 380 |
AssertVectorEquals(Vector2Byte(202, 128), GrayAlpha.PixelPtr(1, 0)^); |
| 381 |
AssertVectorEquals(Vector2Byte(AddBytesPremultiplied(203, 102), 128), GrayAlpha.PixelPtr(1, 1)^); |
| 382 |
|
| 383 |
{ draw from grayscale + alpha } |
| 384 |
ResetImages; |
| 385 |
GrayAlpha2.DrawTo(GrayAlpha, 0, 1, dmBlend); |
| 386 |
AssertVectorEquals(Vector2Byte(200, 128), GrayAlpha.PixelPtr(0, 0)^); |
| 387 |
AssertVectorEquals(Vector2Byte(BlendBytes(201, 200, 128), 128), GrayAlpha.PixelPtr(0, 1)^); |
| 388 |
AssertVectorEquals(Vector2Byte(202, 128), GrayAlpha.PixelPtr(1, 0)^); |
| 389 |
AssertVectorEquals(Vector2Byte(BlendBytes(203, 202, 128), 128), GrayAlpha.PixelPtr(1, 1)^); |
| 390 |
|
| 391 |
ResetImages; |
| 392 |
GrayAlpha2.DrawTo(GrayAlpha, 0, 1, dmAdd); |
| 393 |
AssertVectorEquals(Vector2Byte(200, 128), GrayAlpha.PixelPtr(0, 0)^); |
| 394 |
AssertVectorEquals(Vector2Byte(AddBytesPremultiplied(201, 200), 128), GrayAlpha.PixelPtr(0, 1)^); |
| 395 |
AssertVectorEquals(Vector2Byte(202, 128), GrayAlpha.PixelPtr(1, 0)^); |
| 396 |
AssertVectorEquals(Vector2Byte(AddBytesPremultiplied(203, 202), 128), GrayAlpha.PixelPtr(1, 1)^); |
| 397 |
end; |
| 398 |
|
| 399 |
initialization |
| 400 |
RegisterTest(TTestImagesDraw); |
| 401 |
end. |