Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 7th, 2005, 7:18 PM   #1
thePaleontologist
Newbie
 
thePaleontologist's Avatar
 
Join Date: Sep 2005
Posts: 8
Rep Power: 0 thePaleontologist is on a distinguished road
Question Q: Bitmaps & Problems (old (256col) DIBs = newDIBs ?)

Hi.
This is not exactly a C++ question, but I encountered this problem while working with MS VC++, so I'll post it here.
I was following a programming tutorial from Sams Teach Yourself Game Programming in 24 Hours, and there was a problem with Hour 5. In this hour, a Bitmap class is developed to be used in latter projects, and it's only supposed to deal with 256 color bitmaps. Than, 'Slideshow' app is developed, to demonstrate functionality of the Bitmap class.
Here was the problem. Three of the bitmaps are supposed to be loaded from a file. At first, I couldn't get this to work. My OS is Widows 2000, and I used simple Paint program to save some pictures as 256 color bitmaps. These images wouldn’t open properly. Eventually, I tried to use bitmaps from the example CD, and they worked fine. Then I used my bitmaps with the Slideshow app from the CD, and they didn't work. At the end, I managed to fix the problem by resaving these images from older version of MS Photo Editor.

So, anyone knows what's all this about? Is there any difference between old and new 256 col DIBs?
Maybe someone knows how to change the code to work with both?

Thanks in advance.
thePaleontologist is offline   Reply With Quote
Old Sep 7th, 2005, 8:01 PM   #2
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
You can rightclick>properties>summary on a bitmap and compare too see if there are any differences between a working and non-working bmp. I know photoshop 5/6 didnt save bitmaps properly as it didnt set the size field in the header (it only set the width + height, had problem with DIBs myself 'bout a couple of years ago), the same thing could be happenning with paint. Only way I can think of solving it would be to load them in yourself (if you've only just started learning may not be an option), or just stick with the program that works .
Animatronic is offline   Reply With Quote
Old Sep 8th, 2005, 6:29 AM   #3
thePaleontologist
Newbie
 
thePaleontologist's Avatar
 
Join Date: Sep 2005
Posts: 8
Rep Power: 0 thePaleontologist is on a distinguished road
Thanks.
As for properties>summary, there's no visible difference. And you're right, I just started with GDI, but I'm a fast learner. I also had the same problem with Photoshop 7. Must be some messy work from Microsoft's programmers ( ).
One more question. There are three more bitmaps that are loaded as resources. There were some problems, too. I taught it was the same thing as with file import, but no. When the image is loaded, its pixels aren't at proper positions (look at the picture). Also note those strange pixels at the bottom line. I really have no idea how to fix this.

Here's the code.
(As far as I can tell, there's nothing wrong with it, except that it's maybe in old style. I'm using a downloaded book I recently found, but the edition dates from December 2002)

//-----------------------
// Create() method (Bitmap class), resource loading version
// (It is called by the corresponding constructor)

BOOL Bitmap::Create(HDC hDC, UINT uiResID, HINSTANCE hInstance)
{
// Free any previous DIB info
Free();

// Find the bitmap resource
HRSRC hResInfo = FindResource(hInstance, MAKEINTRESOURCE(uiResID), RT_BITMAP);
if (hResInfo == NULL)
return FALSE;

// Load the bitmap resource
HGLOBAL hMemBitmap = LoadResource(hInstance, hResInfo);
if (hMemBitmap == NULL)
return FALSE;

// Lock the resource and access the entire bitmap image
PBYTE pBitmapImage = (BYTE*)LockResource(hMemBitmap);
if (pBitmapImage == NULL)
{
FreeResource(hMemBitmap);
return FALSE;
}

// Store the width and height of the bitmap
BITMAPINFO* pBitmapInfo = (BITMAPINFO*)pBitmapImage;
m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth;
m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight;

// Get a handle to the bitmap and copy the image bits
PBYTE pBitmapBits;
m_hBitmap = CreateDIBSection(hDC, pBitmapInfo, DIB_RGB_COLORS,
(PVOID*)&pBitmapBits, NULL, 0);


if ((m_hBitmap != NULL) && (pBitmapBits != NULL))
{
const PBYTE pTempBits = pBitmapImage + pBitmapInfo->bmiHeader.biSize +
pBitmapInfo->bmiHeader.biClrUsed * sizeof(RGBQUAD);
CopyMemory(pBitmapBits, pTempBits, pBitmapInfo->bmiHeader.biSizeImage);

// Unlock and free the bitmap graphics object
UnlockResource(hMemBitmap);
FreeResource(hMemBitmap);

return TRUE;
}

// Something went wrong, so cleanup everything
UnlockResource(hMemBitmap);
FreeResource(hMemBitmap);
Free();
return FALSE;
}

//-----------------------
Attached Images
File Type: jpg BitmapErr.jpg (23.7 KB, 25 views)
thePaleontologist is offline   Reply With Quote
Old Sep 8th, 2005, 10:25 AM   #4
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
Can't really say what's wrong from looking at the function. I never really got the whole DIBsection stuff just normally botched it 'till it worked .

const PBYTE pTempBits = pBitmapImage + pBitmapInfo->bmiHeader.biSize +
pBitmapInfo->bmiHeader.biClrUsed * sizeof(RGBQUAD);

Maybe this line is wrong, why are you skipping over the colour table if there is one, surely you need to copy that as well.

Is there any reason your not using LoadImage() ?

//load a resource
hbitmap = (HBITMAP)LoadImage(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP) ,IMAGE_BITMAP,0,0,LR_SHARED);

//load a file
hbitmap = (HBITMAP)LoadImage(0,pFilePath,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
Animatronic is offline   Reply With Quote
Old Sep 8th, 2005, 12:03 PM   #5
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
@thePaleontologist: use code tags next time
Polyphemus_ is offline   Reply With Quote
Old Sep 8th, 2005, 2:46 PM   #6
thePaleontologist
Newbie
 
thePaleontologist's Avatar
 
Join Date: Sep 2005
Posts: 8
Rep Power: 0 thePaleontologist is on a distinguished road
Thanks (to both) for the advice.

Animatronic:
'Is there any reason your not using LoadImage() ?'

Well, I'm fine with that (as far as I'm concerned), but the book insists on this approach.

Polyphemus:
'@thePaleontologist: use code tags next time '

Sorry, I didn’t' see there was such an option until Animatronic posted his answer.
Maybe moderators can modify my post, since I can't edit it anymore.
thePaleontologist is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:25 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC