Move image utilities in a ImageHelpers file
This commit is contained in:
parent
4783f21b31
commit
4b5b190fbe
4 changed files with 103 additions and 66 deletions
|
|
@ -45,6 +45,8 @@ add_library(sfizz_editor STATIC EXCLUDE_FROM_ALL
|
||||||
src/editor/GUIPiano.cpp
|
src/editor/GUIPiano.cpp
|
||||||
src/editor/ColorHelpers.h
|
src/editor/ColorHelpers.h
|
||||||
src/editor/ColorHelpers.cpp
|
src/editor/ColorHelpers.cpp
|
||||||
|
src/editor/ImageHelpers.h
|
||||||
|
src/editor/ImageHelpers.cpp
|
||||||
src/editor/NativeHelpers.h
|
src/editor/NativeHelpers.h
|
||||||
src/editor/NativeHelpers.cpp
|
src/editor/NativeHelpers.cpp
|
||||||
src/editor/layout/main.hpp
|
src/editor/layout/main.hpp
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,11 @@
|
||||||
|
|
||||||
#include "Editor.h"
|
#include "Editor.h"
|
||||||
#include "EditorController.h"
|
#include "EditorController.h"
|
||||||
#include "EditorLibs.h"
|
|
||||||
#include "EditIds.h"
|
#include "EditIds.h"
|
||||||
#include "GUIComponents.h"
|
#include "GUIComponents.h"
|
||||||
#include "GUIHelpers.h"
|
#include "GUIHelpers.h"
|
||||||
#include "GUIPiano.h"
|
#include "GUIPiano.h"
|
||||||
|
#include "ImageHelpers.h"
|
||||||
#include "NativeHelpers.h"
|
#include "NativeHelpers.h"
|
||||||
#include "BitArray.h"
|
#include "BitArray.h"
|
||||||
#include "plugin/MessageUtils.h"
|
#include "plugin/MessageUtils.h"
|
||||||
|
|
@ -40,11 +40,6 @@ using namespace VSTGUI;
|
||||||
const int Editor::viewWidth { 800 };
|
const int Editor::viewWidth { 800 };
|
||||||
const int Editor::viewHeight { 475 };
|
const int Editor::viewHeight { 475 };
|
||||||
|
|
||||||
struct image_deleter {
|
|
||||||
void operator()(unsigned char* x) const noexcept { stbi_image_free(x); }
|
|
||||||
};
|
|
||||||
typedef std::unique_ptr<unsigned char[], image_deleter> image_u;
|
|
||||||
|
|
||||||
struct Editor::Impl : EditorController::Receiver, IControlListener {
|
struct Editor::Impl : EditorController::Receiver, IControlListener {
|
||||||
EditorController* ctrl_ = nullptr;
|
EditorController* ctrl_ = nullptr;
|
||||||
CFrame* frame_ = nullptr;
|
CFrame* frame_ = nullptr;
|
||||||
|
|
@ -242,8 +237,6 @@ struct Editor::Impl : EditorController::Receiver, IControlListener {
|
||||||
const char* keyName = keyNames[key % 12];
|
const char* keyName = keyNames[key % 12];
|
||||||
return std::string(keyName) + ' ' + std::to_string(octave);
|
return std::string(keyName) + ' ' + std::to_string(octave);
|
||||||
}
|
}
|
||||||
|
|
||||||
static SharedPointer<CBitmap> loadAnyFormatImage(const fs::path& filePath);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Editor::Editor(EditorController& ctrl)
|
Editor::Editor(EditorController& ctrl)
|
||||||
|
|
@ -1579,48 +1572,6 @@ void Editor::Impl::updateSWLastLabel(unsigned sw, const char* label)
|
||||||
updateKeyswitchNameLabel();
|
updateKeyswitchNameLabel();
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedPointer<CBitmap> 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<CBitmap> 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<CBitmap>(width, height);
|
|
||||||
SharedPointer<CBitmapPixelAccess> 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)
|
void Editor::Impl::updateBackgroundImage(const char* filepath)
|
||||||
{
|
{
|
||||||
const fs::path sfzFilePath = fs::u8path(currentSfzFile_);
|
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);
|
const fs::path imagePath = sfzDirPath / fs::u8path(filepath);
|
||||||
SharedPointer<CBitmap> bitmap = loadAnyFormatImage(imagePath);
|
SharedPointer<CBitmap> bitmap = loadAnyFormatImage(imagePath);
|
||||||
|
|
||||||
if (bitmap) {
|
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 {
|
|
||||||
bitmap = owned(new CBitmap("background.png"));
|
bitmap = owned(new CBitmap("background.png"));
|
||||||
}
|
|
||||||
|
downscaleToWidthAndHeight(bitmap, imageContainer_->getViewSize().getSize());
|
||||||
imageContainer_->setBackground(bitmap);
|
imageContainer_->setBackground(bitmap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
73
plugins/editor/src/editor/ImageHelpers.cpp
Normal file
73
plugins/editor/src/editor/ImageHelpers.cpp
Normal file
|
|
@ -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 <cstdio>
|
||||||
|
|
||||||
|
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<unsigned char[], stbi_image_delete>;
|
||||||
|
|
||||||
|
SharedPointer<CBitmap> 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<CBitmap> bitmap = makeOwned<CBitmap>(width, height);
|
||||||
|
SharedPointer<CBitmapPixelAccess> 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);
|
||||||
|
}
|
||||||
24
plugins/editor/src/editor/ImageHelpers.h
Normal file
24
plugins/editor/src/editor/ImageHelpers.h
Normal file
|
|
@ -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 <ghc/fs_std.hpp>
|
||||||
|
|
||||||
|
#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<VSTGUI::CBitmap> 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);
|
||||||
Loading…
Add table
Reference in a new issue