透明光谱图选择叠加

时间:2018-01-01 13:19:55

标签: c++ transparency vcl

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

选择叠加层在波形视图中正常工作

enter image description here

这里是频谱图视图中的选择叠加(选择看起来非常糟糕并且遮挡了频谱图的部分内容)

enter image description here

两个视图的代码(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我认为,由于光谱图与波形不同,使用简单的混合复制模式并不能真正具有单一的背景颜色,因此在大多数情况下都不能正常工作。

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

我基本上都在寻找APIVCL换行GDI,所以即使WINAPI也可以这样做。

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

我将回答我自己的问题,希望这对某些人有用。因为显然无法实现这一目标 在普通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);

结果

enter image description here