|
// img_resize.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
Image *ImageResize(Image *image, int intDesiredWidth = 0, int intDesiredHeight = 0);
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid);
int _tmain(int argc, _TCHAR* argv[])
{
int nRet = 0;
Image* image = NULL;
Image *newImage = NULL;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken = NULL;;
Status st = GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
if (st != 0)
{
fprintf(stderr, "ERROR: can't initialize GDI+! Status = %d\n", st);
nRet = 1;
}
else if (argc < 1)
{
fprintf(stderr, "USAGE: %s [INPUTFILE.jpg] [OUTPUTFILE.jpg] [width=800px] [height=0]\n", argv[0]);
nRet = 0;
}
else
{
const _TCHAR *strImageFile = argv[1];
_TCHAR strNewImageFile[_MAX_PATH];
int intDesiredWidth = 800;
int intDesiredHeight = 0;
if (argc > 2)
{
_tcscpy(strNewImageFile, argv[2]);
}
else
{
// Compute resized file name (".tmp.jpg")
_TCHAR strDrive[_MAX_DRIVE];
_TCHAR strDir[_MAX_DIR];
_TCHAR strFileName[_MAX_FNAME];
_TCHAR strFileExt[_MAX_EXT];
_tsplitpath(strImageFile, strDrive, strDir, strFileName, strFileExt);
_tcsncat(strFileName, _T(".tmp"), _MAX_FNAME);
_tmakepath(strNewImageFile, strDrive, strDir, strFileName, strFileExt);
}
if (argc > 3)
{
intDesiredWidth = _tstoi(argv[3]);
}
if (argc > 4)
{
intDesiredHeight = _tstoi(argv[4]);
}
image = new Image(strImageFile);
newImage = ImageResize(image, intDesiredWidth, intDesiredHeight);
if (newImage != NULL)
{
CLSID jpegClsid;
int intEncoderFound = GetEncoderClsid(L"image/jpeg", &jpegClsid);
if (intEncoderFound == -1)
{
fprintf(stderr, "ERROR: Can't find JPG encoder!\n");
}
else
{
delete image;
image = NULL;
Status nSaved = newImage->Save(strNewImageFile, &jpegClsid, NULL);
if (nSaved != 0)
{
fprintf(stderr, "ERROR: Can't save JPEG file \"%s\"!\n",strNewImageFile);
}
nRet = 0;
}
} // TODO else
}
// Cleanup
if (image)
{
delete image;
image = NULL;
}
if (newImage)
{
delete newImage;
newImage = NULL;
}
if (gdiplusToken)
{
GdiplusShutdown(gdiplusToken);
gdiplusToken = NULL;
}
return(nRet);
}
Image *ImageResize(Image *image, int intDesiredWidth, int intDesiredHeight)
{
UINT imageWidth = image->GetWidth();
UINT imageHeight = image->GetHeight();
UINT intNewWidth = intDesiredWidth;
UINT intNewHeight = intDesiredHeight;
if (intDesiredWidth != 0 && intDesiredHeight == 0)
{
intNewHeight = (UINT)((intDesiredWidth / (double) imageWidth) * imageHeight);
}
else if (intDesiredHeight != 0 && intDesiredWidth == 0)
{
intNewWidth = (UINT)((intDesiredHeight / (double) imageHeight) * imageWidth);
}
fprintf(stdout, "Resize: width %d => %d\n", imageWidth, intNewWidth);
fprintf(stdout, " height %d => %d\n", imageHeight, intNewHeight);
Bitmap *bitmap = new Bitmap((int)intNewWidth, (int)intNewHeight, image->GetPixelFormat());
Graphics *graphics = Graphics::FromImage(bitmap);
graphics->SetSmoothingMode(SmoothingModeHighQuality);
graphics->SetInterpolationMode(InterpolationModeBicubic);
graphics->DrawImage(image, 0, 0, bitmap->GetWidth(), bitmap->GetHeight());
return(bitmap);
}
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
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Fell into computer software at the age of 11, founder of 3 startups, and now manager of an independent software vendor (ISV) labelled proSDK (www.prosdk.com)... And still a freeware writer and technical article author!