00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 #include "camtypes.h"
00100 #include "fthrconv.h"
00101 #include "bshadow.h"
00102
00104
00105
00106
00108 const double HalfX = 0.4999999999999 ;
00109
00110
00111
00112
00113
00114 inline UINT32 FloorSqrt( const double D )
00115 {
00116 #if defined(_M_IX86)
00117 UINT32 I ;
00118 __asm fld D
00119 __asm fsqrt
00120 __asm fsub HalfX
00121 __asm fistp I
00122 return I ;
00123 #else
00124 return (UINT32)sqrt(D) ;
00125 #endif
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 LPBITMAPINFO Shadow32BppBitmap(const UINT32 nBlur, LPBITMAPINFO pSrcInfo, LPBYTE pSrcBits32, LPBYTE* pDestBits)
00167 {
00168 ERROR2IF(pSrcInfo->bmiHeader.biBitCount!=32, NULL, "Only supports 32bpp bitmap feathering");
00169
00170
00171 CSize SrcBmpSize(pSrcInfo->bmiHeader.biWidth, pSrcInfo->bmiHeader.biHeight);
00172 CSize InnerBmpSizeCheck(SrcBmpSize.cx - 2*nBlur,SrcBmpSize.cy - 2*nBlur);
00173
00174 if(!(InnerBmpSizeCheck.cx>1 && InnerBmpSizeCheck.cy>1))
00175 {
00176
00177 ENSURE(FALSE, "Conv code cannot create bmp with width or height of 1.");
00178 return NULL;
00179 }
00180
00182
00184
00185
00186
00187
00188 const UINT32 DWordWidth = DIBUtil::ScanlineSize(SrcBmpSize.cx, 8);
00189
00190 UINT32 DWordPadding = DWordWidth - SrcBmpSize.cx;
00191 BOOL DWordAligned = (DWordPadding==0)? TRUE: FALSE;
00192
00193 CSize DWordSrcBmpSize(DWordWidth, SrcBmpSize.cy);
00194
00196
00198 BYTE* pBitmapToShadow = new BYTE[DWordSrcBmpSize.cx*DWordSrcBmpSize.cy];
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 CONST BYTE* pSrc = (CONST BYTE *)pSrcBits32;
00212 BYTE* pDest = (BYTE *)pBitmapToShadow;
00213
00214 UINT32 ForegroundScanlineSize = SrcBmpSize.cx * 4;
00215
00216 for ( UINT32 i=0 ; i<(UINT32)DWordSrcBmpSize.cy ; i++ )
00217 {
00218 CONST BYTE* pNextSrcLine = pSrc +ForegroundScanlineSize;
00219 BYTE* pNextDestLine = pDest+DWordSrcBmpSize.cx;
00220 for ( UINT32 j=0 ; j<(UINT32)SrcBmpSize.cx ; j++ )
00221 {
00222 *pDest++ = *pSrc;
00223 pSrc += 4;
00224 }
00225
00226
00227 if (!DWordAligned)
00228 {
00229 for(j=0; j<DWordPadding; j++)
00230 *pDest++ = 0xFF;
00231 }
00232
00233 pSrc = pNextSrcLine;
00234 pDest = pNextDestLine;
00235 }
00236
00238
00240
00241 BITMAPINFOHEADER SourceBI;
00242 SourceBI.biBitCount = 8;
00243 SourceBI.biWidth = SrcBmpSize.cx;
00244 SourceBI.biHeight = SrcBmpSize.cy;
00245 SourceBI.biPlanes = 1;
00246
00247
00248 CSize InnerBmpSize(SrcBmpSize.cx - 2*nBlur,SrcBmpSize.cy - 2*nBlur);
00249 LPBITMAPINFO pDestInfo = AllocDIB( InnerBmpSize.cx, InnerBmpSize.cy, 8, pDestBits, NULL, TRUE);
00250 if(pDestInfo==NULL)
00251 {
00252 ENSURE(FALSE,"Failed to allocate space for shadow bitmap!");
00253 delete [] pBitmapToShadow;
00254 return NULL;
00255 }
00256 pDestInfo->bmiHeader.biXPelsPerMeter = pSrcInfo->bmiHeader.biXPelsPerMeter;
00257 pDestInfo->bmiHeader.biYPelsPerMeter = pSrcInfo->bmiHeader.biYPelsPerMeter;
00258
00260
00262 DWORD Left [CBitmapShadow::MAX_ROW_OFFSETS] ;
00263 DWORD Right[CBitmapShadow::MAX_ROW_OFFSETS] ;
00264 DWORD Low [CBitmapShadow::MAX_ROW_OFFSETS] ;
00265 DWORD High [CBitmapShadow::MAX_ROW_OFFSETS] ;
00266 UINT32 nSize = nBlur*2+1 ;
00267 double S = nBlur ;
00268 const double R2 = nBlur * nBlur;
00269 UINT32 nLine = nBlur ;
00270 UINT32 nArea = 0 ;
00271 UINT32 nRows = 0 ;
00272 for ( UINT32 r=0 ; r<nSize ; ++r )
00273 {
00274 const double Radius2 = R2-S*S ;
00275 if ( Radius2>=0 )
00276 {
00277 const UINT32 nRadius = FloorSqrt(Radius2) ;
00278 Left[nRows] = nLine-nRadius ;
00279 Right[nRows] = nLine+nRadius+1 ;
00280 if ( Left[nRows]<Right[nRows] )
00281 {
00282 Low[nRows] = (nBlur-nRadius )*DWordSrcBmpSize.cx+r ;
00283 High[nRows] = (nBlur+nRadius+1)*DWordSrcBmpSize.cx+r ;
00284 nArea += Right[nRows]-Left[nRows] ;
00285 nRows++ ;
00286 }
00287 }
00288 S-- ;
00289 nLine += DWordSrcBmpSize.cx ;
00290 }
00291
00292 UINT32 nTableSize = nArea*255 ;
00293 UINT32 nInc = 0xffffffffu/nTableSize ;
00294 UINT32 nShift = 0 ;
00295 while ( nTableSize>=CBitmapShadow::TABLE_SIZE )
00296 {
00297 nTableSize >>= 1 ;
00298 nShift++ ;
00299 }
00300 nInc <<= nShift ;
00301 BYTE* pTranslationTable = new BYTE[nTableSize+1] ;
00302 UINT32 nCount = 0 ;
00303 for ( i=0 ; i<=nTableSize ; ++i )
00304 {
00305 pTranslationTable[i] = nCount>>24 ;
00306 nCount += nInc ;
00307 }
00308
00309
00311
00313 CBitmapShadow::GenerateWallShadow(
00314 &SourceBI, pBitmapToShadow,
00315 &pDestInfo->bmiHeader, *pDestBits,
00316 nRows, Left,Right,
00317 nRows, Low,High,
00318 nShift,pTranslationTable
00319 ) ;
00320
00322
00324 delete [] pBitmapToShadow;
00325 delete [] pTranslationTable;
00326
00327 return pDestInfo;
00328 }
00329
00330
00331 LPBITMAPINFO Shadow8BppBitmap(const UINT32 nBlur, LPBITMAPINFO pSrcInfo, LPBYTE pSrcBits8, LPBYTE* pDestBits)
00332 {
00333 ERROR2IF(pSrcInfo->bmiHeader.biBitCount!=8, NULL, "Only supports 8bpp bitmap feathering");
00334
00335
00336 CSize SrcBmpSize(pSrcInfo->bmiHeader.biWidth, pSrcInfo->bmiHeader.biHeight);
00337 CSize InnerBmpSizeCheck(SrcBmpSize.cx - 2*nBlur,SrcBmpSize.cy - 2*nBlur);
00338
00339 if(!(InnerBmpSizeCheck.cx>1 && InnerBmpSizeCheck.cy>1))
00340 {
00341
00342 ENSURE(FALSE, "Conv code cannot create bmp with width or height of 1.");
00343 return NULL;
00344 }
00345
00347
00349 const UINT32 DWordWidth = DIBUtil::ScanlineSize(SrcBmpSize.cx, 8);
00350
00351 UINT32 DWordPadding = DWordWidth - SrcBmpSize.cx;
00352 BOOL DWordAligned = (DWordPadding==0)? TRUE: FALSE;
00353
00354 CSize DWordSrcBmpSize(DWordWidth, SrcBmpSize.cy);
00355 BYTE* pBitmapToShadow=NULL;
00356 if(DWordAligned)
00357 {
00358 pBitmapToShadow = pSrcBits8;
00359 }
00360 else
00361 {
00362
00363
00364
00365
00366 pBitmapToShadow = pSrcBits8;
00367 }
00368
00370
00372
00373 BITMAPINFOHEADER SourceBI;
00374 SourceBI.biBitCount = 8;
00375 SourceBI.biWidth = SrcBmpSize.cx;
00376 SourceBI.biHeight = SrcBmpSize.cy;
00377 SourceBI.biPlanes = 1;
00378
00379
00380 CSize InnerBmpSize(SrcBmpSize.cx - 2*nBlur,SrcBmpSize.cy - 2*nBlur);
00381 LPBITMAPINFO pDestInfo = AllocDIB( InnerBmpSize.cx, InnerBmpSize.cy, 8, pDestBits, NULL, TRUE);
00382 if(pDestInfo==NULL)
00383 {
00384 ENSURE(FALSE,"Failed to allocate space for shadow bitmap!");
00385 return NULL;
00386 }
00387 pDestInfo->bmiHeader.biXPelsPerMeter = pSrcInfo->bmiHeader.biXPelsPerMeter;
00388 pDestInfo->bmiHeader.biYPelsPerMeter = pSrcInfo->bmiHeader.biYPelsPerMeter;
00389
00391
00393 DWORD Left [CBitmapShadow::MAX_ROW_OFFSETS] ;
00394 DWORD Right[CBitmapShadow::MAX_ROW_OFFSETS] ;
00395 DWORD Low [CBitmapShadow::MAX_ROW_OFFSETS] ;
00396 DWORD High [CBitmapShadow::MAX_ROW_OFFSETS] ;
00397 UINT32 nSize = nBlur*2+1 ;
00398 double S = nBlur ;
00399 const double R2 = nBlur * nBlur;
00400 UINT32 nLine = nBlur ;
00401 UINT32 nArea = 0 ;
00402 UINT32 nRows = 0 ;
00403 for ( UINT32 r=0 ; r<nSize ; ++r )
00404 {
00405 const double Radius2 = R2-S*S ;
00406 if ( Radius2>=0 )
00407 {
00408 const UINT32 nRadius = FloorSqrt(Radius2) ;
00409 Left[nRows] = nLine-nRadius ;
00410 Right[nRows] = nLine+nRadius+1 ;
00411 if ( Left[nRows]<Right[nRows] )
00412 {
00413 Low[nRows] = (nBlur-nRadius )*DWordSrcBmpSize.cx+r ;
00414 High[nRows] = (nBlur+nRadius+1)*DWordSrcBmpSize.cx+r ;
00415 nArea += Right[nRows]-Left[nRows] ;
00416 nRows++ ;
00417 }
00418 }
00419 S-- ;
00420 nLine += DWordSrcBmpSize.cx ;
00421 }
00422
00423 UINT32 nTableSize = nArea*255 ;
00424 UINT32 nInc = 0xffffffffu/nTableSize ;
00425 UINT32 nShift = 0 ;
00426 while ( nTableSize>=CBitmapShadow::TABLE_SIZE )
00427 {
00428 nTableSize >>= 1 ;
00429 nShift++ ;
00430 }
00431 nInc <<= nShift ;
00432 BYTE* pTranslationTable = new BYTE[nTableSize+1] ;
00433 UINT32 nCount = 0 ;
00434 for (UINT32 i=0 ; i<=nTableSize ; ++i )
00435 {
00436 pTranslationTable[i] = nCount>>24 ;
00437 nCount += nInc ;
00438 }
00439
00440
00442
00444 CBitmapShadow::GenerateWallShadow(
00445 &SourceBI, pBitmapToShadow,
00446 &pDestInfo->bmiHeader, *pDestBits,
00447 nRows, Left,Right,
00448 nRows, Low,High,
00449 nShift,pTranslationTable
00450 ) ;
00451
00453
00455
00456 delete [] pTranslationTable;
00457
00458 return pDestInfo;
00459 }