Esempio n. 1
0
/*
 * Changes the handle to a grayscale image
 */
HBITMAP MakeGrayscale(HBITMAP hBitmap)
{
	if (hBitmap) {
		FIBITMAP *dib = FreeImage_CreateDIBFromHBITMAP(hBitmap);
		if (dib) {
			FIBITMAP *dib_new = FreeImage_ConvertToGreyscale(dib);
			FreeImage_Unload(dib);
			if (dib_new) {
				DeleteObject(hBitmap);
				HBITMAP hbm_new = FreeImage_CreateHBITMAPFromDIB(dib_new);
				FreeImage_Unload(dib_new);
				return hbm_new;
			}
		}
	}
	return hBitmap;
}
Esempio n. 2
0
static INT_PTR serviceSave(WPARAM wParam, LPARAM lParam)
{
	IMGSRVC_INFO *isi = (IMGSRVC_INFO *)wParam;
	FREE_IMAGE_FORMAT fif;
	BOOL fUnload = FALSE;
	FIBITMAP *dib = NULL;

	if(isi) {
		if(isi->cbSize != sizeof(IMGSRVC_INFO))
			return 0;

		if(isi->szName || isi->wszName) {
			if(isi->fif == FIF_UNKNOWN) {
				if(lParam & IMGL_WCHAR)
					fif = FreeImage_GetFIFFromFilenameU(isi->wszName);
				else
					fif = FreeImage_GetFIFFromFilename(isi->szName);
			}
			else
				fif = isi->fif;

			if(fif == FIF_UNKNOWN)
				fif = FIF_BMP;                  // default, save as bmp

			if(isi->hbm != 0 && (isi->dwMask & IMGI_HBITMAP) && !(isi->dwMask & IMGI_FBITMAP)) {
				// create temporary dib, because we got a HBTIMAP passed
				fUnload = TRUE;
				FI_CorrectBitmap32Alpha(isi->hbm, FALSE);
				dib = FreeImage_CreateDIBFromHBITMAP(isi->hbm);
			}
			else if(isi->dib != NULL && (isi->dwMask & IMGI_FBITMAP) && !(isi->dwMask & IMGI_HBITMAP))
				dib = isi->dib;

			int ret = 0;
			if(dib) {
				int flags = HIWORD(lParam);

				if(fif == FIF_PNG || fif == FIF_BMP || fif == FIF_JNG)
				{
					if(lParam & IMGL_WCHAR)
						ret = FreeImage_SaveU(fif, dib, isi->wszName, flags);
					else
						ret = FreeImage_Save(fif, dib, isi->szName, flags);
				}
				else
				{
					FIBITMAP *dib_new = FreeImage_ConvertTo24Bits(dib);
					if(lParam & IMGL_WCHAR)
						ret = FreeImage_SaveU(fif, dib_new, isi->wszName, flags);
					else
						ret = FreeImage_Save(fif, dib_new, isi->szName, flags);
					FreeImage_Unload(dib_new);
				}

				if(fUnload)
					FreeImage_Unload(dib);
				return ret;
			}
			return 0;
		}

	}
	return 0;
}
Esempio n. 3
0
static INT_PTR serviceBmpFilterResizeBitmap(WPARAM wParam,LPARAM lParam)
{
	BITMAP bminfo;
	int width, height;
	int xOrig, yOrig, widthOrig, heightOrig;
	ResizeBitmap *info = (ResizeBitmap *) wParam;

	if (info == NULL || info->size != sizeof(ResizeBitmap)
		|| info->hBmp == NULL
		|| info->max_width < 0 || info->max_height < 0
		|| (info->fit & ~RESIZEBITMAP_FLAG_DONT_GROW) < RESIZEBITMAP_STRETCH
		|| (info->fit & ~RESIZEBITMAP_FLAG_DONT_GROW) > RESIZEBITMAP_MAKE_SQUARE)
		return 0;

	// Well, lets do it

	// Calc final size
	GetObject(info->hBmp, sizeof(bminfo), &bminfo);

	width = info->max_width == 0 ? bminfo.bmWidth : info->max_width;
	height = info->max_height == 0 ? bminfo.bmHeight : info->max_height;

	xOrig = 0;
	yOrig = 0;
	widthOrig = bminfo.bmWidth;
	heightOrig = bminfo.bmHeight;

	if (widthOrig == 0 || heightOrig == 0)
		return 0;

	switch(info->fit & ~RESIZEBITMAP_FLAG_DONT_GROW)
	{
		case RESIZEBITMAP_STRETCH:
		{
			// Do nothing
			break;
		}
		case RESIZEBITMAP_KEEP_PROPORTIONS:
		{
			if (height * widthOrig / heightOrig <= width)
			{
				if (info->fit & RESIZEBITMAP_FLAG_DONT_GROW)
					height = min(height, bminfo.bmHeight);
				width = height * widthOrig / heightOrig;
			}
			else
			{
				if (info->fit & RESIZEBITMAP_FLAG_DONT_GROW)
					width = min(width, bminfo.bmWidth);
				height = width * heightOrig / widthOrig;
			}

			break;
		}
		case RESIZEBITMAP_MAKE_SQUARE:
		{
			if (info->fit & RESIZEBITMAP_FLAG_DONT_GROW)
			{
				width = min(width, bminfo.bmWidth);
				height = min(height, bminfo.bmHeight);
			}

			width = height = min(width, height);
			// Do not break. Use crop calcs to make size
		}
		case RESIZEBITMAP_CROP:
		{
			if (heightOrig * width / height >= widthOrig)
			{
				heightOrig = widthOrig * height / width;
				yOrig = (bminfo.bmHeight - heightOrig) / 2;
			}
			else
			{
				widthOrig = heightOrig * width / height;
				xOrig = (bminfo.bmWidth - widthOrig) / 2;
			}

			break;
		}
	}

	if ((width == bminfo.bmWidth && height == bminfo.bmHeight)
		|| ((info->fit & RESIZEBITMAP_FLAG_DONT_GROW)
			&& !(info->fit & RESIZEBITMAP_MAKE_SQUARE)
			&& width > bminfo.bmWidth && height > bminfo.bmHeight))
	{
		// Do nothing
		return (INT_PTR)info->hBmp;
	}
	else
	{
		FIBITMAP *dib = FreeImage_CreateDIBFromHBITMAP(info->hBmp);
		if (dib == NULL)
			return NULL;

		FIBITMAP *dib_tmp;
		if (xOrig > 0 || yOrig > 0)
			dib_tmp = FreeImage_Copy(dib, xOrig, yOrig, xOrig + widthOrig, yOrig + heightOrig);
		else
			dib_tmp = dib;

		if (dib_tmp == NULL)
		{
			FreeImage_Unload(dib);
			return NULL;
		}

		FIBITMAP *dib_new = FreeImage_Rescale(dib_tmp, width, height, FILTER_CATMULLROM);

		HBITMAP bitmap_new = FreeImage_CreateHBITMAPFromDIB(dib_new);

		if (dib_new != dib_tmp)
			FreeImage_Unload(dib_new);
		if (dib_tmp != dib)
			FreeImage_Unload(dib_tmp);
        FreeImage_Unload(dib);

		return (INT_PTR)bitmap_new;
	}
}