2
ITmages upload client for Windows
3
Copyright (C) 2011 Dmitriy Simbiriatin <slpiv@mail.ru>
5
This program is free software; you can redistribute it and/or
6
modify it under the terms of the GNU General Public License
7
as published by the Free Software Foundation; either version 2
8
of the License, or (at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23
#include <FreeImage.h>
24
#include "imagefunc.h"
26
#include "errorshandler.h"
28
void SetExtToLower(wchar_t *ext)
32
for (i = 0; i < wcslen(ext); ++i) {
33
ext[i] = towlower(ext[i]);
37
size_t GetFilterType(size_t filter_index)
41
switch (filter_index) {
43
filter_type = FILTER_LANCZOS3; // Lanczos filter
46
filter_type = FILTER_BOX; // Box filter
49
filter_type = FILTER_BILINEAR; // Bilinear filter
52
filter_type = FILTER_BSPLINE; // B-Spline filter
62
ItmagesImageInfo *imageInfo)
64
FIBITMAP *image = NULL;
66
size_t image_type = FreeImage_GetFileTypeU(image_path, 0);
68
image = FreeImage_LoadU(image_type, image_path, 0);
73
imageInfo->mime = (char*) FreeImage_GetFIFMimeType(image_type);
74
imageInfo->fullname = GetImageFullname(basename, ext);
75
imageInfo->width = FreeImage_GetWidth(image);
76
imageInfo->height = FreeImage_GetHeight(image);
77
imageInfo->size = GetImageSize(image_path);
80
FreeImage_Unload(image);
85
void FreeImageInfo(ItmagesImageInfo *imageInfo)
87
if (imageInfo->fullname != NULL) {
88
free(imageInfo->fullname);
89
imageInfo->fullname = NULL;
91
if (imageInfo->mime != NULL) {
92
imageInfo->mime = NULL;
95
imageInfo->height = 0;
104
FIBITMAP *image = NULL;
105
FIBITMAP *resized_image = NULL;
107
float width, height, ratio;
110
size_t image_type = FreeImage_GetFileTypeU(image_path, 0);
112
image = FreeImage_LoadU(image_type, image_path, 0);
116
width = FreeImage_GetWidth(image);
117
height = FreeImage_GetHeight(image);
119
ratio = width / new_width;
120
new_height = height / ratio;
125
if (new_height < 1) {
129
if (new_height > height) {
130
if (image) FreeImage_Unload(image);
134
size_t filter_type = GetFilterType(filter_index);
135
resized_image = FreeImage_Rescale(image, new_width, new_height, filter_type);
136
if (resized_image == NULL) {
137
if (image) FreeImage_Unload(image);
140
FreeImage_SaveU(image_type, resized_image, image_path, 0);
143
FreeImage_Unload(resized_image);
147
FreeImage_Unload(image);
153
int RotateImageTo(wchar_t *image_path, int angle)
155
FIBITMAP *image = NULL;
156
FIBITMAP *rotated_image = NULL;
158
size_t image_type = FreeImage_GetFileTypeU(image_path, 0);
160
image = FreeImage_LoadU(image_type, image_path, 0);
165
rotated_image = FreeImage_Rotate(image, angle, NULL);
166
if (rotated_image == NULL) {
167
if (image) FreeImage_Unload(image);
170
FreeImage_SaveU(image_type, rotated_image, image_path, 0);
173
FreeImage_Unload(rotated_image);
177
FreeImage_Unload(image);
183
HBITMAP HBITMAPFromFile(
189
FIBITMAP *image = NULL;
190
FIBITMAP *resized_image = NULL;
192
size_t width, height, new_width, new_height;
194
size_t image_type = FreeImage_GetFileTypeU(image_path, 0);
196
image = FreeImage_LoadU(image_type, image_path, 0);
201
width = FreeImage_GetWidth(image);
202
height = FreeImage_GetHeight(image);
204
if (width <= max_width) {
207
new_width = max_width;
209
if (height <= max_height) {
212
new_height = max_height;
215
resized_image = FreeImage_Rescale(image, new_width, new_height, FILTER_BOX);
216
if (resized_image == NULL) {
217
if (image) FreeImage_Unload(image);
224
image_dc = GetDC(image_wnd);
225
bitmap = CreateDIBitmap(
227
FreeImage_GetInfoHeader(resized_image),
229
FreeImage_GetBits(resized_image),
230
FreeImage_GetInfo(resized_image),
233
ReleaseDC(image_wnd, image_dc);
236
FreeImage_Unload(resized_image);
240
FreeImage_Unload(image);
246
int CheckImageExtension(wchar_t *ext)
249
wmemcmp(ext, L".jpg", 5) != 0 &&
250
wmemcmp(ext, L".jpeg", 6) != 0 &&
251
wmemcmp(ext, L".png", 5) != 0 &&
252
wmemcmp(ext, L".gif", 5) != 0
259
int GetImageSize(wchar_t *image_path)
263
_wstat(image_path, &image);
264
if (image.st_size > MAX_IMAGE_SIZE) {
267
return image.st_size;
271
char *GetImageFullname(wchar_t *basename, wchar_t *ext)
273
size_t base_len = wcslen(basename);
274
size_t ext_len = wcslen(ext);
276
wchar_t *fullname = (wchar_t*) malloc((base_len + ext_len + 1) * sizeof (wchar_t));
277
if (fullname == NULL) {
278
ErrorsHandler(ERROR_MEMALLOC);
282
wmemcpy(fullname, basename, base_len + 1);
283
wmemcpy(fullname + base_len, ext, ext_len + 1);
285
char *fullname_mb = WideToUtf8(fullname);
b'\\ No newline at end of file'