1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
|
/*
/--------------------------------------------------------------------
|
| $Id: dibgrit.cpp,v 1.9 2004/06/19 17:04:22 uzadow Exp $
| Bitmap Graphic item class
|
| A bitmap on a canvas.
|
| Copyright (c) 1996-2002 Ulrich von Zadow
|
\--------------------------------------------------------------------
*/
#include "stdafx.h"
#include "dibgrit.h"
#include "plbitmap.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_DYNAMIC (CDIBGrItem, CGrItem);
CDIBGrItem::CDIBGrItem
( int x, // Position on the canvas
int y,
int w, // Width
int h, // Height
int z, // Position in z-Order
PLBYTE Opacity, // Opacity of the object. 255 is completely
// opaque, 0 is completely transparent.
PLBmp * pBmp
)
: CGrItem (x, y, w, h, z, Opacity)
{
// ASSERT_VALID (pBmp);
ASSERT (pBmp->GetBitsPerPixel() == 32);
m_pBmp = pBmp;
}
CDIBGrItem::~CDIBGrItem
()
{
}
void CDIBGrItem::Draw
( PLBmp * pCanvas,
CRect * pUpdateRect
)
// Responsible for drawing the object on the canvas.
{
// ASSERT_VALID (pCanvas);
ASSERT (pCanvas->GetBitsPerPixel() == 32);
// ASSERT_VALID (m_pBmp);
ASSERT (m_pBmp->GetBitsPerPixel() == 32);
// Perform clipping.
CRect ClipRect (pUpdateRect);
clip (pCanvas, &ClipRect);
// Draw
if (m_w == m_pBmp->GetWidth() &&
m_h == m_pBmp->GetHeight())
drawClippedNoScale (pCanvas, &ClipRect);
else
if (m_pBmp->HasAlpha())
drawClipped (pCanvas, &ClipRect);
else
drawClippedNoAlpha (pCanvas, &ClipRect);
// If this fails, the procedure destroyed something.
// ASSERT_VALID (pCanvas);
// ASSERT_VALID (m_pBmp);
}
void CDIBGrItem::drawClippedNoScale
( PLBmp * pCanvas,
CRect * pRect
)
// Draws the object. pRect must have been clipped already.
// Assumes that no scaling is nessesary.
{
PLBYTE * pDest;
PLBYTE * pSrc;
PLBYTE ** pDestLineArray = pCanvas->GetLineArray();
PLBYTE ** pSrcLineArray = m_pBmp->GetLineArray();
int SrcXOfs = (pRect->left-m_x); // Starting X-Coordinate in the
// source bitmap. 0 unless clipped.
for (int sy = pRect->top; sy<pRect->bottom; sy++)
{ // For each line
// Set up pointers to beginnings of lines.
pDest = pDestLineArray[sy] + pRect->left*4;
pSrc = pSrcLineArray[sy-m_y] + SrcXOfs*4;
if (m_pBmp->HasAlpha())
drawAlphaLine (pDest, pSrc, pRect);
else
// No alpha channel.
if (m_Opacity == 255)
memcpy (pDest, pSrc, 4*pRect->Width());
else
drawFadeLine (pDest, pSrc, pRect);
}
__asm emms
}
// Disable 'no EMMS instruction' warning. The calling function contains
// the emms instruction, so everything's ok.
#pragma warning (disable: 4799)
inline void CDIBGrItem::drawAlphaLine
( PLBYTE * pDest,
PLBYTE * pSrc,
CRect * pRect
)
// Draws one line. No scaling. Assumes alpha channel exists.
{
PLBYTE * pSrcStart = pSrc+(pRect->left-m_x)*4;
__int64 Alpha = __int64(m_Opacity) + (__int64(m_Opacity)<<16)
+ (__int64(m_Opacity)<<32) + (__int64(m_Opacity)<<48);
__int64 Negator = 0x00ff00ff00ff00ff;
int LoopLen = pRect->right-pRect->left+1;
__asm
{
mov ecx, LoopLen
mov edi, pDest
// Set up constants.
mov esi, pSrcStart
movq mm7, Alpha
movq mm6, Negator
pxor mm0, mm0
// Calculate current alpha.
movzx eax, byte ptr [esi+PL_RGBA_ALPHA]
mov ebx, eax
shl ebx, 8
or ebx, eax
shl ebx, 8
startofloop:
ALIGN 16
or ebx, eax
// Load source, do alpha calc.
movd mm1, [esi]
movd mm3, ebx
add edi, 4
punpcklbw mm3, mm0
punpcklbw mm1, mm0
pmullw mm3, mm7
// Load destination, do neg. alpha calc.
movd mm2, [edi-4]
add esi, 4
psrlw mm3, 8
punpcklbw mm2, mm0
pmullw mm1, mm3
pxor mm3, mm6
pmullw mm2, mm3
dec ecx
je endofloop
// Calculate alpha for next iteration
movzx eax, byte ptr [esi+PL_RGBA_ALPHA]
paddusw mm1, mm2
mov ebx, eax
psrlw mm1, 8
shl ebx, 8
// Write results back.
packuswb mm1, mm0
or ebx, eax
movd [edi-4], mm1
shl ebx, 8
jmp startofloop
endofloop:
}
}
#pragma warning (default: 4799)
inline void CDIBGrItem::drawFadeLine
( PLBYTE * pDest,
PLBYTE * pSrc,
CRect * pRect
)
// Draws one line. No scaling. Assumes alpha channel doesn't
// exist.
{
int negalpha;
negalpha = 255-m_Opacity;
for (int sx = pRect->left; sx<pRect->right; sx++)
{ // For each pixel
pDest[PL_RGBA_RED] = (pDest[PL_RGBA_RED]*negalpha +
pSrc[PL_RGBA_RED]*m_Opacity)>>8;
pDest[PL_RGBA_GREEN] = (pDest[PL_RGBA_GREEN]*negalpha +
pSrc[PL_RGBA_GREEN]*m_Opacity)>>8;
pDest[PL_RGBA_BLUE] = (pDest[PL_RGBA_BLUE]*negalpha +
pSrc[PL_RGBA_BLUE]*m_Opacity)>>8;
pDest += 4;
pSrc += 4;
}
}
void CDIBGrItem::drawClipped
( PLBmp * pCanvas,
CRect * pRect
)
// Draws the object. pRect must have been clipped already.
{
PLBYTE * pDest;
PLBYTE * pSrc;
PLBYTE * pSrcStart;
PLBYTE * pAlpha = NULL;
// Invisible.
if (m_Opacity < 3)
return;
PLBYTE ** pDestLineArray = pCanvas->GetLineArray();
PLBYTE ** pSrcLineArray = m_pBmp->GetLineArray();
// Calculate Loop invariants.
double YScale = (double)(m_pBmp->GetHeight())/m_h;
int ys = (int)(YScale*65536);
int SrcLineNum;
double XScale = (double)(m_pBmp->GetWidth())/m_w;
int xs = (int)(XScale*65536)*4;
__int64 Alpha = __int64(m_Opacity) + (__int64(m_Opacity)<<16)
+ (__int64(m_Opacity)<<32) + (__int64(m_Opacity)<<48);
int SrcStart = ((pRect->left-m_x)*xs)<<16;
__int64 Negator = 0x00ff00ff00ff00ff;
int LoopStart = pRect->left-m_x;
int LoopLen = pRect->right-pRect->left+1;
for (int sy = pRect->top; sy<pRect->bottom; sy++)
{ // For each line
pDest = pDestLineArray[sy] + pRect->left*4;
SrcLineNum = (int)((sy-m_y)*ys)>>16;
pSrc = pSrcLineArray[SrcLineNum];
pSrcStart = pSrc+SrcStart;
__asm
{
mov ecx, LoopLen
mov edi, pDest
mov edx, 0
// Set up constants.
mov esi, pSrcStart
movq mm7, Alpha
movq mm6, Negator
pxor mm0, mm0
// Calculate current alpha.
movzx eax, byte ptr [esi+PL_RGBA_ALPHA]
mov ebx, eax
shl ebx, 8
or ebx, eax
shl ebx, 8
startofloop:
ALIGN 16
or ebx, eax
// Load source, do alpha calc.
movd mm1, [esi]
movd mm3, ebx
add edi, 4
punpcklbw mm3, mm0
punpcklbw mm1, mm0
pmullw mm3, mm7
// Load destination, do neg. alpha calc.
movd mm2, [edi-4]
// Calc source address for next iteration.
add edx, xs
punpcklbw mm2, mm0
mov esi, edx
psrlw mm3, 8
shr esi, 16
pmullw mm1, mm3
pxor mm3, mm6
add esi, pSrcStart
pmullw mm2, mm3
and esi, 0xfffffffc
// Calculate alpha for next iteration
movzx eax, byte ptr [esi+PL_RGBA_ALPHA]
paddusw mm1, mm2
mov ebx, eax
psrlw mm1, 8
shl ebx, 8
// Write results back.
packuswb mm1, mm0
or ebx, eax
movd [edi-4], mm1
shl ebx, 8
dec ecx
jnz startofloop
}
}
__asm emms
}
/* This is the unoptimized assembler version 2.0. */
/*
int LoopStart = pRect->left-m_x;
int LoopEnd = pRect->right-m_x;
int Opacity = m_Opacity;
__int64 Negator = 0x00ff00ff00ff00ff;
__int64 Alpha = __int64(m_Opacity) + (__int64(m_Opacity)<<16)
+ (__int64(m_Opacity)<<32) + (__int64(m_Opacity)<<48);
__int64 temp;
__asm
{
push si
push di
mov ecx, LoopStart
mov edi, pDest
movq mm7, Alpha
startofloop:
// Calculate source address
mov eax, xs
mul ecx
shr eax, 16 // This is 16 to compensate for fixed-point calculations
shl eax, 2 // Every pixel is 4 bytes wide.
// Can't do these two in one step because we need the last two
// bits to be zero.
mov esi, pSrc
add esi, eax
// Calculate current alpha.
movq mm3, mm7
cmp bAlpha, 0
jz endalphacalc
mov ebx, 0
mov bl, [esi+PL_RGBA_ALPHA]
mov bh, bl
mov edx, ebx
shl edx, 16
add ebx, edx
movd mm3, ebx
punpcklbw mm3, mm0
pmullw mm3, mm7
psrlw mm3, 8
endalphacalc:
movd ebx, mm3
cmp bl, 4 // Completely transparent
jb endofloop
cmp bl, 0xfc
jb realalpha
// Completely opaque
mov eax, [esi]
mov [edi], eax
jmp endofloop
realalpha:
// Die Konstante 0 speichern.
pxor mm0, mm0
// Load source, do alpha calc.
movd mm1, [esi]
punpcklbw mm1, mm0
movq temp, mm1
pmullw mm1, mm3
movq temp, mm1
psrlw mm1, 8
// Load destination, do neg. alpha calc.
movd mm2, [edi]
punpcklbw mm2, mm0
pxor mm3, Negator
pmullw mm2, mm3
psrlw mm2, 8
paddusw mm1, mm2
// Write results back.
packuswb mm1, mm0
movd [edi], mm1
endofloop:
add edi, 4
inc ecx
cmp ecx, LoopEnd
jl startofloop
emms
pop si
pop di
}
*/
/* Original C++ version
int SrcOfs;
int alpha;
int negalpha;
for (int sx = pRect->left-m_x; sx<pRect->right-m_x; sx++)
{ // For each pixel
SrcOfs = (sx*xs)>>16;
if (bAlpha)
alpha = ((pSrc[SrcOfs*4+PL_RGBA_ALPHA]) * m_Opacity)>>8;
else
alpha = m_Opacity;
negalpha = 255-alpha;
pDest[PL_RGBA_RED] = (pDest[PL_RGBA_RED]*negalpha +
pSrc[SrcOfs*4+PL_RGBA_RED]*alpha)>>8;
pDest[PL_RGBA_GREEN] = (pDest[PL_RGBA_GREEN]*negalpha +
pSrc[SrcOfs*4+PL_RGBA_GREEN]*alpha)>>8;
pDest[PL_RGBA_BLUE] = (pDest[PL_RGBA_BLUE]*negalpha +
pSrc[SrcOfs*4+PL_RGBA_BLUE]*alpha)>>8;
pDest += 4;
}
*/
void CDIBGrItem::drawClippedNoAlpha
( PLBmp * pCanvas,
CRect * pRect
)
// Draws the object. pRect must have been clipped already.
{
PLBYTE * pDest;
PLBYTE * pSrc;
PLBYTE * pSrcStart;
PLBYTE * pAlpha = NULL;
// Invisible.
if (m_Opacity < 3)
return;
PLBYTE ** pDestLineArray = pCanvas->GetLineArray();
PLBYTE ** pSrcLineArray = m_pBmp->GetLineArray();
// Calculate Loop invariants.
double YScale = (double)(m_pBmp->GetHeight())/m_h;
int ys = (int)(YScale*65536);
int SrcLineNum;
double XScale = (double)(m_pBmp->GetWidth())/m_w;
int xs = (int)(XScale*65536)*4;
__int64 Alpha = __int64(m_Opacity) + (__int64(m_Opacity)<<16)
+ (__int64(m_Opacity)<<32) + (__int64(m_Opacity)<<48);
int SrcStart = ((pRect->left-m_x)*xs)<<16;
__int64 Negator = 0x00ff00ff00ff00ff;
int LoopStart = pRect->left-m_x;
int LoopLen = pRect->right-pRect->left+1;
for (int sy = pRect->top; sy<pRect->bottom; sy++)
{ // For each line
pDest = pDestLineArray[sy] + pRect->left*4;
SrcLineNum = (int)((sy-m_y)*ys)>>16;
pSrc = pSrcLineArray[SrcLineNum];
pSrcStart = pSrc+SrcStart;
__asm
{
mov ecx, LoopLen
mov edi, pDest
mov edx, 0
// Set up constants.
mov esi, pSrcStart
movq mm3, Alpha
movq mm4, mm3
movq mm6, Negator
pxor mm4, mm6
pxor mm0, mm0
startofloop:
ALIGN 16
// Load source, do alpha calc.
movd mm1, [esi]
add edi, 4
punpcklbw mm1, mm0
// Load destination, do neg. alpha calc.
movd mm2, [edi-4]
// Calc source address for next iteration.
add edx, xs
punpcklbw mm2, mm0
mov esi, edx
pmullw mm1, mm3
shr esi, 16
pmullw mm2, mm4
add esi, pSrcStart
and esi, 0xfffffffc
paddusw mm1, mm2
psrlw mm1, 8
// Write results back.
packuswb mm1, mm0
movd [edi-4], mm1
dec ecx
jnz startofloop
}
}
__asm emms
}
/*
/--------------------------------------------------------------------
|
| $Log: dibgrit.cpp,v $
| Revision 1.9 2004/06/19 17:04:22 uzadow
| Removed Lock(), Unlock(), PLDDrawBmp
|
| Revision 1.8 2002/03/31 13:36:42 uzadow
| Updated copyright.
|
| Revision 1.7 2001/09/16 19:03:23 uzadow
| Added global name prefix PL, changed most filenames.
|
| Revision 1.6 2001/09/15 17:00:35 uzadow
| Fixed bug where too much memory is read.
|
| Revision 1.5 2000/08/13 12:11:44 Administrator
| Added experimental DirectDraw-Support
|
| Revision 1.4 2000/01/16 20:43:19 anonymous
| Removed MFC dependencies
|
| Revision 1.3 1999/10/21 16:06:55 Ulrich von Zadow
| Added #pragma warning off
|
|
--------------------------------------------------------------------
*/
|