• 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.
  • Posts

    • Remarkably based article from Garter - apparently there IS someone working there that actually understands mainframe systems.
    • Calling it TDS doesn’t answer the question: why did this site skip prior UFC games but review this one, at this exact political moment?
    • Its the timing of its release, mixed with the timing of the review mixed with the timing of the event, when this site never reviewed previous iterations of this game that I could find.
    • Instagram just got "a long-requested feature" by Hamid Ganji Instagram has finally added a new feature that has been "long-requested" and it allows users to write an individual caption for each image or video in a carousel. The feature will start rolling out this week, and it’ll be available to all users. Previously, Instagram only allowed a single caption for all regular posts and carousels. This made it harder for content creators to describe every scene in a carousel and often forced them to write longer captions. However, with this new feature, every slide in a carousel can have its own caption, which can be viewed by swiping left or right. To use the feature, when writing a caption for your post, select “Multiple captions” from the dropdown menu in the caption area. You can then write unique captions for each slide. By swiping through the carousel and selecting individual slides, a dedicated space for writing captions will appear. Instagram says the feature will help audiences get the right context at the right moment. The ability to assign a unique caption to each slide in a carousel could be particularly useful for content creators and brands on the platform. Instagram says the feature will begin rolling out this week. The new feature appears to have been well received by Instagram users, many of whom have been requesting it for a long time. However, users are now asking for additional features, such as the ability to add new slides to a carousel after it has been published. Instagram has been introducing several useful features lately, but users who want access to additional tools and perks can subscribe to the platform’s Instagram Plus offering. Priced at $3.99 per month, Instagram Plus includes exclusive features such as 48-hour Stories, more detailed post analytics, and the ability to tailor posts or Stories to specific audiences. The subscription includes 11 new features in total.
  • Recent Achievements

    • Week One Done
      Huge Trailer earned a badge
      Week One Done
    • Week One Done
      Classifyskilleducation earned a badge
      Week One Done
    • One Month Later
      eurospharma62 earned a badge
      One Month Later
    • Week One Done
      With What earned a badge
      Week One Done
    • Week One Done
      Harris Gilbert earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      587
    2. 2
      +Edouard
      169
    3. 3
      PsYcHoKiLLa
      72
    4. 4
      Michael Scrip
      66
    5. 5
      ATLien_0
      64
  • Tell a friend

    Love Neowin? Tell a friend!