Skip to content

Commit

Permalink
Add xtd::drawing::image::from_xbm_data and xtd::drawing::image::from_…
Browse files Browse the repository at this point in the history
…xpm_data methods
  • Loading branch information
gammasoft71 committed Feb 25, 2025
1 parent 4caf480 commit 757a97e
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ intptr image::create(const char* const* bits, std::map<size_t, size_t>& frame_re
return reinterpret_cast<intptr>(img);
}

intptr image::create(const unsigned char* bits, int32 width, int32 height, std::map<size_t, size_t>& frame_resolutions) {
toolkit::initialize(); // Must be first
auto img = new wxImage(wxBitmap((char*)bits, width, height).ConvertToImage());
if (!img->IsOk()) {
delete img;
return invalid_handle;
}
frame_resolutions[get_frame_resolution(*img)] = 1;
return reinterpret_cast<intptr>(img);
}

intptr image::create(int32 width, int32 height) {
toolkit::initialize(); // Must be first
wxImage* img = new wxImage(width, height);
Expand Down
13 changes: 11 additions & 2 deletions src/xtd.drawing.native/include/xtd/drawing/native/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
/// @endcond

#include <istream>
#include <map>
#include <tuple>
#include <vector>
#include <xtd/collections/generic/dictionary>
#include <xtd/static>
#include <xtd/types>
#include <xtd/string>
Expand Down Expand Up @@ -325,9 +325,18 @@ namespace xtd {
/// @param bits The bits containing the image.
/// @param frame_resolutions an std::map<size_t, size_t> containing the frame dimention and the image count collection (see frame_dimension.h for more information).
/// @return A new image handle.
/// @remarks This method is used for creating an imgae from an XPM (or XBM) image.
/// @remarks This method is used for creating an imgae from an XPM image.
/// @warning Internal use only
static intptr create(const char* const* bits, std::map<size_t, size_t>& frame_resolutions);
/// @brief Creates an image from bits.
/// @param bits The bits containing the image.
/// @param width The width for the image.
/// @param height The height for the image.
/// @param frame_resolutions an std::map<size_t, size_t> containing the frame dimention and the image count collection (see frame_dimension.h for more information).
/// @return A new image handle.
/// @remarks This method is used for creating an imgae from an XBM image.
/// @warning Internal use only
static intptr create(const unsigned char* bits, int32 width, int32 height, std::map<size_t, size_t>& frame_resolutions);
/// @brief Creates an empty image from size.
/// @param width The width for the empty image.
/// @param height The height for the empty image.
Expand Down
2 changes: 2 additions & 0 deletions src/xtd.drawing/include/xtd/drawing/bitmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ namespace xtd {
explicit bitmap(std::istream& stream, bool use_icm);
/// @brief Initializes a new instance of the bitmap class from the specified data xpm.
/// @param bits The data xpm used to load the image.
/// @deprecated Replaced by xtd::drawing::image::from_xpm - Will be removed in version 0.4.0.
[[deprecated("Replaced by xtd::drawing::image::from_xpm - Will be removed in version 0.4.0.")]]
explicit bitmap(const char* const* bits);
/// @brief Initializes a new instance of the xtd::drawing::bitmap class with the specified size.
/// @param width The width, in pixels, of the new bitmap.
Expand Down
21 changes: 19 additions & 2 deletions src/xtd.drawing/include/xtd/drawing/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,27 @@ namespace xtd {
static image from_stream(std::istream& stream);

/// @brief Creates an xtd::drawing::image from the specified data pointer.
/// @param data A pointer that contains the data for this xtd::drawing::image.
/// @param data A pointer that contains the data for the xtd::drawing::image.
/// @return The xtd::drawing::image this method creates.
/// @remarks This method is used for creating a xtd::drawing::image from an XPM (or XBM) image.
/// @remarks This method is used for creating a xtd::drawing::image from an XPM image.
/// @deprecated Replaced by xtd::drawing::image::from_xpm - Will be removed in version 0.4.0.
[[deprecated("Replaced by xtd::drawing::image::from_xpm - Will be removed in version 0.4.0.")]]
static image from_data(const char* const* bits);

/// @brief Creates an xtd::drawing::image from the specified data pointer, width and hieght.
/// @param data A pointer that contains the data for the xtd::drawing::image.
/// @param width The width for the xtd::drawing::image.
/// @param height The height for the xtd::drawing::image.
/// @return The xtd::drawing::image this method creates.
/// @remarks This method is used for creating a xtd::drawing::image from an XBM image.
static bitmap from_xbm_data(const unsigned char* bits, int32 width, int32 height);

/// @brief Creates an xtd::drawing::image from the specified data pointer.
/// @param data A pointer that contains the data for the xtd::drawing::image.
/// @return The xtd::drawing::image this method creates.
/// @remarks This method is used for creating a xtd::drawing::image from an XPM image.
static bitmap from_xpm_data(const char* const* bits);

/// @brief Returns the color depth, in number of bits per pixel, of the specified pixel format.
/// @param pixfmt The xtd::drawing::imaging::pixel_format member that specifies the format for which to find the size.
/// @return The color depth of the specified pixel format.
Expand Down Expand Up @@ -330,6 +346,7 @@ namespace xtd {
explicit image(const xtd::string& filename, bool use_icm);
explicit image(std::istream& stream);
explicit image(std::istream& stream, bool use_icm);
[[deprecated("Replaced by xtd::drawing::image::from_xpm - Will be removed in version 0.4.0.")]]
explicit image(const char* const* bits);
image(int32 width, int32 height);
image(int32 width, int32 height, float horizontal_resolution, float vertical_resolution);
Expand Down
46 changes: 34 additions & 12 deletions src/xtd.drawing/src/xtd/drawing/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,7 @@ image::image(std::istream& stream, bool use_icm) : data_(xtd::new_sptr<data>())
}

image::image(const char* const* bits) : data_(xtd::new_sptr<data>()) {
auto frame_resolutions = std::map<xtd::size, xtd::size> {};
data_->handle = native::image::create(bits, frame_resolutions);
if (data_->handle == invalid_handle) throw_helper::throws(exception_case::argument);
data_->raw_format = imaging::image_format::memory_xpm();
for (auto frame_resolution : frame_resolutions) {
if (frame_resolution.first == FD_PAGE) data_->frame_dimensions[imaging::frame_dimension::page().guid()] = frame_resolution.second;
else if (frame_resolution.first == FD_RESOLUTION) data_->frame_dimensions[imaging::frame_dimension::resolution().guid()] = frame_resolution.second;
else if (frame_resolution.first == FD_TIME) data_->frame_dimensions[imaging::frame_dimension::time().guid()] = frame_resolution.second;
else throw_helper::throws(exception_case::argument);
}
update_properties();
*this = from_xpm_data(bits);
}

image::image(int32 width, int32 height) : data_(xtd::new_sptr<data>()) {
Expand Down Expand Up @@ -406,7 +396,7 @@ image image::from_stream(std::istream& stream) { // stream param can't be const
}

image image::from_data(const char* const* bits) {
return image {bits};
return from_xpm_data(bits);
}

image image::from_hicon(intptr hicon) {
Expand All @@ -417,6 +407,38 @@ image image::from_hicon(intptr hicon) {
return result;
}

bitmap image::from_xbm_data(const unsigned char* bits, int32 width, int32 height) {
auto img = image {};
auto frame_resolutions = std::map<xtd::size, xtd::size> {};
img.data_->handle = native::image::create(bits, width, height, frame_resolutions);
if (img.data_->handle == invalid_handle) throw_helper::throws(exception_case::argument);
img.data_->raw_format = imaging::image_format::memory_xpm();
for (auto frame_resolution : frame_resolutions) {
if (frame_resolution.first == FD_PAGE) img.data_->frame_dimensions[imaging::frame_dimension::page().guid()] = frame_resolution.second;
else if (frame_resolution.first == FD_RESOLUTION) img.data_->frame_dimensions[imaging::frame_dimension::resolution().guid()] = frame_resolution.second;
else if (frame_resolution.first == FD_TIME) img.data_->frame_dimensions[imaging::frame_dimension::time().guid()] = frame_resolution.second;
else throw_helper::throws(exception_case::argument);
}
img.update_properties();
return bitmap {img};
}

bitmap image::from_xpm_data(const char* const* bits) {
auto img = image {};
auto frame_resolutions = std::map<xtd::size, xtd::size> {};
img.data_->handle = native::image::create(bits, frame_resolutions);
if (img.data_->handle == invalid_handle) throw_helper::throws(exception_case::argument);
img.data_->raw_format = imaging::image_format::memory_xpm();
for (auto frame_resolution : frame_resolutions) {
if (frame_resolution.first == FD_PAGE) img.data_->frame_dimensions[imaging::frame_dimension::page().guid()] = frame_resolution.second;
else if (frame_resolution.first == FD_RESOLUTION) img.data_->frame_dimensions[imaging::frame_dimension::resolution().guid()] = frame_resolution.second;
else if (frame_resolution.first == FD_TIME) img.data_->frame_dimensions[imaging::frame_dimension::time().guid()] = frame_resolution.second;
else throw_helper::throws(exception_case::argument);
}
img.update_properties();
return bitmap {img};
}

drawing::color image::get_pixel(int32 x, int32 y) const {
if (x < 0 || x > width() || y < 0 || y > height()) throw_helper::throws(exception_case::argument);

Expand Down
8 changes: 4 additions & 4 deletions tools/xtdc-gui/src/main_form.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ main_form::main_form() {

startup_open_project_button_.parent(startup_panel_);
startup_open_project_button_.image(images::from_name("document-open", drawing::size {48, 48}));
startup_open_project_button_.image(bitmap(xtd_open_icon));
startup_open_project_button_.image(bitmap::from_xpm_data(xtd_open_icon));
startup_open_project_button_.image_align(content_alignment::middle_left);
startup_open_project_button_.text("Open a project or solution");
startup_open_project_button_.location({startup_panel_.size().width - 400, 175});
Expand All @@ -115,7 +115,7 @@ main_form::main_form() {

startup_run_project_button_.parent(startup_panel_);
startup_run_project_button_.image(images::from_name("system-run", drawing::size {48, 48}));
startup_run_project_button_.image(bitmap(xtd_run_icon));
startup_run_project_button_.image(bitmap::from_xpm_data(xtd_run_icon));
startup_run_project_button_.image_align(content_alignment::middle_left);
startup_run_project_button_.text("Run a project");
startup_run_project_button_.location({startup_panel_.size().width - 400, 285});
Expand All @@ -126,7 +126,7 @@ main_form::main_form() {

startup_new_project_button_.parent(startup_panel_);
startup_new_project_button_.image(images::from_name("document-new", drawing::size {48, 48}));
startup_new_project_button_.image(bitmap(xtd_new_icon));
startup_new_project_button_.image(bitmap::from_xpm_data(xtd_new_icon));
startup_new_project_button_.image_align(content_alignment::middle_left);
startup_new_project_button_.text("Create a new project");
startup_new_project_button_.location({startup_panel_.size().width - 400, 395});
Expand All @@ -137,7 +137,7 @@ main_form::main_form() {

startup_open_xtd_examples_button_.parent(startup_panel_);
startup_open_xtd_examples_button_.image(images::from_name("xtd", drawing::size {48, 48}));
startup_open_xtd_examples_button_.image(bitmap(xtd_open_examples_icon));
startup_open_xtd_examples_button_.image(bitmap::from_xpm_data(xtd_open_examples_icon));
startup_open_xtd_examples_button_.image_align(content_alignment::middle_left);
startup_open_xtd_examples_button_.text("Open xtd examples");
startup_open_xtd_examples_button_.location({startup_panel_.size().width - 400, 505});
Expand Down
8 changes: 4 additions & 4 deletions tools/xtdc-gui/src/main_form.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ namespace xtdc_gui {

settings_form settings_form_;

xtd::forms::menu_item file_open_project_menu_item_ {"Open a project or solution", {*this, overload_<>(&main_form::open_project)}, xtd::drawing::bitmap(xtd::drawing::bitmap(xtd_open_examples_icon), xtd::forms::menu_images::size()), xtd::forms::shortcut::ctrl_o};
xtd::forms::menu_item file_run_project_menu_item_ {"Run a project", {*this, overload_<>(&main_form::run_project)}, xtd::drawing::bitmap(xtd::drawing::bitmap(xtd_run_icon), xtd::forms::menu_images::size()), xtd::forms::shortcut::ctrl_r};
xtd::forms::menu_item file_create_new_project_menu_item_ {"Create new project", {*this, overload_<>(&main_form::new_project)}, xtd::drawing::bitmap(xtd::drawing::bitmap(xtd_new_icon), xtd::forms::menu_images::size()), xtd::forms::shortcut::ctrl_n};
xtd::forms::menu_item file_open_xtd_examples_menu_item_ {"Open xtd examples", {*this, &main_form::open_xtd_examples}, xtd::drawing::bitmap(xtd::drawing::bitmap(xtd_open_icon), xtd::forms::menu_images::size()), xtd::forms::shortcut::ctrl_e};
xtd::forms::menu_item file_open_project_menu_item_ {"Open a project or solution", {*this, overload_<>(&main_form::open_project)}, xtd::drawing::bitmap(xtd::drawing::bitmap::from_xpm_data(xtd_open_examples_icon), xtd::forms::menu_images::size()), xtd::forms::shortcut::ctrl_o};
xtd::forms::menu_item file_run_project_menu_item_ {"Run a project", {*this, overload_<>(&main_form::run_project)}, xtd::drawing::bitmap(xtd::drawing::bitmap::from_xpm_data(xtd_run_icon), xtd::forms::menu_images::size()), xtd::forms::shortcut::ctrl_r};
xtd::forms::menu_item file_create_new_project_menu_item_ {"Create new project", {*this, overload_<>(&main_form::new_project)}, xtd::drawing::bitmap(xtd::drawing::bitmap::from_xpm_data(xtd_new_icon), xtd::forms::menu_images::size()), xtd::forms::shortcut::ctrl_n};
xtd::forms::menu_item file_open_xtd_examples_menu_item_ {"Open xtd examples", {*this, &main_form::open_xtd_examples}, xtd::drawing::bitmap(xtd::drawing::bitmap::from_xpm_data(xtd_open_icon), xtd::forms::menu_images::size()), xtd::forms::shortcut::ctrl_e};
xtd::forms::menu_item file_separator1_menu_item_ {"-"};
xtd::forms::menu_item file_settings_menu_item_ {xtd::drawing::texts::settings(), {*this, &main_form::settings}, xtd::forms::shortcut::f2};
xtd::forms::menu_item file_separator2_menu_item_ {"-"};
Expand Down
Loading

0 comments on commit 757a97e

Please sign in to comment.