• 0

[c/c++] JPG to BMP conversion


Question

20 answers to this question

Recommended Posts

  • 0

Just in case you're doing it on Windows, here's an example of converting a jpeg to bmp:

#include <windows.h>
#include <gdiplus.h>

#define WINDOWS_LEAN_AND_MEAN
using namespace Gdiplus;

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
	UINT  num = 0;		  // number of image encoders
	UINT  size = 0;		 // size of the image encoder array in bytes

	ImageCodecInfo* pImageCodecInfo = NULL;

	GetImageEncodersSize(&num, &size);
	if(size == 0)
		return -1;  // Failure

	pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
	if(pImageCodecInfo == NULL)
		return -1;  // Failure

	GetImageEncoders(num, size, pImageCodecInfo);

	for(UINT j = 0; j < num; ++j)
	{
		if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
		{
			*pClsid = pImageCodecInfo[j].Clsid;
			free(pImageCodecInfo);
			return j;  // Success
		}
	}
	free(pImageCodecInfo);
	return -1;
}

int main(void){

	GdiplusStartupInput startupInput;
	ULONG_PTR token;
	GdiplusStartup(&token, &startupInput, NULL);
	Image testImg(L"c:\\test.jpg", false);
	CLSID clsid;
	int ret = -1;

	if(-1 != GetEncoderClsid(L"image/bmp", &clsid)){
		ret = testImg.Save(L"c:\\test.bmp", &clsid);
	}

	GdiplusShutdown(token);
	return ret;
}

  • 0

BMP is a Windows (and OS/2) format. The HBITMAP handle is very much a Windows-specific thing. Why do you want this to be cross-platform? And you do realize that, even for .bmp files, there are many different types of bitmaps, right? So... um, are you really sure you need/want this to be cross-platform?

  • 0
I would prefer not to use the Gdiplus namespace.

That's a really bad reason to reject this approach. First, you don't have to use that namespace. Or, you can squirrel all this away in a separate source file. Etc.

The alternative is to roll your own implementation (or copy some existing implementation), which is wasteful if you can avoid it by using a system library like GdiPlus or WIC (Vista or later).

  • 0

Gdiplus doesn't even work on my machine.

I don't have the header files and libraries with my compiler(mingw32-gcc).

I've tried CxImage,ipl98,DevIL, and a bunch of other Image Processing Libraries and none of them worked

(probably because they were coded with MS VC++ 6, and I'm using mingw)

BTW I want to convert a 24-bit JPG to a 24-bit bitmap

(another thing)

I already tried using a command-line image converter via system() but it didn't work.

  • 0

So it's not cross-platform you're after, it's cross-compiler...

If you're developing on Windows, you are going to need the Windows SDK no matter what compiler you use. And as a bonus, the SDK also contains a free copy of MSFT's own compiler that you can use (though mingw-gcc should work for GDI+).

GDI+ actually uses a regular, flat C API (which is definitely cross-compiler-safe), that is then wrapped in a bunch of C++ classes via the headers (which you get through the SDK) so that C++ users can pretend that GDI+ is C++. As a last resort, you can always just bypass the middleman and use GDI+ as any old set of flat C APIs.

  • 0

Funny, I was about to suggest CxImage, after my pleasant experience with it in a recent project. It compiles fine for me on both MinGW 3.4.5 (stable) and MinGW 4.4.0 (testing) (with the latest Win32API, of course.) I had to make some slight modifications, though, to get it to compile with libJPEG and libPNG. It should work like a charm for what you need.

  • 0
Well, actually it is cross-platform because unix/macosx have gcc.

If you intend to have your program run on *nix without Wine, then it's cross-platform, and you probably shouldn't use GDI+ (you still can, if you find an alternative method for your *nix builds) (note that BMP files are used much outside of Windows). If you intend your program to run only on Windows, then it's cross-compiler (even if the compiler can compile for other platforms).

  • 0
Funny, I was about to suggest CxImage, after my pleasant experience with it in a recent project. It compiles fine for me on both MinGW 3.4.5 (stable) and MinGW 4.4.0 (testing) (with the latest Win32API, of course.) I had to make some slight modifications, though, to get it to compile with libJPEG and libPNG. It should work like a charm for what you need.

Do you think you could send me a CxImage Makefile? Or tell me how to compile it.

Before I couldn't get it to compile.

If you intend to have your program run on *nix without Wine, then it's cross-platform, and you probably shouldn't use GDI+ (you still can, if you find an alternative method for your *nix builds) (note that BMP files are used much outside of Windows). If you intend your program to run only on Windows, then it's cross-compiler (even if the compiler can compile for other platforms).

Well, I'm developing this for both Ubuntu and Windows, maybe even Mac so...

  • 0
Useless code.

You can do it with 1 api call on Windows (Shell)...

So... Instead of you being an arrogant **** head, why not post the code and enlighten us? Jeezus, now I remember why I stopped posting here. I mostly code for the web these days, so forgive my ignorance of the shell that I haven't even looked at in years, O Mighty One!

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.