• 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

    • Kind of glad I upgraded to S26 Ultra 1TB this year, with trade-in and discounts it cost me €1199, that's only €119 more than the S23 Ultra (256 GB) cost me. Last time I bought it via my phone provider outright, now via Samsung.com I reckon the S27 series will be more expensive too.
    • Save 66% on a MagTag Ultra Slim Tracker Card for Apple or Android by Steven Parker Never Lose Anything Again with MagTag Today's highlighted deal comes via our Gear + Gadgets section of the Neowin Deals store where you can save 66% on this MagTag Ultra Slim Tracker Card - Works with Apple Find My App. Keep track of your world with MagTag, a sleek, ultra-slim, reliable tracker that’s built to help you safeguard your most important items. In the size of a credit card, just 1.5mm thick, you can slip MagTag easily into your wallet, backpack, passport pouch luggage…etc. Integrated seamlessly with Apple’s FindMy app, MagTag offers precise real-time global tracking, instant left-behind alerts, loud location beeping, and a long-lasting rechargeable battery. Whether you’re heading to work, on vacation, or simply running errands, MagTag ensures you never lose what matters most. No item left behind Precision Global Tracking: Works seamlessly with the Apple FindMy app, providing real-time tracking anywhere in the world, powered by the vast Apple network. Ultra Slim Design: At just 1.5mm thick and the size of a credit card, MagTag slips easily into your wallet, passport pouch, backpack, or luggage. Instant Alerts: Receive notifications the moment you leave behind your valuables, and locate them easily with a loud beeping sound. Versatile Attachment Options: With a built-in keyring hole, attach MagTag to keys, ID lanyards, kids’ bags, or name tags for easy access and protection. Long Battery Life & Wireless Charging: Lasts up to 5 months on a single charge and can be easily recharged with any Qi wireless charger. Durable & Waterproof: IP68 waterproof and dustproof built to withstand your adventures, perfect for vacations and everyday use, no matter where life takes you. Specs Color: Black Materials: ABS Dimensions: 0.05" x 3.35" x 2.13" (1.5mm x 85mm x 54mm) Ultra-slim Apple FindMy App Built-in keyring hole Battery life: up to 5 months Charging: Qi wireless IP68 rating (waterproof, dustproof) Manufacturer's 90-day warranty Good to know Ships to US Expected Delivery: Expected Delivery: Jun 23 - Jul 2 All sales final. This item is excluded from coupons. Here's the deal: This MagTag Ultra Slim Tracker Card (for Apple or Android) normally costs $59.99, but you can pick it up for just $19.99 for a limited time - that represents a saving of $19. For a full description, specs, and shipping info, click the link below. MagTag Ultra Slim Tracker Card now just $19.99 (was $59.99) Get the two-pack and save 70% Ships only to Contiguous US Support queries If you have queries or need support for any of the Neowin Deals, please use the contact form here. Neowin Deals are managed and sold by StackCommerce who represent Neowin on an affiliate basis. Why we post these deals We post these because we earn commission on each sale so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. So for those that keep moaning and complaining, be thankful we're still online for you to even do that. Other ways to support Neowin Whitelist Neowin by not blocking our ads Create a free member account to see fewer ads Make a donation to support our day to day running costs Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: Neowin benefits from revenue of each sale made through our branded deals site powered by StackCommerce.
    • I cannot believe this is a news post from Neowin. This should be embarassing, coming from a "senior editor". Is it your first day using Windows?! Maybe it's time to find a new Windows news site.
  • 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
      586
    2. 2
      +Edouard
      169
    3. 3
      PsYcHoKiLLa
      73
    4. 4
      Michael Scrip
      66
    5. 5
      ATLien_0
      64
  • Tell a friend

    Love Neowin? Tell a friend!