【问题标题】:Monochrome Bitmap SetPixel/GetPixel problems... Win32 C Code单色位图SetPixel/GetPixel问题... Win32 C代码
【发布时间】:2010-12-13 03:08:26
【问题描述】:

这是我的一些位掩码代码(单色位图)。 Bitmask_Create() 函数没有问题。我已经通过打开、加载和保存 Windows 单色位图对其进行了测试,效果很好。但是,我制作的 GetPixel 和 SetPixel 函数似乎无法正常工作。在某些情况下,它们似乎工作正常,具体取决于位图尺寸。

如果有人可以提供帮助,我将不胜感激。它快把我逼疯了。 谢谢。

typedef struct _GL_BITMASK GL_BITMASK;
struct _GL_BITMASK {
    int nWidth; // Width in pixels
    int nHeight; // Height in pixels
    int nPitch; // Width of scanline in bytes (may have extra padding to align to DWORD)
    BYTE *pData; // Pointer to the first byte of the first scanline (top down)
};

int BitMask_GetPixel(GL_BITMASK *pBitMask, int x, int y)
{
    INT nElement = ((y * pBitMask->nPitch) + (x / 8));
    PBYTE pElement = pBitMask->pData + nElement;
    BYTE bMask = 1 << (7 - (x % 8));

    return *pElement & bMask;
}

void BitMask_SetPixel(GL_BITMASK *pBitMask, int x, int y, int nPixelColor)
{
    INT nElement = x / 8;
    INT nScanLineOffset = y * pBitMask->nPitch;
    PBYTE pElement = pBitMask->pData + nScanLineOffset + nElement;
    BYTE bMask = 1 << (7 - (x % 8));

    if(*pElement & bMask)
    {
        if(!nPixelColor) return;
        else *pElement ^= bMask;
    }
    else
    {
        if(nPixelColor) return;
        else *pElement |= bMask;
    }
}

GL_BITMASK *BitMask_Create(INT nWidth, INT nHeight)
{
    GL_BITMASK *pBitMask;
    int nPitch;

    nPitch = ((nWidth / 8) + 3) & ~3;

    pBitMask = (GL_BITMASK *)GlobalAlloc(GMEM_FIXED, (nPitch * nHeight) + sizeof(GL_BITMASK));
    if(!pBitMask) 
        return (GL_BITMASK *)NULL;

    pBitMask->nPitch = nPitch;
    pBitMask->nWidth = nWidth;
    pBitMask->nHeight = nHeight;
    pBitMask->pData = (PBYTE)pBitMask + sizeof(GL_BITMASK);

    return pBitMask;
}

【问题讨论】:

    标签: windows bitmap winapi bitmask


    【解决方案1】:

    我认为你计算音高的公式有点偏离。它在宽度是 8 的倍数时有效,否则无效。试试:

    nPitch = ((nWidth + 31) / 8) & ~3;
    

    【讨论】:

    • 我更改了音高代码,但 SetPixel 和 GetPixel 功能仍然不起作用...保存位图时全黑...保存位图功能很好;我已经从加载到位掩码缓冲区的位图中对其进行了测试。 (不过感谢您的建议)
    【解决方案2】:

    我想通了...我在 SetPixel() 中对 nPixelColor 进行了两个测试

    if(*pElement & bMask)
    {
        if(nPixelColor) return; // this was !nPixelColor
        else *pElement ^= bMask;
    }
    else
    {
        if(!nPixelColor) return; // this was nPixelColor
        else *pElement |= bMask;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 2016-09-02
      相关资源
      最近更新 更多