From 4b5b190fbe80874b4d563e3da2c999fc3eb2174f Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Sat, 3 Apr 2021 20:37:45 +0200 Subject: [PATCH] Move image utilities in a ImageHelpers file --- plugins/editor/CMakeLists.txt | 2 + plugins/editor/src/editor/Editor.cpp | 70 ++------------------- plugins/editor/src/editor/ImageHelpers.cpp | 73 ++++++++++++++++++++++ plugins/editor/src/editor/ImageHelpers.h | 24 +++++++ 4 files changed, 103 insertions(+), 66 deletions(-) create mode 100644 plugins/editor/src/editor/ImageHelpers.cpp create mode 100644 plugins/editor/src/editor/ImageHelpers.h diff --git a/plugins/editor/CMakeLists.txt b/plugins/editor/CMakeLists.txt index 0d939d09..bd0d71e1 100644 --- a/plugins/editor/CMakeLists.txt +++ b/plugins/editor/CMakeLists.txt @@ -45,6 +45,8 @@ add_library(sfizz_editor STATIC EXCLUDE_FROM_ALL src/editor/GUIPiano.cpp src/editor/ColorHelpers.h src/editor/ColorHelpers.cpp + src/editor/ImageHelpers.h + src/editor/ImageHelpers.cpp src/editor/NativeHelpers.h src/editor/NativeHelpers.cpp src/editor/layout/main.hpp diff --git a/plugins/editor/src/editor/Editor.cpp b/plugins/editor/src/editor/Editor.cpp index df3b00a6..e00e7795 100644 --- a/plugins/editor/src/editor/Editor.cpp +++ b/plugins/editor/src/editor/Editor.cpp @@ -6,11 +6,11 @@ #include "Editor.h" #include "EditorController.h" -#include "EditorLibs.h" #include "EditIds.h" #include "GUIComponents.h" #include "GUIHelpers.h" #include "GUIPiano.h" +#include "ImageHelpers.h" #include "NativeHelpers.h" #include "BitArray.h" #include "plugin/MessageUtils.h" @@ -40,11 +40,6 @@ using namespace VSTGUI; const int Editor::viewWidth { 800 }; const int Editor::viewHeight { 475 }; -struct image_deleter { - void operator()(unsigned char* x) const noexcept { stbi_image_free(x); } -}; -typedef std::unique_ptr image_u; - struct Editor::Impl : EditorController::Receiver, IControlListener { EditorController* ctrl_ = nullptr; CFrame* frame_ = nullptr; @@ -242,8 +237,6 @@ struct Editor::Impl : EditorController::Receiver, IControlListener { const char* keyName = keyNames[key % 12]; return std::string(keyName) + ' ' + std::to_string(octave); } - - static SharedPointer loadAnyFormatImage(const fs::path& filePath); }; Editor::Editor(EditorController& ctrl) @@ -1579,48 +1572,6 @@ void Editor::Impl::updateSWLastLabel(unsigned sw, const char* label) updateKeyswitchNameLabel(); } -SharedPointer Editor::Impl::loadAnyFormatImage(const fs::path& filePath) -{ -#if defined(_WIN32) - FILE* file { _wfopen(filePath.wstring().c_str(), L"rb") }; -#else - FILE* file { fopen(filePath.c_str(), "rb") }; -#endif - SharedPointer bitmap; - - if (!file) - return bitmap; - - int width, height, channels; - image_u image { - stbi_load_from_file(file, &width, &height, &channels, STBI_rgb_alpha) - }; - fclose(file); - auto imageData = image.get(); - - if (imageData) { - bitmap = makeOwned(width, height); - SharedPointer accessor = - owned(CBitmapPixelAccess::create(bitmap.get())); - - if (accessor) { - do { - CColor c( - imageData[0], - imageData[1], - imageData[2], - imageData[3] - ); - accessor->setColor(c); - imageData += 4; - } - while (++*accessor); - accessor = nullptr; - } - } - return bitmap; -} - void Editor::Impl::updateBackgroundImage(const char* filepath) { const fs::path sfzFilePath = fs::u8path(currentSfzFile_); @@ -1628,23 +1579,10 @@ void Editor::Impl::updateBackgroundImage(const char* filepath) const fs::path imagePath = sfzDirPath / fs::u8path(filepath); SharedPointer bitmap = loadAnyFormatImage(imagePath); - if (bitmap) { - CCoord containerW = imageContainer_->getWidth(); - CCoord containerH = imageContainer_->getHeight(); - CCoord bitmapW = bitmap->getWidth(); - CCoord bitmapH = bitmap->getHeight(); - - if (bitmapW > containerW || bitmapH > containerH) { - CCoord xScale = bitmapW / containerW; - CCoord yScale = bitmapH / containerH; - CCoord scale = (xScale > yScale) ? xScale : yScale; - - PlatformBitmapPtr ptr = bitmap->getPlatformBitmap(); - ptr->setScaleFactor(scale); - } - } else { + if (!bitmap) bitmap = owned(new CBitmap("background.png")); - } + + downscaleToWidthAndHeight(bitmap, imageContainer_->getViewSize().getSize()); imageContainer_->setBackground(bitmap); } diff --git a/plugins/editor/src/editor/ImageHelpers.cpp b/plugins/editor/src/editor/ImageHelpers.cpp new file mode 100644 index 00000000..a243a228 --- /dev/null +++ b/plugins/editor/src/editor/ImageHelpers.cpp @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: BSD-2-Clause + +// This code is part of the sfizz library and is licensed under a BSD 2-clause +// license. You should have receive a LICENSE.md file along with the code. +// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz + +#include "ImageHelpers.h" +#include "EditorLibs.h" +#include + +using namespace VSTGUI; + +struct stbi_image_delete { + void operator()(unsigned char* x) const noexcept { stbi_image_free(x); } +}; +using stbi_image_u = std::unique_ptr; + +SharedPointer loadAnyFormatImage(const fs::path& filePath) +{ + stbi_image_u image; + int width, height, channels; + +#if defined(_WIN32) + FILE* file { _wfopen(filePath.wstring().c_str(), L"rb") }; +#else + FILE* file { fopen(filePath.c_str(), "rb") }; +#endif + if (file) { + image.reset(stbi_load_from_file(file, &width, &height, &channels, STBI_rgb_alpha)); + fclose(file); + } + + if (!image) + return nullptr; + + SharedPointer bitmap = makeOwned(width, height); + SharedPointer accessor = + owned(CBitmapPixelAccess::create(bitmap.get())); + + if (!accessor) + return nullptr; + + const unsigned char* pixel = image.get(); + do { + CColor c(pixel[0], pixel[1], pixel[2], pixel[3]); + accessor->setColor(c); + pixel += 4; + } while (++*accessor); + accessor = nullptr; + + return bitmap; +} + +void downscaleToWidthAndHeight(VSTGUI::CBitmap* bitmap, VSTGUI::CPoint frameSize) +{ + if (!bitmap) + return; + + CCoord frameW = frameSize.x; + CCoord frameH = frameSize.y; + CCoord bitmapW = bitmap->getWidth(); + CCoord bitmapH = bitmap->getHeight(); + + CCoord scale = 1.0; + if (bitmapW > frameW || bitmapH > frameH) { + CCoord xScale = bitmapW / frameW; + CCoord yScale = bitmapH / frameH; + scale = (xScale > yScale) ? xScale : yScale; + } + + if (PlatformBitmapPtr platformBitmap = bitmap->getPlatformBitmap()) + platformBitmap->setScaleFactor(scale); +} diff --git a/plugins/editor/src/editor/ImageHelpers.h b/plugins/editor/src/editor/ImageHelpers.h new file mode 100644 index 00000000..8c51612e --- /dev/null +++ b/plugins/editor/src/editor/ImageHelpers.h @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: BSD-2-Clause + +// This code is part of the sfizz library and is licensed under a BSD 2-clause +// license. You should have receive a LICENSE.md file along with the code. +// If not, contact the sfizz maintainers at https://github.com/sfztools/sfizz + +#pragma once +#include + +#include "utility/vstgui_before.h" +#include "vstgui/vstgui.h" +#include "utility/vstgui_after.h" + +/** + * @brief Loads a bitmap from an image file, with a large support of formats + * through the stb_image library. + */ +VSTGUI::SharedPointer loadAnyFormatImage(const fs::path& filePath); + +/** + * @brief Adjust the scale factor of this bitmap, such that both its dimensions + * fit into a frame of the given size. + */ +void downscaleToWidthAndHeight(VSTGUI::CBitmap* bitmap, VSTGUI::CPoint frameSize);