c++ - 透明的频谱图选择叠加

标签 c++ transparency vcl

我正在尝试在频谱图上创建透明的选择叠加层,但效果不佳。我的意思是结果不是很令人满意。相比之下,绘制在波形顶部的叠加效果很好,但我需要同时支持波形和频谱图 View (以及 future 可能的其他 View )

选择覆盖在波形 View 中工作正常

enter image description here

这是频谱图 View 中的选择叠加层(选择看起来很糟糕,遮住了部分频谱图)

enter image description here

两个 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 对应于一个设备上下文(Windows 上的 HDC)。我认为,由于频谱图与波形不同,它实际上没有单一的背景颜色,因此在大多数情况下使用简单的混合复制模式效果不佳。

请注意,我仍然可以完成我想要的,但这需要弄乱各个像素,这是我希望尽可能避免的事情)

我主要是在寻找能够执行此操作的 API(VCL 包装 GDI,因此即使 WINAPI 也可以)。

非常感谢任何帮助

最佳答案

我将回答我自己的问题,希望这对某些人有用。因为这显然不可能实现 在普通 VCL 或使用 WINAPI 中(某些情况除外),我编写了一个混合位图的简单函数 (32bpp/24bpp ) 叠加颜色(任何颜色)。 实际结果还取决于赋予单个像素的红色、绿色和蓝色 分量的权重 (w0,w1)。改变这些会产生 分别更倾向于频谱图颜色或叠加层颜色的叠加层。

代码

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;
}

请注意,由于某些原因,CopyRectcmSrcCopyCopyMode 值一起使用效果不佳,因此我使用了 Draw 代替。

pCanvas->CopyMode = cmSrcCopy;
pCanvas->CopyRect(dstRect, pSelOverlay->Canvas, srcRec);//this still didn't work well--possibly a bug

所以我用了

pCanvas->Draw(0,0, pSelOverlay);

结果

enter image description here

关于c++ - 透明的频谱图选择叠加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48050009/

相关文章:

c++ - 如何用clang++编译boost单元测试模块?

c++ - 这个 string_to_number 函数有什么问题?

iphone - UIView背景图片透明度问题

delphi - 在 Delphi 中使用 TOwnedCollection 后代

delphi - 创建表单时不会调用 TPicture 属性的 Set 方法

c++ - 增加堆栈大小以使用 alloca()?

C++ 编译器错误 'no match for call to function'

html - ie6 显示透明背景的图像 (.PNG) 背景颜色

plot - 在保留调色板的同时使用 Gnuplot 矩阵中特定值的透明度?

delphi - 避免 SetFocus 引发异常