Pregunta

Esto es parte de mi código de máscara de bits (mapa de bits monocromo). No hay ningún problema con la función Bitmask_Create (). He probado con la apertura, cargar y guardar mapas de bits monocromo ventanas, y funciona muy bien. Sin embargo, las funciones y GetPixel SetPixel que he hecho no parecen derecho del trabajo. En algunos casos parece que funcionan bien en función de las dimensiones de mapa de bits.

Si alguien puede ayudar, yo lo apreciaría. Me está volviendo loca. Gracias.

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;
}
¿Fue útil?

Solución

Creo que su fórmula de cálculo de tono es sólo un poco apagado. Funciona cuando la anchura es un múltiplo de 8, pero no de otra manera. Proveedores:

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

Otros consejos

lo he descubierto ... Tenía las dos pruebas revés para nPixelColor en SetPixel ()

if(*pElement & bMask)
{
    if(nPixelColor) return; // this was !nPixelColor
    else *pElement ^= bMask;
}
else
{
    if(!nPixelColor) return; // this was nPixelColor
    else *pElement |= bMask;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top