Monochrome Bitmap problèmes SetPixel / getPixel ... Win32 code C
Question
Ceci est une partie de mon code bitmask (bitmaps monochromes). Il n'y a pas de problème avec la fonction Bitmask_Create (). Je l'ai testé avec ouverture, fenêtres chargement et l'enregistrement monochrome bitmaps, et il fonctionne très bien. Cependant, les fonctions getPixel et setPixel que j'ai fait ne semble pas fonctionner à droite. Dans certains cas, ils semblent fonctionner très bien en fonction des dimensions bitmap.
Si quelqu'un pouvait aider, je serais reconnaissant. Ça me rend fou. Merci.
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;
}
La solution
Je pense que votre formule pour le calcul est pas juste un peu au large. Cela fonctionne lorsque la largeur est un multiple de 8, mais pas autrement. Essayez:
nPitch = ((nWidth + 31) / 8) & ~3;
Autres conseils
je me suis dit qu'il ... J'avais les deux tests en arrière pour nPixelColor dans SetPixel ()
if(*pElement & bMask)
{
if(nPixelColor) return; // this was !nPixelColor
else *pElement ^= bMask;
}
else
{
if(!nPixelColor) return; // this was nPixelColor
else *pElement |= bMask;
}