Create device-independent bitmap without allocating memory - winapi

This is what I currenty do to load images in my application:
auto b = ::FreeImage_Load(type, path.c_str());
void *bits;
auto hbmp = CreateDIBSection(
dc,
FreeImage_GetInfo(bitmap),
DIB_RGB_COLORS,
&bits,
0,
0
);
std::memcpy(bits, FreeImage_GetBits(b), size);
It works very well, but I'd like to avoid allocating memory twice - ::FreeImage_Load already stores data in a way that's suitable for dib. Is there a way of calling CreateDIBSection that would prevent allocation and would force dib created that way to use buffer provided by me? If not, is there another method that would allow that?
It seems that providing NULL instead of **ppvBits prevents allocation - is there a way to modify buffer address later? I've also tried tinkering with hSection parameter but dibs created that way were incorrect.

I guess I didn't get your idea wrong.
Maybe you can use CreateDIBitmap to convert FreeImage images to HBITMAP.
FIBITMAP *dib = FreeImage_Load(type, path.c_str());
// ...
HBITMAP bitmap = CreateDIBitmap(hDC, FreeImage_GetInfoHeader(dib),
CBM_INIT, FreeImage_GetBits(dib), FreeImage_GetInfo(dib), DIB_RGB_COLORS);
// ...
FreeImage_Unload(dib);
Related Link: How do I convert a FreeImage image to a HBITMAP ?
If you want to use CreateDIBSection, you can use a non-null hSection argument and use CreateFileMapping and MapViewOfFileEx() to create the section, the latter's lpBaseAddress should point to your bits.
Remember to fully initialize the parameters in MapViewOfFileEx and CreateFileMapping.
Here is a similar discussion you can refer: How to create an HBITMAP wrapping an existing byte buffer?

Related

How do you get the value Gdiplus::Image::GetHorizontalResolution() returns via a HBITMAP?

I've used Gdiplus::Image::GetHorizontalResolution() in printing calculations. But now I'm wondering if I have just a HBITMAP in a generic print function, how do I get that same value Gdiplus::Image::GetHorizontalResolution() would return without having to setup a Gdiplus::Image object?
Thanks!

freeing pwfx after waveOutOpen throws

According to Microsoft documentation:
"You can free this [pwfx] structure immediately after passing it to waveOutOpen."
But this code dosen't seem to agree:
pwfx=new WAVEFORMATEX;
pwfx->wFormatTag=WAVE_FORMAT_PCM;
pwfx->nChannels=2;
pwfx->nSamplesPerSec=SPS;
pwfx->nAvgBytesPerSec=SPS*2;
pwfx->nBlockAlign=2;
pwfx->wBitsPerSample=8;
mmres=waveOutOpen(&ghwo,uDeviceID,pwfx,dwCallback,dwCallbackInstance,fdwOpen);
delete pwfx;
The only problem I can see in the code you provided is that you did not fully initialise the struct. You did not initialise cbSize which in this instance must be set to 0.
Given that you are not allocating any extra data at the end of this struct, there's no need to allocate it in the heap.
It's entirely plausible that the problem lies in the other parameters that you pass to the function. We can't see any details of them, and therefore can't comment.
You don't need to new or delete anything. You can just do:
WAVEFORMATEX wfx = { };
wfx.wFormatTag=WAVE_FORMAT_PCM;
...
mmres=waveOutOpen(&ghwo,uDeviceID,&wfx,dwCallback,dwCallbackInstance,fdwOpen);
Does that help at all?

How to create a Mat from byte[] with JavaCV

Trying to use JavaCV for image processing to rotate image according to it's Exif data.
Reading and writing from file to either Mat or IplImage works, but since the file is being upload or downloaded, I also want to be able to do the same thing processing byte[] instead of having to write to file.
However, I cannot find how to create a Mat instance from byte[].
The method 'aMat.put(0, 0, byteArray)' which is mentions in some answers is not available on a Mat instance in javacp version 1.0 using javacpp-presets:opencv:3.0.0.
Trying to put the bytes in the Mat data via : 'aMat.data().put(imageBytes, 0, 0)' throws a NPE because data() returns null. I cannot find how to set the data since it is a JNI call.
Any idea's on how to create a opencv_core.Mat from byte[]?
Did you try using:
yourMat.data().put(yourByteArray);
Just make sure yourMat is of the right size.

JavaCV IplImage.createFrom hangs in place

Not sure why this is happening. The method :
IplImage.createFrom(image);
Is hanging without returning any value. I've tried multiple images, and confirmed their existence. I'm writing an application that harnesses template matching, however this initial step is giving me a headache. Does anyone know why this method would suspend the thread and not return any value? I've done some research, and confirmed my OpenCV path is set up, and that all my libraries are properly setup.
Before converting a BufferedImage to iplimage we need to create an iplimage which has same height and width as the BufferedImage. Try this code:
IplImage ipl_image = IplImage.create(buffered_image.getWidth(),buffered_.getHeight(),IPL_DEPTH_8U,1);
ipl_image = IplImage.createFrom(buffered_image);

Copy embedded structure to clipboard and retrieve it in win32

I am trying to implement clipboard operation (cut/copy/paste) on my win32 window. This window has a bunch of gdi objects drawn over it, and the window can have child controls inserted in it as well.
I have searched allot on the win32 clipboard API, and every where they have explained how to handle a single type of data, e.g. we can write text onto clipboard by specifying the appropriate clipboard format and etc.
What I need is to place ALL the data on the clipboard that will be used to reconstruct the original window after the paste operation. I do not want to use COM as suggested by msdn for embedded data structures.
Can this be carried out using the basic clipboard API? Can anybody point me in the right direction and what steps do I need to take to bring this about? I am a newbie in win32 and don't know allot about what I am doing.
Use RegisterClipboardFormat() to register a custom clipboard format ID. Then serialize your data as needed, using whatever serialization format is meaningful for your data, and store it on the clipboard using SetClipboardData(). At a later time, you can use GetCliipboardData() to retrieve your data and de-serialize it as needed.
Update: For example:
struct sMyData
{
int Value1;
int Value2;
float Value3;
float Value4;
};
UINT uMyDataFmtID = RegisterClipboardFormat(TEXT("MyData"));
...
HANDLE hMyData = GlobalAlloc(GHND, sizeof(sMyData));
sMyData *pMyData = (sMyData*) GlobalLock(hMyData);
// fill in pMyData as needed...
GlobalUnlock(hMyData);
SetClipboardData(uMyDataFmtID, hMyData);
...
HANDLE hMyData = GetClipboardData(uMyDataFmtID);
sMyData *pMyData = (sMyData*) GlobalLock(hMyData);
// use pMyData as needed...
GlobalUnlock(hMyData);

Resources