• 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

    • If you look around on Amazon, some of these are available for $9
    • I’m still using an Xbox One S, so time for an upgrade to play this but as much as I hate Sony, I think I’ll get the ps5 pro
    • I bought this game. Played it for an hour, and then got a refund from Steam. Not a fun game at all.
    • Nothing Ear buds with active noise cancellation are at their lowest price ever with 51% off by Fiza Ali Amazon is currently offering the Nothing Ear wireless earbuds at their lowest price ever with 51% off limited prime deal. The earbuds feature an 11mm dynamic drivers with a ceramic diaphragm, and support high-resolution audio codecs including AAC, SBC, LDAC, and LHDC 5.0. They support active noise cancellation of up to 45dB across a frequency range of up to 5000Hz, and include a smart ANC algorithm, adaptive noise cancellation, and a transparency mode that allows surrounding sounds to be heard when needed. Connectivity is provided via Bluetooth 5.3, with support for multiple profiles including HFP, A2DP, AVRCP, and others. The earbuds also support dual connection, allowing them to be paired with two devices at the same time. Additional features include IP54 water and dust resistance for the earbuds and IP55 for the charging case, in-ear detection, pinch controls, low-latency mode, Google Fast Pair, Microsoft Swift Pair, and a three-microphone system per earbud for clearer voice calls. The Nothing X app, available on Android and iOS, provides access to custom EQ settings, bass enhancement, personal sound profiles, ear tip fit testing, firmware updates, customisable controls, dual-device management, and a find-my-earbuds feature. In terms of battery performance, each earbud has a 46mAh battery and the charging case has a 500mAh capacity. With active noise cancellation (ANC) turned off, the earbuds should offer up to 8.5 hours of playback on a single charge and up to 40.5 hours in total with the charging case. With ANC enabled, playback should last up to 5.2 hours on the earbuds and up to 24 hours with the case. For calls, talk time should reach up to 5 hours on the earbuds and 23 hours with the case when ANC is off, while ANC on should provide up to 4 hours on the earbuds and 18 hours with the case. Finally, fast charging should deliver up to 10 hours of playback from 10 minutes of charging when ANC is disabled. Nothing Ear Wireless Earbuds Bluetooth: $73.15 (Amazon US) - 51% off Good to know This Amazon deal is U.S. specific, and not available in other regions unless specified. We only use first-party seller links (at the time of article publishing); ensure that you purchase from a first-party seller link only. Check out Today's Deals on Amazon | or our recent tech deals. Become a Prime member (for Students or SNAP) via Neowin Get Prime Access - Prime for half price (for qualifying Medicaid, EBT, SNAP) Subscribe to Prime Video, Audible Plus, Music Unlimited or Kindle Unlimited via Neowin As an Amazon Associate, we earn from qualifying purchases.
    • Microsoft officially launched its Copilot Cowork enterprise AI agent on June 16, 2026, switching to usage-based pricing on the same day it disclosed it is considering a Microsoft-hosted version of China's DeepSeek V4 as a lower-cost engine for the platform — a pairing that puts the company on a collision course with both its enterprise customers' security teams and a White House that has spent months trying to wall off Chinese AI from American infrastructure.................... https://www.techtimes.com/articles/318647/20260618/microsoft-eyes-deepseek-v4-copilot-cowork-what-azure-hosting-cannot-fix.htm  
  • Recent Achievements

    • First Post
      AndreaB earned a badge
      First Post
    • 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
  • Popular Contributors

    1. 1
      +primortal
      583
    2. 2
      +Edouard
      174
    3. 3
      PsYcHoKiLLa
      74
    4. 4
      Michael Scrip
      68
    5. 5
      neufuse
      64
  • Tell a friend

    Love Neowin? Tell a friend!