Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Is there any way to save images or change image formats using predefined header files (without using third party applications) using c++ in Linux environment.

Thanks in advance.

What I have tried:

I found this code for the same req in msdn for windows:

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

#include <gdiplusimagecodec.h>
#pragma comment (lib, "gdiplus.lib")


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; // Failure
}


int _tmain(int argc, _TCHAR* argv[])
{
// Initialize GDI+.
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

CLSID encoderClsid;
EncoderParameters encoderParameters;
ULONG quality;
Status stat;

// Get an image from the disk.
Image* image = new Image(L"C:\\Users\\Elisa\\Documents\\Visual Studio 2010\\Projects\\Image_Conversion_2\\Penguins.jpg");

// Get the CLSID of the JPEG encoder.
GetEncoderClsid(L"image/jpeg", &encoderClsid);

// Before we call Image::Save, we must initialize an
// EncoderParameters object. The EncoderParameters object
// has an array of EncoderParameter objects. In this
// case, there is only one EncoderParameter object in the array.
// The one EncoderParameter object has an array of values.
// In this case, there is only one value (of type ULONG)
// in the array. We will let this value vary from 0 to 100.

encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;

// Save the image as a JPEG with quality level 0.
quality = 0;
encoderParameters.Parameter[0].Value = &quality;
stat = image->Save(L"Shapes001.jpg", &encoderClsid, &encoderParameters);

if(stat == Ok)
wprintf(L"%s saved successfully.\n", L"Shapes001.jpg");
else
wprintf(L"%d Attempt to save %s failed.\n", stat, L"Shapes001.jpg");

// Save the image as a JPEG with quality level 50.
quality = 50;
encoderParameters.Parameter[0].Value = &quality;
stat = image->Save(L"Shapes050.jpg", &encoderClsid, &encoderParameters);

if(stat == Ok)
wprintf(L"%s saved successfully.\n", L"Shapes050.jpg");
else
wprintf(L"%d Attempt to save %s failed.\n", stat, L"Shapes050.jpg");

// Save the image as a JPEG with quality level 100.
quality = 100;
encoderParameters.Parameter[0].Value = &quality;
stat = image->Save(L"Shapes100.jpg", &encoderClsid, &encoderParameters);

if(stat == Ok)
wprintf(L"%s saved successfully.\n", L"Shapes100.jpg");
else
wprintf(L"%d Attempt to save %s failed.\n", stat, L"Shapes100.jpg");

delete image;
GdiplusShutdown(gdiplusToken);
return 0;
}
Posted
Updated 7-Apr-16 2:32am
v3
Comments
KarstenK 7-Apr-16 8:11am    
Changing the image format means that you recode the image. Maybe the OpenCV library may help you. It isnt as simple as copying some code from MSDN and asking for portation to Linux. :-(
Member 12333356 7-Apr-16 9:08am    
I can understand how complex it is Karsten. :P I'm new to programming please don't mind. :P

I was wondering if the same process can be done in linux using standard libraries available.

I've just found a library tiffio.h. Can it be used to read tiff image and can I use someother library to convert it to a jpeg image?
Richard MacCutchan 7-Apr-16 8:20am    
This has nothing to do with header files, and GDIPlus is Windows only.
Member 12333356 7-Apr-16 9:01am    
I can get you Richard, I was wondering if there are any predefined libraries which could be used in Linux as the same way gdiplus is being used for Windows.

Thanks for your help in advance. :)

1 solution

The usual tool for this task with Linux is ImageMagick: Convert, Edit, Or Compose Bitmap Images[^]. It provides command line utilities and a C++ API[^].

ImageMagick is installed by default with most Linux distributions and the C++ API can be usually installed using the package manager.
 
Share this answer
 
Comments
Member 12333356 7-Apr-16 9:03am    
Thanks Jochen, I'll certainly have a look into it. Have you ever tried ImageMagick and if so is the processing time for conversion is much better than other libraries available?
Jochen Arndt 7-Apr-16 9:15am    
I have not used it by C/C++ programs but the command line utilities and never cared about the execution time.

But it is "the" library for images with Linux. So there should be no need to use other libraries besides when there is low system drive capacity and only a few image formats must be supported.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900