Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

distorted bits?

Hi again. Well I am able to copy the bitmap I want. The problem now is that the bits are distorted. I'm getting some pretty funky colors, and I'm not sure why. Hope someone has an idea. Thanks.

[code]
CFile dibFile(_T("BITMAP1.bmp"), CFile::modeRead);


//open file

//create memory for the file header

BITMAPFILEHEADER bmpFileHead;

//read the header into memory

if(!dibFile.Read((void*)&bmpFileHead, sizeof(BITMAPFILEHEADER)))
AfxMessageBox("Read failed.");


//check to make sure the file is a valid bitmap

if(bmpFileHead.bfType == ((WORD) ('M' << 8) | 'B'))
{
//need the length of the file for further processing

DWORD fileLength = dibFile.GetLength();

//take the size of the header away from the length, we already have the header in memory

DWORD size = fileLength - sizeof(BITMAPFILEHEADER);

//create a pointer to the bitmap data as a chunk of memory

BYTE* pDib = (BYTE*)malloc(size);
if(!pDib)
AfxMessageBox("Allocation failed");

//Create second pointer so we have somewhere to reverse the data
BYTE* pTest = (BYTE*)malloc(size);
if(!pTest)
AfxMessageBox("Allocation failed");

//read the data into the array

dibFile.Read((void*)pDib, size);
dibFile.Close();

//The following structs contain display information about the bitmap,
//so we read into them for use later

//fill the BITMAPINFO struct from the file data

BITMAPINFO* pBmpInfo = (BITMAPINFO*)pDib;


//fill the BITMAPINFOHEADER struct from the file

BITMAPINFOHEADER* pBmpInfoHead = (BITMAPINFOHEADER*) pDib;


//fill the RGBQUAD struct from the file

RGBQUAD* pRGB = (RGBQUAD*)(pDib + pBmpInfoHead->biSize);


//determine the number of colors used in the bitmap

int numColors;
if((pBmpInfoHead->biClrUsed == 0) && (pBmpInfoHead->biBitCount < 9))
{
switch(pBmpInfoHead->biBitCount)
{
case 1: numColors = 2; break;
case 4: numColors = 16; break;
case 8: numColors = 256;
}
}
else
numColors = (int)pBmpInfoHead->biClrUsed;

if(pBmpInfoHead->biClrUsed == 0)
pBmpInfoHead->biClrUsed = numColors;


//now that we have the number of colors we can determine where the actual image
//data starts, and read it into an array so we can work with it

DWORD clrTable = numColors * sizeof(RGBQUAD);
BYTE* pData = pDib + pBmpInfoHead->biSize + clrTable;

if(pBmpInfoHead->biSizeImage == 0)
{
DWORD height = pBmpInfoHead->biHeight;
DWORD width = pBmpInfoHead->biWidth;

pBmpInfoHead->biSize = height * width;
}

//reverse the data to flip the image.

int s = 0;
for(unsigned g = size;g >= sizeof(BITMAPINFO);g--)
{
pTest[s] = pDib[g];
s++;
}
//create and write to the new file
CFile wFile(_T("test.bmp"), CFile::modeWrite | CFile::modeCreate);

wFile.Write((void*)&bmpFileHead, sizeof(BITMAPFILEHEADER));

wFile.Write((void*)pDib, sizeof(BITMAPINFO));

wFile.Write((void*)pTest, size - sizeof(BITMAPINFO));

wFile.Close();

//clean up

free(pTemp);
free(pTest);
free(pDib);
[/code]

Comments

  • Andre YoungAndre Young USAMember Posts: 0

    ________ / http://forcoder.org / free ebooks and video tutorials about ( Python, Scratch, Delphi, Visual Basic .NET, MATLAB, Assembly, Visual Basic, C++, Go, PHP, Java, Perl, JavaScript, PL/SQL, R, C, Swift, Objective-C, C#, Ruby Prolog, SAS, Scala, Apex, Crystal, Transact-SQL, Lisp, Logo, ABAP, Hack, FoxPro, Scheme, Dart, D, VBScript, Fortran, LabVIEW, ML, Julia, Alice, F#, Ada, Kotlin, Lua, Awk, COBOL, Erlang, Rust, Clojure, Bash ) _______

Sign In or Register to comment.