我正在尝试在频谱图上创建透明的选择叠加层,但效果不佳。我的意思是结果并不令人满意。相比之下,绘制在波形顶部的叠加效果很好,但我需要同时支持波形和频谱图 View ( future 可能还有其他 View )
选择叠加在波形 View 中工作正常
这是频谱图 View 中的选择叠加(选择看起来非常糟糕并且掩盖了频谱图的部分)
两种 View 的代码 (VCL) 相同
void TWaveDisplayContainer::DrawSelectedRegion(){
if(selRange.selStart.x == selRange.selEnd.x){
DrawCursorPosition( selRange.selStart.x);
return;
}
Graphics::TBitmap *pWaveBmp = eContainerView == WAVEFORM ? pWaveBmpLeft : pSfftBmpLeft;
TRect selRect(selRange.selStart.x, 0, selRange.selEnd.x, pWaveLeft->Height);
TCanvas *pCanvas = pWaveLeft->Canvas;
int copyMode = pCanvas->CopyMode;
pCanvas->Draw(0,0, pWaveBmp);
pCanvas->Brush->Color = clActiveBorder;
pCanvas->CopyMode = cmSrcAnd;
pCanvas->Rectangle(selRect);
pCanvas->CopyRect(selRect, pWaveBmp->Canvas, selRect);
pCanvas->CopyMode = copyMode;
if(numChannels == 2){
TCanvas* pOtherCanvas = pWaveRight->Canvas;
pWaveBmp = eContainerView == WAVEFORM ? pWaveBmpRight :
pSfftBmpRight;
pOtherCanvas->Draw(0,0, pWaveBmp);
pOtherCanvas->Brush->Color = clActiveBorder;
pOtherCanvas->CopyMode = cmSrcAnd;
pOtherCanvas->Rectangle(selRect);
pOtherCanvas->CopyRect(selRect, pWaveBmp->Canvas, selRect);
pOtherCanvas->CopyMode = copyMode;
}
}
所以,我使用
cmSrcAnd
复制模式和 CopyRect
方法来进行实际的绘画/绘图( TCanvas
对应于设备上下文( HDC on Windows
)。我认为,因为频谱图与波形不同,实际上并没有单一背景在大多数情况下,使用简单混合复制模式的颜色效果不佳。请注意,我仍然可以完成我想要的操作,但这需要弄乱单个像素,如果可能的话,我想避免这种情况)
我基本上是在寻找能够做到这一点的
API
( VCL
包装 GDI
所以即使 WINAPI 也很好)。任何帮助深表感谢
最佳答案
我将回答我自己的问题,希望这对某些人有用。因为显然没有办法做到这一点
在普通的 VCL
或使用 WINAPI
(某些情况下除外)中,我编写了一个简单的函数,将位图 ( 32bpp / 24bpp
) 与叠加颜色(任何颜色)混合。
实际结果还取决于赋予单个像素的 w0,w1
分量的权重 ( red, green and blue
)。改变这些会产生
分别更倾向于光谱图颜色或叠加颜色的叠加。
代码
Graphics::TBitmap *TSelectionOverlay::GetSelectionOverlay(Graphics::TBitmap *pBmp, TColor selColour,
TRect &rect, EChannel eChannel){
Graphics::TBitmap *pSelOverlay = eChannel==LEFT ? pSelOverlayLeft : pSelOverlayRight;
const unsigned cGreenShift = 8;
const unsigned cBlueShift = 16;
const unsigned overlayWidth = abs(rect.right-rect.left);
const unsigned overlayHeight = abs(rect.bottom-rect.top);
pSelOverlay->Width = pBmp->Width;
pSelOverlay->Height = pBmp->Height;
const unsigned startOffset = rect.right>rect.left ? rect.left : rect.right;
pSelOverlay->Assign(pBmp);
unsigned char cRed0, cGreen0, cBlue0,cRed1, cGreen1, cBlue1, bRedColor0, bGreenColor0, bBlueColor0;
cBlue0 = selColour >> cBlueShift;
cGreen0 = selColour >> cGreenShift & 0xFF;
cRed0 = selColour & 0xFF;
unsigned *pPixel;
for(int i=0;i<overlayHeight;i++){
pPixel = (unsigned*)pSelOverlay->ScanLine[i];//provides access to the pixel array
for(int j=0;j<overlayWidth;j++){
unsigned pixel = pPixel[startOffset+j];
cBlue1 = pixel >> cBlueShift;
cGreen1 = pixel >> cGreenShift & 0xFF;
cRed1 = pixel & 0xFF;
//blend the current bitmap pixel with the overlay colour
const float w0 = 0.5f; //these weights influence the appearance of the overlay (here we use 50%)
const float w1 = 0.5f;
bRedColor0 = cRed0*w0+cRed1*w1;
bGreenColor0 = cGreen0*w0+cGreen1*w1);
bBlueColor0 = cBlue0*w0+cBlue1*w1;
pPixel[startOffset+j] = ((bBlueColor0 << cBlueShift) | (bGreenColor0 << cGreenShift)) | bRedColor0;
}
}
return pSelOverlay;
}
请注意,由于某种原因,
CopyRect
与 CopyMode
的 cmSrcCopy
值一起使用时效果不佳,因此我改用 Draw
。pCanvas->CopyMode = cmSrcCopy;
pCanvas->CopyRect(dstRect, pSelOverlay->Canvas, srcRec);//this still didn't work well--possibly a bug
所以我用
pCanvas->Draw(0,0, pSelOverlay);
结果
关于c++ - 透明的频谱图选择叠加,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48050009/