From ee89703abd075cacb396d86bfdf7e1dd4ce9d4d1 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 26 Jan 2018 18:59:01 +0300 Subject: code cleaning --- plugins/AdvaImg/src/main.cpp | 301 ++++++++++++++++++------------------------- 1 file changed, 126 insertions(+), 175 deletions(-) (limited to 'plugins/AdvaImg/src') diff --git a/plugins/AdvaImg/src/main.cpp b/plugins/AdvaImg/src/main.cpp index a3b74d15a5..362d12be71 100644 --- a/plugins/AdvaImg/src/main.cpp +++ b/plugins/AdvaImg/src/main.cpp @@ -41,20 +41,15 @@ static const PLUGININFOEX pluginInfoEx = { // 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) +static INT_PTR serviceBmpFilterResizeBitmap(WPARAM wParam, LPARAM) { - BITMAP bminfo; - int width, height; - int xOrig, yOrig, widthOrig, heightOrig; - ResizeBitmap *info = (ResizeBitmap *) wParam; - + ResizeBitmap *info = (ResizeBitmap *)wParam; if (info == nullptr || info->size != sizeof(ResizeBitmap) || info->hBmp == nullptr || info->max_width < 0 || info->max_height < 0 @@ -65,109 +60,90 @@ static INT_PTR serviceBmpFilterResizeBitmap(WPARAM wParam,LPARAM) // Well, lets do it // Calc final size + BITMAP bminfo; 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; + int width = info->max_width == 0 ? bminfo.bmWidth : info->max_width; + int height = info->max_height == 0 ? bminfo.bmHeight : info->max_height; - xOrig = 0; - yOrig = 0; - widthOrig = bminfo.bmWidth; - heightOrig = bminfo.bmHeight; + 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; + 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; } - case RESIZEBITMAP_MAKE_SQUARE: - { + else { if (info->fit & RESIZEBITMAP_FLAG_DONT_GROW) - { width = min(width, bminfo.bmWidth); - height = min(height, bminfo.bmHeight); - } + 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 - 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; } - 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; + 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)) - { + 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 == nullptr) - 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 == nullptr) - { - FreeImage_Unload(dib); - return NULL; - } + FIBITMAP *dib = FreeImage_CreateDIBFromHBITMAP(info->hBmp); + if (dib == nullptr) + return 0; - FIBITMAP *dib_new = FreeImage_Rescale(dib_tmp, width, height, FILTER_CATMULLROM); + FIBITMAP *dib_tmp; + if (xOrig > 0 || yOrig > 0) + dib_tmp = FreeImage_Copy(dib, xOrig, yOrig, xOrig + widthOrig, yOrig + heightOrig); + else + dib_tmp = dib; - HBITMAP bitmap_new = FreeImage_CreateHBITMAPFromDIB(dib_new); + if (dib_tmp == nullptr) { + FreeImage_Unload(dib); + return 0; + } - if (dib_new != dib_tmp) - FreeImage_Unload(dib_new); - if (dib_tmp != dib) - FreeImage_Unload(dib_tmp); - FreeImage_Unload(dib); + FIBITMAP *dib_new = FreeImage_Rescale(dib_tmp, width, height, FILTER_CATMULLROM); + HBITMAP bitmap_new = FreeImage_CreateHBITMAPFromDIB(dib_new); - return (INT_PTR)bitmap_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; } /////////////////////////////////////////////////////////////////////////////// @@ -176,16 +152,17 @@ static INT_PTR serviceBmpFilterResizeBitmap(WPARAM wParam,LPARAM) static INT_PTR serviceLoad(WPARAM wParam, LPARAM lParam) { char *lpszFilename = (char *)wParam; - if(lpszFilename==nullptr) return 0; + if (lpszFilename == nullptr) + return 0; + FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; - - if(lParam & IMGL_WCHAR) + 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) + if (fif == FIF_UNKNOWN) { + if (lParam & IMGL_WCHAR) fif = FreeImage_GetFIFFromFilenameU((wchar_t *)lpszFilename); else fif = FreeImage_GetFIFFromFilename(lpszFilename); @@ -195,19 +172,18 @@ static INT_PTR serviceLoad(WPARAM wParam, LPARAM lParam) 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)) + 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 (INT_PTR)hbm; } return NULL; } @@ -215,7 +191,7 @@ static INT_PTR serviceLoad(WPARAM wParam, LPARAM lParam) static INT_PTR serviceLoadFromMem(WPARAM wParam, LPARAM lParam) { IMGSRVC_MEMIO *mio = (IMGSRVC_MEMIO *)wParam; - if(mio->iLen == 0 || mio->pBuf == nullptr) + if (mio->iLen == 0 || mio->pBuf == nullptr) return 0; FIMEMORY *hmem = FreeImage_OpenMemory((BYTE *)mio->pBuf, mio->iLen); @@ -223,7 +199,7 @@ static INT_PTR serviceLoadFromMem(WPARAM wParam, LPARAM lParam) FIBITMAP *dib = FreeImage_LoadFromMemory(fif, hmem, mio->flags); FreeImage_CloseMemory(hmem); - if(dib == nullptr || (lParam & IMGL_RETURNDIB)) + if (dib == nullptr || (lParam & IMGL_RETURNDIB)) return (INT_PTR)dib; HBITMAP hbm = FreeImage_CreateHBITMAPFromDIB(dib); @@ -232,107 +208,82 @@ static INT_PTR serviceLoadFromMem(WPARAM wParam, LPARAM lParam) return (INT_PTR)hbm; } -static INT_PTR serviceUnload(WPARAM wParam, LPARAM) -{ - FIBITMAP *dib = (FIBITMAP *)wParam; - - if(dib) - FreeImage_Unload(dib); - - return 0; -} - static INT_PTR serviceSave(WPARAM wParam, LPARAM lParam) { - IMGSRVC_INFO *isi = (IMGSRVC_INFO *)wParam; + 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) { - 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 != 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; - - 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; - } + 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); } - return 0; + 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; } -static int IMGSERVICE_Load() +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_UNLOAD, serviceUnload); CreateServiceFunction(MS_IMG_RESIZE, serviceBmpFilterResizeBitmap); return 0; } -extern "C" int __declspec(dllexport) Load(void) -{ - return IMGSERVICE_Load(); -} - /////////////////////////////////////////////////////////////////////////////// // Unload - destroys the plugin instance -static int IMGSERVICE_Unload( void ) -{ - return 0; -} - extern "C" int __declspec(dllexport) Unload(void) { - return IMGSERVICE_Unload(); + return 0; } extern "C" __declspec(dllexport) const PLUGININFOEX * MirandaPluginInfoEx(DWORD) -- cgit v1.2.3