PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

workaround: don't call IMGDECMP.DLL

CodeFlatter · 05 Apr 2009, 12:10 UTC
Ok, this workaround fixes all of my problems with this dll and call's of ::DecompressImageIndirect( &dii; );

1. create a new method prototype
after line 52 at image.h
Code:

// Load an image from a file
Surface* LoadImage( const TCHAR* filename );

// new code!
// Load an image from a file
Surface* LoadImageSH( const TCHAR* filename );[/b]

LoadImageSH calls SHLoadImageFile and don't needs separate memory mapping or decompress.

2. implement the new method at image.cpp
after line 240:
Code:

Surface* LoadImage( const TCHAR* filename )
{
Internal::MemoryFile file( filename );
if (!file.IsOpen())
return 0;

return DecompressImage( file.begin(), file.end() );
}

// new code!
Surface* LoadImageSH( const TCHAR* filename )
{
Surface* pSurface = NULL;

HBITMAP hBitmap = SHLoadImageFile( filename );

if( NULL != hBitmap )
{
pSurface = CreateSurface( hBitmap );
DeleteObject( hBitmap );
}

return pSurface;
}


3. add pragma comment to image.cpp for new bindings
after line 24:
Code:

#ifdef _WIN32_WCE
#include "foreign/imgdecmp/imgdecmp.h"
#pragma comment( lib, "imgdecmp.lib" )
// new code
#pragma comment( lib, "Aygshell.lib" ) //new lib!
#else


4. don't call
Code:

Surface* pSurface = LoadImage( pDisplay, _T( "\\Program Files\\...\\gfx.png" ) );
// or
Surface* pSurface = LoadImage( _T( "\\Program Files\\...\\gfx.png" ) );

better call
Code:

Surface* pSurface = LoadImageSH( _T( "\\Program Files\\...\\gfx.png" ) );


5. recompile PocketFrog AND replace the lib's...

this works fine for me...[/code]
PsychoChris · 05 Apr 2009, 19:37 UTC
Hi CodeFlatter,

i would suggest to completely avoid using SHLoadImageFile() for Windows Mobile 5.x and higher since it has some known issues regarding large images that lead to unpredictable failures while loading some images. Also avoid imgdecmp since it is deprecated. Better use the Imaging API.
It also supports loading thumbs, sizing operations, different decoders and also runs under pure WinCE. Only drawback: A little bit more code and usage of COM. But it is worth the effort.

Regards,
Chris[/url]
All of my games use the SHLoadImageFile, and I haven't run into any problems. In fact, I don't think I even started using that until WM5 because the original code became deprecated.

PsychoChris, do you know how large the image needs to be to start having problems? I know 240x1400ish work just fine.

-John
CodeFlatter · 06 Apr 2009, 19:44 UTC — Thx 4 info
Thanks at PsychoChris and Presto! :)

I will see if I find code examples to use imaging api, com code is more complex...

Best regards
CF :roll:
PsychoChris · 06 Apr 2009, 21:58 UTC
@ Presto:
We encountered not clearly reproducable problems using SHLoadImageFile() with images of dimensions 1600x1200 or larger. Especially images created by cameras of mobile devices. Those images loaded most times fine in the emulator but return NULL handles on devices although memory on the device was more than sufficiant. You can find similar issues by googling around. Solutions suggested by microsoft developers always pointed out avoiding that function and using Imaging API (see this link for instance).
Using the Imaging API we didn't encounter those problems anymore.


@ CodeFlatter:
Take a look at this link. It shows you how to implement loading images with Imaging API for WM 5.x and higher and also includes a fallback solution for older devices not providing that API.


Regards,
Chris
CodeFlatter · 07 Apr 2009, 08:24 UTC — THX
Hello PsychoChris,

thx 4 the link, this page is helpfull - but use MFC code (no one of my mobile app's use MFC).
I need an example without MFC (using of MFC is impossible for me) or an simple code snipped...

Regards, CF
PsychoChris · 07 Apr 2009, 11:57 UTC
Hi CodeFlatter,

the only function using MFC is LoadImageWithImagingApi(). Since MFC just wraps the Windows API you can actually unwrap the code and use Windows API calls instead. Here is how i would do it:

Code:

HBITMAP LoadImageWithImagingApi(LPCTSTR lpFileName)
{
IImagingFactory* pImgFactory = NULL;
IImage* pImage = NULL;
HBITMAP hResultBmp = NULL;

::CoInitializeEx(NULL, COINIT_MULTITHREADED);

if( SUCCEEDED(CoCreateInstance( CLSID_ImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IImagingFactory,
(VOID**)&pImgFactory;) ) )
{
ImageInfo imageInfo;

if( SUCCEEDED(pImgFactory->CreateImageFromFile(lpFileName, &pImage;))
&& SUCCEEDED(pImage->GetImageInfo(&imageInfo;)) )
{
HDC hDc = ::GetDC(NULL);
HDC hDcBitmap = ::CreateCompatibleDC(hDc);
HBITMAP hBitmap = ::CreateCompatibleBitmap(hDc, imageInfo.Width, imageInfo.Height);
if( hBitmap )
{
RECT rcBitmap;
HGDIOBJ hOldBitmap = ::SelectObject(hDcBitmap, hBitmap);
if( ::SetRect(&rcBitmap;, 0, 0, imageInfo.Width, imageInfo.Height)
&& pImage->Draw(hDcBitmap, &rcBitmap;, NULL) == S_OK )
hResultBmp = hBitmap;

::SelectObject(hDcBitmap, hOldBitmap);
}

::ReleaseDC(NULL, hDc);
::ReleaseDC(NULL, hDcBitmap);

pImage->Release();
}
pImgFactory->Release();
}

::CoUninitialize();

return hResultBmp;
}


Regards,
Chris
CodeFlatter · 07 Apr 2009, 12:08 UTC — Nice!
Hi Chris,

thats's really nice, thank you very much!!! :D

Best regards, CF

...and works fine! i'm happy now :lol:
Thanks for the great info and code, Chris!

Good luck with your app, CF!
CodeFlatter · 09 Apr 2009, 08:14 UTC
Thx Presto and dito. :-)