Rotating video using GDI+

Rotating video using GDI+

Post by jjack99 » Sat, 07 Jun 2008 11:10:05


Hi,

I'm writing a rotate filter and figured to use GDI+ since it has
builtin functions that would do what I want. I'm able to add text to
the bitmap without problem but when I try to rotate the bitmap 180
degrees the video is displayed without being rotated but the text
displays. To check to see what the bitmap looks like I save it to a
file and when I look at that file it's rotated. The following is my
Transform() code:

HRESULT CSPRotateFilter::Transform(IMediaSample *pMediaSample)
{
BYTE *pData; // Pointer to the actual image buffer

AM_MEDIA_TYPE* pType = &m_pInput->CurrentMediaType();
VIDEOINFOHEADER *pvi = (VIDEOINFOHEADER *) pType->pbFormat;
ASSERT(pvi);

CheckPointer(pMediaSample,E_POINTER);
pMediaSample->GetPointer(&pData);

// Create the GDI+ Bitmap object
BITMAPINFOHEADER bmih = pvi->bmiHeader;
BITMAPINFO* pbmi = (BITMAPINFO*)&bmih;

Bitmap bitmap(pbmi,pData);
Graphics g(&bitmap);
int status;

// setup text output
wchar_t* string = L"This is a test";
Font myFont(L"Arial",18);
RectF layout(0.0f,0.0f,200.0f,50.0f);
StringFormat format;
SolidBrush blue(Color(255,0,0,255));

// output the text
format.SetAlignment(StringAlignmentCenter);
g.DrawString(string,wcslen(string),&myFont,layout,&format,&black);

// Rotate bitmap 180 degrees
bitmap.RotateFlip(Rotate180FlipNone);

// Save bitmap to disk to check it
CLSID bmpClsid;
GetEncoderClsid(L"image/bmp", &bmpClsid);
bitmap.Save(L"c:\\images\\foobar.bmp",&bmpClsid);

return NOERROR;
}

I also need to know how to specify the output format in case the
rotation is 90 or 270 degrees, meaning the width and height has to be
swapped.

Thanks!!!

- Jim
 
 
 

Rotating video using GDI+

Post by The March » Sat, 07 Jun 2008 11:52:03


Does this line re-use pData in "bitmap" or copy it? (not familiar with
GDI+, but the latter would be my guess on the problem)


--
Please read this before replying:
1. Dshow & posting help: http://www.yqcomputer.com/
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

 
 
 

Rotating video using GDI+

Post by jjack99 » Sat, 07 Jun 2008 12:23:21

On Jun 5, 7:52爌m, "The March Hare [MVP]"



That line causes the bitmap to use that buffer. It is not a copy. I
actually output the text string to that bitmap and it shows up in the
video.
 
 
 

Rotating video using GDI+

Post by The March » Sat, 07 Jun 2008 13:21:07


Indeed, so you said in your OP :)

What happens if you move the RotateFlip line before the Graphics object "g"
is created?


--
Please read this before replying:
1. Dshow & posting help: http://www.yqcomputer.com/
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
 
 
 

Rotating video using GDI+

Post by rep_movs » Sat, 07 Jun 2008 16:58:05

Hi

It may be pure ocean foam, but I think RotateFlip doesnt actually
modify the contents of the underlying bitmap data , but instead just
sets some mapping coordinates internally. GDI+ probably uses that
mapping when you write it to a file or draw it elsewhere.

Try a small experiment, add some code to draw some more text after the
rotateflip and see what you get...
Another thing to try is to make a bitmap say half white and half black
(vertically), observe the contents of the DIB data in the de ***
memory window ( to do this, first set a breakpoint before calling
RotateFlip(), when it breaks, get the pointer value of pData, paste
the HEX value into the memory view window, and then step through the
RotateFlip(). If It did rotate the contents the memory view window
will clearly show the bytes go from FFFFF... to 000000... for the
abovementioned bitmap )

To rotate by any number of degrees, You need to use the
RotateTransform() method.

Note however that GDI+ isn't hardware accelerated, so it hogs a huge
amount of CPU for this kind of stuff.

Vivek
 
 
 

Rotating video using GDI+

Post by jjack99 » Sun, 08 Jun 2008 00:43:03


Yeah, I figured that out this morning about 3am... It looks like I'm
going to have to rotate the input bitmap and DrawImage() to the output
bitmap. I'm keeping my fingers crossed that it's not too slow. I've
got a couple of other filters that the graph requires but those use
simple GDI text and image blits. How does hardware acceleration apply
to a transform filter like this? I thought hardware acceleration was
only used when rendered. I figured my only alternative is to create
DIB sections of both input and output and do a bitblit with world
transforms setup for the rotation. Would that be faster?

- Jim