Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Windos
win32.3rdparty
win32.directx.audio
win32.directx.ddk
win32.directx.graphics
win32.directx.input
win32.directx.managed
win32.directx.misc
win32.directx.networking
win32.directx.sdk
win32.directx.video
win32.dirx.grap.shaders
win32.gdi
win32.international
win32.kernel
win32.messaging
win32.mmedia
win32.networks
win32.ole
win32.rtc
win32.tapi
win32.tapi.beta
win32.tools
win32.ui
win32.wince
win32.wmi
windows.mediacenter
winfx.aero
winfx.announcements
winfx.avalon
winfx.collaboration
winfx.fundamentals
winfx.general
winfx.indigo
winfx.sdk
winfx.winfs
  
 
date: Sat, 5 Jul 2008 13:26:27 +0800,    group: microsoft.public.win32.programmer.directx.video        back       


setting BITMAPINFO for RGB565   
I need to create a RGB565 bitmap via CreateDIBSection. I set the header bit 
mask using:

 BITMAPINFO dibInfo;
 dibInfo.bmiHeader.biBitCount = 16;
 dibInfo.bmiHeader.biClrImportant = 0;
 dibInfo.bmiHeader.biClrUsed = 0;
 dibInfo.bmiHeader.biCompression = BI_BITFIELDS;
 dibInfo.bmiHeader.biHeight = frameHeight;
 dibInfo.bmiHeader.biWidth = frameWidth;
 dibInfo.bmiHeader.biPlanes = 1;
 dibInfo.bmiHeader.biSize = 40;
 dibInfo.bmiHeader.biSizeImage = frameWidth*frameHeight*2;

 //set bitmap bits to indicate RGB 565
 unsigned long *ptr = (unsigned long *)(dibInfo.bmiColors);
 ptr[0] = 0xf800;
 ptr[1] = 0x07e0;
 ptr[2] = 0x001f;

The bitmasks value of 0xf800 (red), 0x07e0 (green) and 0x001f(blue) are 
taken from MSDN. After first looking at the documentation, a very obvious 
way would be setting dibInfo.bmiColors->rgbBlue, etc. However, this could 
not work because the values are too large for rgbBlue, which is BYTE. After 
googling, I came up with above approach.

This works only if CreateDIBSection is called immediately after the 
assignment to ptr[2]. After a while, the assignments for ptr[0], ptr[1], 
ptr[2] seems to have some side effects on other variables, e.g. random 
change of variable values and eventually access violations.

Any ideas how I can avoid this? Maybe assigning some other specific values 
to dibInfo.bmiColors->rgbBlue , instead of setting them via pointer?
date: Sat, 5 Jul 2008 13:26:27 +0800   author:   minhdanh

Re: setting BITMAPINFO for RGB565   
Hi,

> I need to create a RGB565 bitmap via CreateDIBSection. I set the header bit
> mask using:
>
>  BITMAPINFO dibInfo;

You are creating a variable on stack and you are writing beyond
allocated stack memory. You are messing memory by initializing ptr[]
and later as soon as stack is used for other purposes these values are
messed again by different code.

You need something like:

BYTE pnDibInfoMemory[sizeof (BITMAPINFOHEADER)  sizeof (DWORD) * 3];
BITMAPINFOHEADER* pBitmapInfoHeader = (BITMAPINFOHEADER*)
pnDibInfoMemory;
pBitmapInfoHeader->... = ...
DWORD* pnBitFields = (DWORD*) (pBitmapInfoHeader  1);
pnBitFields[0] = ...
pnBitFields[1] = ...
pnBitFields[2] = ...

Note that first line BYTE[] allocates additional memory for three
trailing DWORDs, this is what you needed.

Roman
date: Fri, 4 Jul 2008 23:52:43 -0700 (PDT)   author:   Roman Ryl...

Re: setting BITMAPINFO for RGB565   
Hi,

If you are using ATL, it could be a bit easier:

CTempBuffer<BITMAPINFOHEADER> pBitmapInfoHeader(sizeof
(BITMAPINFOHEADER) + sizeof (DWORD) * 3);
pBitmapInfoHeader->...

Roman
date: Fri, 4 Jul 2008 23:55:33 -0700 (PDT)   author:   Roman Ryl...

Re: setting BITMAPINFO for RGB565   
I got it working as per your suggestions, thanks a lot ;)

Just one quick question, do I need to clean up the BITMAPINFOHEADER variable 
to prevent memory leak? How do I do so?
date: Sat, 5 Jul 2008 23:43:09 +0800   author:   minhdanh

Re: setting BITMAPINFO for RGB565   
Hi,

> I got it working as per your suggestions, thanks a lot ;)
>
> Just one quick question, do I need to clean up the BITMAPINFOHEADER variable
> to prevent memory leak? How do I do so?

No, you don't need to.

Roman
date: Sat, 5 Jul 2008 10:08:55 -0700 (PDT)   author:   Roman Ryl...

Re: setting BITMAPINFO for RGB565   
"Roman Ryl..."  wrote:
>
>If you are using ATL, it could be a bit easier:
>
>CTempBuffer<BITMAPINFOHEADER> pBitmapInfoHeader(sizeof
>(BITMAPINFOHEADER) + sizeof (DWORD) * 3);

Have you tried that?  It doesn't do what you think it does.  That will
allocate enough space for 52 copies of a BITMAPINFOHEADER (2080 bytes),
*NOT* 52 bytes.

However, this would work:

  CTempBuffer<BITMAPINFOHEADER,
     sizeof(BITMAPINFOHEADER) + sizeof(DWORD) * 3> pBitmapInfoHeader( 0 );
-- 
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
date: Sat, 05 Jul 2008 22:10:53 -0700   author:   Tim Roberts

Re: setting BITMAPINFO for RGB565   
Tim,

> >If you are using ATL, it could be a bit easier:
>
> >CTempBuffer<BITMAPINFOHEADER> pBitmapInfoHeader(sizeof
> >(BITMAPINFOHEADER)  sizeof (DWORD) * 3);
>
> Have you tried that?  It doesn't do what you think it does.  That will
> allocate enough space for 52 copies of a BITMAPINFOHEADER (2080 bytes),
> *NOT* 52 bytes.
>
> However, this would work:
>
>   CTempBuffer<BITMAPINFOHEADER,
>      sizeof(BITMAPINFOHEADER)  sizeof(DWORD) * 3> pBitmapInfoHeader( 0 );

You are correct, original allocation is incorrect. However yours is
also not quite how it should be (it creates sufficient buffer on stack
but request zero bytes to be "allocated" from it). It should be this
way:

CTempBuffer<BITMAPINFOHEADER> pBitmapInfoHeader;
ATLVERIFY(pBitmapInfoHeader.AllocateBytes(sizeof(BITMAPINFOHEADER) 
sizeof(DWORD) * 3));

Roman
date: Sun, 6 Jul 2008 00:33:16 -0700 (PDT)   author:   Roman Ryl...

Re: setting BITMAPINFO for RGB565   
On Sun, 6 Jul 2008 00:33:16 -0700 (PDT), Roman Ryl... wrote:

>>   CTempBuffer<BITMAPINFOHEADER,
>>      sizeof(BITMAPINFOHEADER) + sizeof(DWORD) * 3> pBitmapInfoHeader( 0 );
> 
> You are correct, original allocation is incorrect. However yours is
> also not quite how it should be (it creates sufficient buffer on stack
> but request zero bytes to be "allocated" from it). It should be this
> way:
> 
> CTempBuffer<BITMAPINFOHEADER> pBitmapInfoHeader;
> ATLVERIFY(pBitmapInfoHeader.AllocateBytes(sizeof(BITMAPINFOHEADER) +
> sizeof(DWORD) * 3));


Tim's version looks fine to me.  Did you look at what happens if you call
the constructor with '0'?

-- 
Please read this before replying:
1. Dshow & posting help:  http://tmhare.mvps.org/help.htm
2. Trim & respond inline (please don't top post or snip everything)
3. Benefit others:  follow up if you are helped or you found a solution
date: Sun, 6 Jul 2008 07:58:38 -0600   author:   The March Hare [MVP] erland

Re: setting BITMAPINFO for RGB565   
"Roman Ryl..."  wrote:
>> >If you are using ATL, it could be a bit easier:
>>
>> >CTempBuffer<BITMAPINFOHEADER> pBitmapInfoHeader(sizeof
>> >(BITMAPINFOHEADER) + sizeof (DWORD) * 3);
>>
>> Have you tried that?  It doesn't do what you think it does.  That will
>> allocate enough space for 52 copies of a BITMAPINFOHEADER (2080 bytes),
>> *NOT* 52 bytes.
>>
>> However, this would work:
>>
>>   CTempBuffer<BITMAPINFOHEADER,
>>      sizeof(BITMAPINFOHEADER) + sizeof(DWORD) * 3> pBitmapInfoHeader( 0 );
>
>You are correct, original allocation is incorrect. However yours is
>also not quite how it should be (it creates sufficient buffer on stack
>but request zero bytes to be "allocated" from it).

Actually, if the requested allocation is less than the "minimum" size in
the second template parameter, it uses the "minimum" size, so my code will
work.  However...

>It should be this way:
>
>CTempBuffer<BITMAPINFOHEADER> pBitmapInfoHeader;
>ATLVERIFY(pBitmapInfoHeader.AllocateBytes(sizeof(BITMAPINFOHEADER) +
>sizeof(DWORD) * 3));

...I had not noticed CTempBuffer before this week.  After I made my post, I
dug in more thoroughly and came to the same conclusion you did.  Both our
schemes will work, but I believe yours is more sensible.
-- 
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
date: Mon, 07 Jul 2008 21:41:27 -0700   author:   Tim Roberts

Re: setting BITMAPINFO for RGB565   
>>> Have you tried that? It doesn't do what you think it does.

Another idea I got from MSDN forum is

struct tagBITMAPINFO2
{
BITMAPINFOHEADER bmh;
DWORD arrBitMasks[3];
} dibInfo;
...
CreateDibSection(hdc, (const BITMAPINFO* )&dibInfo, ... )
date: Wed, 9 Jul 2008 21:24:47 +0800   author:   minhdanh

Re: setting BITMAPINFO for RGB565   
Hi,

> Another idea I got from MSDN forum is
>
> struct tagBITMAPINFO2
> {
> BITMAPINFOHEADER bmh;
> DWORD arrBitMasks[3];} dibInfo;
>
> ...
> CreateDibSection(hdc, (const BITMAPINFO* )&dibInfo, ... )

This one is going to work also and you might like it even more,
especially if you need more than one time use solution, however please
be aware of struct alignment: your struct should have no extra
alignment between bmh and arrBitMasks inserted by compiler. You can
make sure it is this way by specifying alignment explicitly:

typedef struct __declspec(align(1)) tagBITMAPINFO2
{
	BITMAPINFOHEADER bmh;
	DWORD arrBitMasks[3];
} BITMAPINFO2;

...

BITMAPINFO2 BitmapInfo;

Roman
date: Thu, 10 Jul 2008 14:29:14 -0700 (PDT)   author:   Roman Ryl...

Re: setting BITMAPINFO for RGB565   
From: "Roman Ryl..."

> solution, however please be aware of struct alignment:
> your struct should have no extra alignment between bmh
> and arrBitMasks inserted by compiler. You can make sure
> it is this way by specifying alignment explicitly:
>
> typedef struct __declspec(align(1)) tagBITMAPINFO2
> {
> BITMAPINFOHEADER bmh;
> DWORD arrBitMasks[3];
> } BITMAPINFO2;

__declspec(align(x)) specifies the alignment for BITMAPINFO2 
variables not the packing of the BITMAPINFO2 fields. You 
should use #pragma pack() instead:

#pragma pack(1)
typedef struct tagBITMAPINFO2
{
  BITMAPINFOHEADER bmh;
  DWORD arrBitMasks[3];
} BITMAPINFO2;
#pragma pack()

However, since sizeof(BITMAPINFOHEADER) is 40, the compiler 
will not insert any padding in between the fields whatever 
the packing (1 to 8, even 16 should be ok).


-- 
// Alessandro Angeli
// MVP :: DirectShow / MediaFoundation
// mvpnews at riseoftheants dot com
// http://www.riseoftheants.com/mmx/faq.htm
date: Thu, 10 Jul 2008 18:57:48 -0400   author:   Alessandro Angeli

Re: setting BITMAPINFO for RGB565   
Alessandro,

> __declspec(align(x)) specifies the alignment for BITMAPINFO2 variables not
> the packing of the BITMAPINFO2 fields.

You are correct, thanks for correcting me here.

Roman
date: Thu, 10 Jul 2008 22:55:49 -0700 (PDT)   author:   Roman Ryl...

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us