summaryrefslogtreecommitdiff
path: root/plugins/AdvaImg/src/main.cpp
blob: 362d12be71805ac4a5ca3020b8b41a14a61b75b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
Plugin of Miranda IM for reading/writing PNG images.
Copyright (c) 2004-06 George Hazan (ghazan@postman.ru)

Portions of this code are gotten from the libpng codebase.
Copyright 2000, Willem van Schaik.  For conditions of distribution and
use, see the copyright/license/disclaimer notice in png.h

Miranda IM: the free icq client for MS Windows
Copyright (C) 2000-2002 Richard Hughes, Roland Rabien & Tristan Van de Vreede

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "stdafx.h"

static const PLUGININFOEX pluginInfoEx = {
	sizeof(PLUGININFOEX),
	__PLUGIN_NAME,
	PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
	__DESCRIPTION,
	__AUTHOR,
	__COPYRIGHT,
	__AUTHORWEB,
	UNICODE_AWARE | STATIC_PLUGIN,
	// {7C070F7C-459E-46b7-8E6D-BC6EFAA22F78}
	{0x7c070f7c, 0x459e, 0x46b7, {0x8e, 0x6d, 0xbc, 0x6e, 0xfa, 0xa2, 0x2f, 0x78}}
};

// Resize /////////////////////////////////////////////////////////////////////////////////////////

// Returns a copy of the bitmap with the size especified
// !! the caller is responsible for destroying the original bitmap when it is no longer needed !!
// wParam = ResizeBitmap *
// lParam = NULL
// return NULL on error, ResizeBitmap->hBmp if don't need to resize or a new HBITMAP if resized

static INT_PTR serviceBmpFilterResizeBitmap(WPARAM wParam, LPARAM)
{
	ResizeBitmap *info = (ResizeBitmap *)wParam;
	if (info == nullptr || info->size != sizeof(ResizeBitmap)
		|| info->hBmp == nullptr
		|| 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
	BITMAP bminfo;
	GetObject(info->hBmp, sizeof(bminfo), &bminfo);

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

	int xOrig = 0;
	int yOrig = 0;
	int widthOrig = bminfo.bmWidth;
	int 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;
	}

	FIBITMAP *dib = FreeImage_CreateDIBFromHBITMAP(info->hBmp);
	if (dib == nullptr)
		return 0;

	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 == nullptr) {
		FreeImage_Unload(dib);
		return 0;
	}

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

///////////////////////////////////////////////////////////////////////////////
// Load - initializes the plugin instance

static INT_PTR serviceLoad(WPARAM wParam, LPARAM lParam)
{
	char *lpszFilename = (char *)wParam;
	if (lpszFilename == nullptr)
		return 0;
	
	FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
	if (lParam & IMGL_WCHAR)
		fif = FreeImage_GetFileTypeU((wchar_t *)lpszFilename, 0);
	else
		fif = FreeImage_GetFileType(lpszFilename, 0);

	if (fif == FIF_UNKNOWN) {
		if (lParam & IMGL_WCHAR)
			fif = FreeImage_GetFIFFromFilenameU((wchar_t *)lpszFilename);
		else
			fif = FreeImage_GetFIFFromFilename(lpszFilename);
	}
	// check that the plugin has reading capabilities ...

	if ((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
		// ok, let's load the file
		FIBITMAP *dib;
		if (lParam & IMGL_WCHAR)
			dib = FreeImage_LoadU(fif, (wchar_t *)lpszFilename, 0);
		else
			dib = FreeImage_Load(fif, lpszFilename, 0);

		if (dib == nullptr || (lParam & IMGL_RETURNDIB))
			return (INT_PTR)dib;

		HBITMAP hbm = FreeImage_CreateHBITMAPFromDIB(dib);
		FreeImage_Unload(dib);
		FreeImage_CorrectBitmap32Alpha(hbm, FALSE);
		return (INT_PTR)hbm;
	}
	return NULL;
}

static INT_PTR serviceLoadFromMem(WPARAM wParam, LPARAM lParam)
{
	IMGSRVC_MEMIO *mio = (IMGSRVC_MEMIO *)wParam;
	if (mio->iLen == 0 || mio->pBuf == nullptr)
		return 0;

	FIMEMORY *hmem = FreeImage_OpenMemory((BYTE *)mio->pBuf, mio->iLen);
	FREE_IMAGE_FORMAT fif = (mio->fif != FIF_UNKNOWN) ? mio->fif : mio->fif = FreeImage_GetFileTypeFromMemory(hmem, 0);
	FIBITMAP *dib = FreeImage_LoadFromMemory(fif, hmem, mio->flags);
	FreeImage_CloseMemory(hmem);

	if (dib == nullptr || (lParam & IMGL_RETURNDIB))
		return (INT_PTR)dib;

	HBITMAP hbm = FreeImage_CreateHBITMAPFromDIB(dib);

	FreeImage_Unload(dib);
	return (INT_PTR)hbm;
}

static INT_PTR serviceSave(WPARAM wParam, LPARAM lParam)
{
	IMGSRVC_INFO *isi = (IMGSRVC_INFO*)wParam;
	if (isi == nullptr)
		return 0;
	if (isi->cbSize != sizeof(IMGSRVC_INFO))
		return 0;
	if (isi->szName == nullptr)
		return 0;

	FREE_IMAGE_FORMAT fif;
	BOOL fUnload = FALSE;
	FIBITMAP *dib = nullptr;

	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) // default, save as bmp
		fif = FIF_BMP;

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

	if (dib == nullptr)
		return 0;

	int flags = HIWORD(lParam);

	int ret = 0;
	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;
}

extern "C" int __declspec(dllexport) Load(void)
{
	CreateServiceFunction(MS_IMG_LOAD, serviceLoad);
	CreateServiceFunction(MS_IMG_LOADFROMMEM, serviceLoadFromMem);
	CreateServiceFunction(MS_IMG_SAVE, serviceSave);
	CreateServiceFunction(MS_IMG_RESIZE, serviceBmpFilterResizeBitmap);
	return 0;
}

///////////////////////////////////////////////////////////////////////////////
// Unload - destroys the plugin instance

extern "C" int __declspec(dllexport) Unload(void)
{
	return 0;
}

extern "C" __declspec(dllexport) const PLUGININFOEX * MirandaPluginInfoEx(DWORD)
{
	return &pluginInfoEx;
}