Skip to content

Commit

Permalink
image_producer: implement scaling modes
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitry-ishenko committed Sep 19, 2024
1 parent c3f506b commit e41271e
Showing 1 changed file with 13 additions and 88 deletions.
101 changes: 13 additions & 88 deletions src/modules/image/producer/image_producer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <core/frame/draw_frame.h>
#include <core/frame/frame.h>
#include <core/frame/frame_factory.h>
#include <core/frame/geometry.h>
#include <core/frame/pixel_format.h>
#include <core/monitor/monitor.h>
#include <core/producer/frame_producer.h>
Expand Down Expand Up @@ -63,35 +64,30 @@ struct image_producer : public core::frame_producer
const uint32_t length_ = 0;
core::draw_frame frame_;

image_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, std::wstring description, uint32_t length)
: description_(std::move(description))
, frame_factory_(frame_factory)
, length_(length)
image_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, std::wstring description, uint32_t length, core::frame_geometry::scale_mode scale_mode) :
description_(std::move(description)), frame_factory_(frame_factory), length_(length)
{
load(load_image(description_));
load(load_image(description_), scale_mode);

CASPAR_LOG(info) << print() << L" Initialized";
}

image_producer(const spl::shared_ptr<core::frame_factory>& frame_factory,
const void* png_data,
size_t size,
uint32_t length)
: description_(L"png from memory")
, frame_factory_(frame_factory)
, length_(length)
image_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const void* png_data, size_t size, uint32_t length, core::frame_geometry::scale_mode scale_mode) :
description_(L"png from memory"), frame_factory_(frame_factory), length_(length)
{
load(load_png_from_memory(png_data, size));
load(load_png_from_memory(png_data, size), scale_mode);

CASPAR_LOG(info) << print() << L" Initialized";
}

void load(const std::shared_ptr<FIBITMAP>& bitmap)
void load(const std::shared_ptr<FIBITMAP>& bitmap, core::frame_geometry::scale_mode scale_mode)
{
FreeImage_FlipVertical(bitmap.get());
core::pixel_format_desc desc(core::pixel_format::bgra);
desc.planes.emplace_back(FreeImage_GetWidth(bitmap.get()), FreeImage_GetHeight(bitmap.get()), 4);

auto frame = frame_factory_->create_frame(this, desc);
frame.geometry() = core::frame_geometry::get_default(scale_mode);

std::copy_n(FreeImage_GetBits(bitmap.get()), frame.image_data(0).size(), frame.image_data(0).begin());
frame_ = core::draw_frame(std::move(frame));
Expand Down Expand Up @@ -120,19 +116,6 @@ struct image_producer : public core::frame_producer
core::monitor::state state() const override { return state_; }
};

// class ieq
//{
// std::wstring test_;
//
// public:
// explicit ieq(std::wstring test)
// : test_(std::move(test))
// {
// }
//
// bool operator()(const std::wstring& elem) const { return boost::iequals(elem, test_); }
// };

spl::shared_ptr<core::frame_producer> create_producer(const core::frame_producer_dependencies& dependencies,
const std::vector<std::wstring>& params)
{
Expand All @@ -141,72 +124,14 @@ spl::shared_ptr<core::frame_producer> create_producer(const core::frame_producer
}

auto length = get_param(L"LENGTH", params, std::numeric_limits<uint32_t>::max());
auto scale_mode = core::scale_mode_from_string(get_param(L"SCALE_MODE", params, L"STRETCH"));

// if (boost::iequals(params.at(0), L"[IMG_SEQUENCE]"))
//{
// if (params.size() != 2)
// return core::frame_producer::empty();

// auto dir = boost::filesystem::path(env::media_folder() + params.at(1)).parent_path();
// auto basename = boost::filesystem::basename(params.at(1));
// std::set<std::wstring> files;
// boost::filesystem::directory_iterator end;

// for (boost::filesystem::directory_iterator it(dir); it != end; ++it)
// {
// auto name = it->path().filename().wstring();

// if (!boost::algorithm::istarts_with(name, basename))
// continue;

// auto extension = it->path().extension().wstring();

// if (std::find_if(supported_extensions().begin(), supported_extensions().end(), ieq(extension)) ==
// supported_extensions().end()) continue;

// files.insert(it->path().wstring());
// }

// if (files.empty())
// return core::frame_producer::empty();

// int width = -1;
// int height = -1;
// std::vector<core::draw_frame> frames;
// frames.reserve(files.size());

// for (auto& file : files)
// {
// auto frame = load_image(dependencies.frame_factory, file);

// if (width == -1)
// {
// width = static_cast<int>(frame.second.width.get());
// height = static_cast<int>(frame.second.height.get());
// }

// frames.push_back(std::move(frame.first));
// }

// return core::create_const_producer(std::move(frames), width, height);
//}
// else
// if (boost::iequals(params.at(0), L"[PNG_BASE64]")) {
// if (params.size() < 2)
// return core::frame_producer::empty();

// auto png_data = from_base64(std::string(params.at(1).begin(), params.at(1).end()));

// return spl::make_shared<image_producer>(dependencies.frame_factory, png_data.data(), png_data.size(), length);
//}

std::optional<boost::filesystem::path> filename =
find_file_within_dir_or_absolute(env::media_folder(), params.at(0), is_valid_file);
auto filename = find_file_within_dir_or_absolute(env::media_folder(), params.at(0), is_valid_file);
if (!filename) {
return core::frame_producer::empty();
}

return spl::make_shared<image_producer>(dependencies.frame_factory, filename->wstring(), length);
return spl::make_shared<image_producer>(dependencies.frame_factory, filename->wstring(), length, scale_mode);
}

}} // namespace caspar::image

0 comments on commit e41271e

Please sign in to comment.