Merge branch 'hotfix-1.6.2' of https://github.com/cnjinhao/nana into hotfix-1.6.2

This commit is contained in:
James Bremner
2019-01-16 16:38:20 -05:00
8 changed files with 213 additions and 177 deletions

View File

@@ -163,7 +163,8 @@ namespace nana
bld_fgcolor = fgcolor.blend(highlighted, 0.6);
break;
case element_state::disabled:
bld_bgcolor = bld_fgcolor = static_cast<color_rgb>(0xb2b7bc);
bld_bgcolor = static_cast<color_rgb>(0xE0E0E0);
bld_fgcolor = static_cast<color_rgb>(0x999A9E);
break;
default:
//Leave things as they are

View File

@@ -85,9 +85,13 @@ namespace nana{ namespace drawerbase
graph.text_metrics(txt_px, descent, ileading);
txt_px += (descent + 2);
auto e_state = API::element_state(*wdg);
if(!wdg->enabled())
e_state = element_state::disabled;
impl_->crook.draw(graph,
impl_->scheme_ptr->square_bgcolor.get(wdg->bgcolor()), impl_->scheme_ptr->square_border_color.get(wdg->fgcolor()),
rectangle(0, txt_px > 16 ? (txt_px - 16) / 2 : 0, 16, 16), API::element_state(*wdg));
rectangle(0, txt_px > 16 ? (txt_px - 16) / 2 : 0, 16, 16), e_state);
}
void drawer::mouse_down(graph_reference graph, const arg_mouse&)
@@ -212,6 +216,7 @@ namespace nana{ namespace drawerbase
{
e.uiobj->radio(false);
e.uiobj->react(true);
API::umake_event(e.eh_clicked);
API::umake_event(e.eh_checked);
API::umake_event(e.eh_destroy);
API::umake_event(e.eh_keyboard);
@@ -228,7 +233,7 @@ namespace nana{ namespace drawerbase
el.uiobj = &uiobj;
uiobj.events().checked.connect_unignorable([this](const arg_checkbox& arg)
el.eh_checked = uiobj.events().checked.connect_unignorable([this](const arg_checkbox& arg)
{
if (arg.widget->checked())
{
@@ -240,7 +245,7 @@ namespace nana{ namespace drawerbase
}
}, true);
el.eh_checked = uiobj.events().click.connect_unignorable([this](const arg_click& arg)
el.eh_clicked = uiobj.events().click.connect_unignorable([this](const arg_click& arg)
{
for (auto & i : ui_container_)
i.uiobj->check(arg.window_handle == i.uiobj->handle());

View File

@@ -33,13 +33,14 @@ namespace nana
static const char* field_title = "__nana_group_title__";
static const char* field_options = "__nana_group_options__";
struct group::implement
{
label caption;
align caption_align{ align::left };
place place_content;
unsigned gap{2};
std::string usr_div_str;
struct group::implement
{
label caption;
align caption_align{ align::left };
background_mode caption_mode{ background_mode::blending };
place place_content;
unsigned gap{2};
std::string usr_div_str;
nana::size caption_dimension;
@@ -143,200 +144,216 @@ checkbox& group::add_option(std::string text)
impl_->options.emplace_back(new checkbox(handle()));
auto & opt = impl_->options.back();
#endif
opt->transparent(true);
opt->caption(std::move(text));
impl_->place_content[field_options] << *opt;
impl_->place_content.field_display(field_options, true);
impl_->place_content.collocate();
if (impl_->radio_logic)
impl_->radio_logic->add(*opt);
opt->transparent(true);
opt->caption(std::move(text));
impl_->place_content[field_options] << *opt;
impl_->place_content.field_display(field_options, true);
impl_->place_content.collocate();
return *impl_->options.back();
}
if (impl_->radio_logic)
impl_->radio_logic->add(*opt);
void group::caption_align(align position)
{
if (position != impl_->caption_align)
{
impl_->caption_align = position;
impl_->update_div();
impl_->place_content.collocate();
API::refresh_window(*this);
}
}
return *impl_->options.back();
}
group& group::radio_mode(bool enable)
{
_THROW_IF_EMPTY()
group& group::caption_align(align position)
{
if (position != impl_->caption_align)
{
impl_->caption_align = position;
impl_->update_div();
impl_->place_content.collocate();
API::refresh_window(*this);
}
return *this;
}
if (enable)
{
//Create radio_group if it is null
if (!impl_->radio_logic)
impl_->radio_logic = new ::nana::radio_group;
group& group::caption_background_mode(background_mode mode)
{
if (mode != impl_->caption_mode)
{
impl_->caption_mode = mode;
switch (mode)
{
case background_mode::none:
impl_->caption.bgcolor(this->bgcolor());
impl_->caption.transparent(false);
break;
case background_mode::blending:
impl_->caption.transparent(true);
impl_->caption.bgcolor(API::bgcolor(this->parent()).blend(colors::black, 0.025));
break;
case background_mode::transparent:
impl_->caption.transparent(true);
impl_->caption.bgcolor(API::bgcolor(this->parent()).blend(colors::black, 0.025));
break;
}
API::refresh_window(*this);
}
return *this;
}
//add all options into the radio_group
for (auto & opt : impl_->options)
impl_->radio_logic->add(*opt);
}
else
{
delete impl_->radio_logic;
impl_->radio_logic = nullptr;
}
return *this;
}
group& group::radio_mode(bool enable)
{
_THROW_IF_EMPTY()
std::size_t group::option() const
{
_THROW_IF_EMPTY();
if (enable)
{
//Create radio_group if it is null
if (!impl_->radio_logic)
impl_->radio_logic = new ::nana::radio_group;
if (impl_->radio_logic)
return impl_->radio_logic->checked();
//add all options into the radio_group
for (auto & opt : impl_->options)
impl_->radio_logic->add(*opt);
}
else
{
delete impl_->radio_logic;
impl_->radio_logic = nullptr;
}
return *this;
}
throw std::logic_error("the radio_mode of the group is disabled");
}
std::size_t group::option() const
{
_THROW_IF_EMPTY();
bool group::option_checked(std::size_t pos) const
{
_THROW_IF_EMPTY();
return impl_->options.at(pos)->checked();
}
if (impl_->radio_logic)
return impl_->radio_logic->checked();
void group::typeface( const nana::paint::font& font )
{
// change typeface of caption label
impl_->caption.typeface( font );
throw std::logic_error("the radio_mode of the group is disabled");
}
/* change size of caption label
bool group::option_checked(std::size_t pos) const
{
_THROW_IF_EMPTY();
return impl_->options.at(pos)->checked();
}
The caption may be changed AFTER this call
so the neccessary label size is unknown
set it to 80% of the current widget width and 50 pixels
*/
impl_->caption.move( rectangle(0,0,size().width * 0.8,50));
}
group& group::enable_format_caption(bool format)
{
impl_->caption.format(format);
return *this;
}
group& group::enable_format_caption(bool format)
{
impl_->caption.format(format);
return *this;
}
group& group::collocate() noexcept
{
impl_->place_content.collocate();
return *this;
}
group& group::collocate() noexcept
{
impl_->place_content.collocate();
return *this;
}
group& group::div(const char* div_str) noexcept
{
if (div_str)
impl_->usr_div_str = div_str;
else
impl_->usr_div_str.clear();
group& group::div(const char* div_str) noexcept
{
if (div_str)
impl_->usr_div_str = div_str;
else
impl_->usr_div_str.clear();
impl_->update_div();
return *this;
}
impl_->update_div();
return *this;
}
group::field_reference group::operator[](const char* field)
{
return impl_->place_content.field(field);
}
group::field_reference group::operator[](const char* field)
{
return impl_->place_content.field(field);
}
void group::field_display(const char* field_name, bool display)
{
impl_->place_content.field_display(field_name, display);
}
void group::field_display(const char* field_name, bool display)
{
impl_->place_content.field_display(field_name, display);
}
bool group::field_display(const char* field_name) const
{
return impl_->place_content.field_display(field_name);
}
bool group::field_display(const char* field_name) const
{
return impl_->place_content.field_display(field_name);
}
void group::erase(window handle)
{
impl_->place_content.erase(handle);
}
void group::erase(window handle)
{
impl_->place_content.erase(handle);
}
void group::_m_add_child(const char* field, widget* wdg)
{
impl_->place_content[field] << wdg->handle();
}
void group::_m_add_child(const char* field, widget* wdg)
{
impl_->place_content[field] << wdg->handle();
}
void group::_m_init()
{
this->div(nullptr);
void group::_m_init()
{
this->div(nullptr);
auto & outter = impl_->place_content;
auto & outter = impl_->place_content;
outter[field_title] << impl_->caption;
outter.collocate();
outter[field_title] << impl_->caption;
outter.collocate();
impl_->caption.transparent(true);
color pbg = API::bgcolor(this->parent());
impl_->caption.bgcolor(pbg.blend(colors::black, 0.025));
impl_->caption.transparent(true);
color pbg = API::bgcolor(this->parent());
impl_->caption.bgcolor(pbg.blend(colors::black, 0.025));
this->bgcolor(pbg.blend(colors::black, 0.05));
this->bgcolor(pbg.blend(colors::black, 0.05));
drawing dw(*this);
drawing dw(*this);
//When the group is resized, the drawing is called before moving the caption, but
//the drawing of group requires the lastest position of caption for gradual rectangle.
//For the requirement, a move event handler is required for listning the change of caption's position.
impl_->caption.events().move([this](const arg_move&){
if (align::left != impl_->caption_align)
API::refresh_window(*this);
});
//When the group is resized, the drawing is called before moving the caption, but
//the drawing of group requires the lastest position of caption for gradual rectangle.
//For the requirement, a move event handler is required for listning the change of caption's position.
impl_->caption.events().move([this](const arg_move&)
{
if (align::left != impl_->caption_align)
API::refresh_window(*this);
});
// This drawing function is owner by the onwer of dw (the outer panel of the group widget), not by dw !!
dw.draw([this](paint::graphics& graph)
{
auto gap_px = impl_->gap - 1;
// This drawing function is owner by the onwer of dw (the outer panel of the group widget), not by dw !!
dw.draw([this](paint::graphics& graph)
{
auto gap_px = impl_->gap - 1;
auto const top_round_line = static_cast<int>(impl_->caption_dimension.height) / 2;
auto const top_round_line = static_cast<int>(impl_->caption_dimension.height) / 2;
graph.rectangle(true, API::bgcolor(this->parent()));
graph.round_rectangle(rectangle(point(gap_px, top_round_line),
nana::size(graph.width() - 2 * gap_px, graph.height() - top_round_line - gap_px)
),
3, 3, this->scheme().border, true, this->bgcolor());
graph.rectangle(true, API::bgcolor(this->parent()));
graph.round_rectangle(rectangle(point(gap_px, top_round_line),
nana::size(graph.width() - 2 * gap_px, graph.height() - top_round_line - gap_px)
),
3, 3, this->scheme().border, true, this->bgcolor());
if (background_mode::blending == impl_->caption_mode)
{
auto opt_r = API::window_rectangle(impl_->caption);
if (opt_r)
{
rectangle grad_r{ opt_r->position(), nana::size{ opt_r->width + 4, static_cast<unsigned>(top_round_line - opt_r->y) } };
auto opt_r = API::window_rectangle(impl_->caption);
if (opt_r)
{
rectangle grad_r{ opt_r->position(), nana::size{ opt_r->width + 4, static_cast<unsigned>(top_round_line - opt_r->y) } };
grad_r.y += top_round_line * 2 / 3;
grad_r.x -= 2;
grad_r.y += top_round_line*2 / 3;
grad_r.x -= 2;
graph.gradual_rectangle(grad_r,
API::bgcolor(this->parent()), this->bgcolor(), true
);
}
}
});
}
graph.gradual_rectangle(grad_r,
API::bgcolor(this->parent()), this->bgcolor(), true
);
}
});
}
void group::_m_complete_creation()
{
widget::_m_complete_creation();
impl_->create(handle());
_m_init();
}
void group::_m_complete_creation()
{
widget::_m_complete_creation();
impl_->create(handle());
_m_init();
}
auto group::_m_caption() const noexcept -> native_string_type
{
return impl_->caption.caption_native();
}
auto group::_m_caption() const noexcept -> native_string_type
{
return impl_->caption.caption_native();
}
void group::_m_caption(native_string_type&& str)
{
impl_->caption.caption(std::move(str));
impl_->update_div();
impl_->place_content.collocate();
}
void group::_m_caption(native_string_type&& str)
{
impl_->caption.caption(std::move(str));
impl_->update_div();
impl_->place_content.collocate();
}
}//end namespace nana

View File

@@ -1,7 +1,7 @@
/*
* A text editor implementation
* Nana C++ Library(http://www.nanapro.org)
* Copyright(C) 2003-2018 Jinhao(cnjinhao@hotmail.com)
* Copyright(C) 2003-2019 Jinhao(cnjinhao@hotmail.com)
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
@@ -1728,7 +1728,7 @@ namespace nana {
str = impl_->textbase.getline(0);
for (std::size_t i = 1; i < lines; ++i)
{
str += L"\n\r";
str += L"\r\n";
str += impl_->textbase.getline(i);
}
}
@@ -2002,7 +2002,7 @@ namespace nana {
auto fgcolor = scheme_->foreground.get_color();
if (!API::window_enabled(window_))
fgcolor.blend(bgcolor, 0.5);
fgcolor = fgcolor.blend(bgcolor, 0.5); //Thank to besh81 for getting the fgcolor to be changed
if (API::widget_borderless(window_))
graph_.rectangle(false, bgcolor);

View File

@@ -1,7 +1,7 @@
#include <nana/push_ignore_diagnostic>
#include <nana/paint/detail/image_process_provider.hpp>
#include <nana/paint/detail/image_processor.hpp>
#include "image_processor.hpp"
namespace nana
{

View File

@@ -0,0 +1,696 @@
/*
* Image Processor Algorithm Implementation
* Nana C++ Library(http://www.nanapro.org)
* Copyright(C) 2003-2018 Jinhao(cnjinhao@hotmail.com)
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* @file: nana/paint/detail/image_processor.hpp
* @brief: This header file implements the algorithms of image processor
*
* DON'T INCLUDE THIS HEADER FILE DIRECTLY TO YOUR SOURCE FILE.
*/
#ifndef NANA_PAINT_DETAIL_IMAGE_PROCESSOR_HPP
#define NANA_PAINT_DETAIL_IMAGE_PROCESSOR_HPP
#include <nana/paint/pixel_buffer.hpp>
#include <nana/paint/image_process_interface.hpp>
#include <nana/paint/detail/native_paint_interface.hpp>
#include <algorithm>
namespace nana
{
namespace paint
{
namespace detail
{
namespace algorithms
{
///@brief Seek a pixel address by using offset bytes
///@return the specified pixel address
inline pixel_color_t * pixel_at(pixel_color_t * p, std::size_t bytes)
{
return reinterpret_cast<pixel_color_t*>(reinterpret_cast<char*>(p) + bytes);
}
inline const pixel_color_t * pixel_at(const pixel_color_t * p, std::size_t bytes)
{
return reinterpret_cast<const pixel_color_t*>(reinterpret_cast<const char*>(p) + bytes);
}
class proximal_interoplation
: public image_process::stretch_interface
{
void process(const paint::pixel_buffer& s_pixbuf, const nana::rectangle& r_src, paint::pixel_buffer & pixbuf, const nana::rectangle& r_dst) const
{
const auto bytes_per_line = s_pixbuf.bytes_per_line();
double rate_x = double(r_src.width) / r_dst.width;
double rate_y = double(r_src.height) / r_dst.height;
pixel_argb_t * s_raw_pixbuf = s_pixbuf.raw_ptr(0);
if(s_pixbuf.alpha_channel())
{
for(std::size_t row = 0; row < r_dst.height; ++row)
{
const pixel_argb_t * s_line = pixel_at(s_raw_pixbuf, (static_cast<int>(row * rate_y) + r_src.y) * bytes_per_line);
pixel_argb_t * i = pixbuf.raw_ptr(r_dst.y + row);
for(std::size_t x = 0; x < r_dst.width; ++x, ++i)
{
const pixel_argb_t * s = s_line + static_cast<int>(x * rate_x) + r_src.x;
if(0 == s->element.alpha_channel)
continue;
if(s->element.alpha_channel != 255)
{
i->element.red = unsigned(i->element.red * (255 - s->element.alpha_channel) + s->element.red * s->element.alpha_channel) / 255;
i->element.green = unsigned(i->element.green * (255 - s->element.alpha_channel) + s->element.green * s->element.alpha_channel) / 255;
i->element.blue = unsigned(i->element.blue * (255 - s->element.alpha_channel) + s->element.blue * s->element.alpha_channel) / 255;
}
else
{
unsigned alpha_chn = i->element.alpha_channel;
*i = *s;
i->element.alpha_channel = alpha_chn;
}
}
}
}
else
{
for(std::size_t row = 0; row < r_dst.height; ++row)
{
const pixel_argb_t * s_line = pixel_at(s_raw_pixbuf, (static_cast<int>(row * rate_y) + r_src.y) * bytes_per_line);
pixel_argb_t * i = pixbuf.raw_ptr(r_dst.y + row);
for(std::size_t x = 0; x < r_dst.width; ++x, ++i)
*i = s_line[static_cast<int>(x * rate_x) + r_src.x];
}
}
}
};
class bilinear_interoplation
: public image_process::stretch_interface
{
struct x_u_table_tag
{
int x;
int iu;
int iu_minus_coef;
};
void process(const paint::pixel_buffer & s_pixbuf, const nana::rectangle& r_src, paint::pixel_buffer & pixbuf, const nana::rectangle& r_dst) const
{
const auto s_bytes_per_line = s_pixbuf.bytes_per_line();
const int shift_size = 8;
const std::size_t coef = 1 << shift_size;
const int double_shift_size = shift_size << 1;
double rate_x = double(r_src.width) / r_dst.width;
double rate_y = double(r_src.height) / r_dst.height;
const int right_bound = static_cast<int>(r_src.width) - 1 + r_src.x;
const nana::pixel_argb_t * s_raw_pixel_buffer = s_pixbuf.raw_ptr(0);
const int bottom = r_src.y + static_cast<int>(r_src.height - 1);
x_u_table_tag * x_u_table = new x_u_table_tag[r_dst.width];
for(std::size_t x = 0; x < r_dst.width; ++x)
{
double u = (int(x) + 0.5) * rate_x - 0.5;
x_u_table_tag el;
el.x = r_src.x;
if(u < 0)
{
u = 0;
}
else
{
int ipart = static_cast<int>(u);
el.x += ipart;
u -= ipart;
}
el.iu = static_cast<int>(u * coef);
el.iu_minus_coef = coef - el.iu;
x_u_table[x] = el;
}
const bool is_alpha_channel = s_pixbuf.alpha_channel();
for(std::size_t row = 0; row < r_dst.height; ++row)
{
double v = (int(row) + 0.5) * rate_y - 0.5;
int sy = r_src.y;
if(v < 0)
{
v = 0;
}
else
{
int ipart = static_cast<int>(v);
sy += ipart;
v -= ipart;
}
std::size_t iv = static_cast<size_t>(v * coef);
const std::size_t iv_minus_coef = coef - iv;
const nana::pixel_argb_t * s_line = pixel_at(s_raw_pixel_buffer, sy * s_bytes_per_line);
const nana::pixel_argb_t * next_s_line = pixel_at(s_line, (sy < bottom ? s_bytes_per_line : 0));
nana::pixel_argb_t col0;
nana::pixel_argb_t col1;
nana::pixel_argb_t col2;
nana::pixel_argb_t col3;
pixel_argb_t * i = pixbuf.raw_ptr(row + r_dst.y) + r_dst.x;
if(is_alpha_channel)
{
for(std::size_t x = 0; x < r_dst.width; ++x, ++i)
{
x_u_table_tag el = x_u_table[x];
col0 = s_line[el.x];
col1 = next_s_line[el.x];
if(el.x < right_bound)
{
col2 = s_line[el.x + 1];
col3 = next_s_line[el.x + 1];
}
else
{
col2 = col0;
col3 = col1;
}
std::size_t coef0 = el.iu_minus_coef * iv_minus_coef;
std::size_t coef1 = el.iu_minus_coef * iv;
std::size_t coef2 = el.iu * iv_minus_coef;
std::size_t coef3 = el.iu * iv;
unsigned alpha_chn = static_cast<unsigned>((coef0 * col0.element.alpha_channel + coef1 * col1.element.alpha_channel + (coef2 * col2.element.alpha_channel + coef3 * col3.element.alpha_channel)) >> double_shift_size);
unsigned s_red = static_cast<unsigned>((coef0 * col0.element.red + coef1 * col1.element.red + (coef2 * col2.element.red + coef3 * col3.element.red)) >> double_shift_size);
unsigned s_green = static_cast<unsigned>((coef0 * col0.element.green + coef1 * col1.element.green + (coef2 * col2.element.green + coef3 * col3.element.green)) >> double_shift_size);
unsigned s_blue = static_cast<unsigned>((coef0 * col0.element.blue + coef1 * col1.element.blue + (coef2 * col2.element.blue + coef3 * col3.element.blue)) >> double_shift_size);
if(alpha_chn)
{
if(alpha_chn != 255)
{
i->element.red = unsigned(i->element.red * (255 - alpha_chn) + s_red * alpha_chn) / 255;
i->element.green = unsigned(i->element.green * (255 - alpha_chn) + s_green * alpha_chn) / 255;
i->element.blue = unsigned(i->element.blue * (255 - alpha_chn) + s_blue * alpha_chn) / 255;
}
else
{
i->element.red = s_red;
i->element.green = s_green;
i->element.blue = s_blue;
}
}
}
}
else
{
for(std::size_t x = 0; x < r_dst.width; ++x, ++i)
{
x_u_table_tag el = x_u_table[x];
col0 = s_line[el.x];
col1 = next_s_line[el.x];
if(el.x < right_bound)
{
col2 = s_line[el.x + 1];
col3 = next_s_line[el.x + 1];
}
else
{
col2 = col0;
col3 = col1;
}
std::size_t coef0 = el.iu_minus_coef * iv_minus_coef;
std::size_t coef1 = el.iu_minus_coef * iv;
std::size_t coef2 = el.iu * iv_minus_coef;
std::size_t coef3 = el.iu * iv;
i->element.red = static_cast<unsigned char>((coef0 * col0.element.red + coef1 * col1.element.red + (coef2 * col2.element.red + coef3 * col3.element.red)) >> double_shift_size);
i->element.green = static_cast<unsigned char>((coef0 * col0.element.green + coef1 * col1.element.green + (coef2 * col2.element.green + coef3 * col3.element.green)) >> double_shift_size);
i->element.blue = static_cast<unsigned char>((coef0 * col0.element.blue + coef1 * col1.element.blue + (coef2 * col2.element.blue + coef3 * col3.element.blue)) >> double_shift_size);
}
}
}
delete [] x_u_table;
}
};
//alpha_blend
class alpha_blend
: public image_process::alpha_blend_interface
{
//process
virtual void process(const paint::pixel_buffer& s_pixbuf, const nana::rectangle& s_r, paint::pixel_buffer& d_pixbuf, const nana::point& d_pos) const
{
auto d_rgb = d_pixbuf.at(d_pos);
auto s_rgb = s_pixbuf.raw_ptr(s_r.y) + s_r.x;
if(d_rgb && s_rgb)
{
const unsigned rest = s_r.width & 0x3;
const unsigned length_align4 = s_r.width - rest;
std::size_t d_step_bytes = d_pixbuf.bytes_per_line() - (s_r.width - rest) * sizeof(pixel_argb_t);
std::size_t s_step_bytes = s_pixbuf.bytes_per_line() - (s_r.width - rest) * sizeof(pixel_argb_t);
for(unsigned line = 0; line < s_r.height; ++line)
{
const auto end = d_rgb + length_align4;
for(; d_rgb < end; d_rgb += 4, s_rgb += 4)
{
//0
if(s_rgb->element.alpha_channel)
{
if(s_rgb->element.alpha_channel != 255)
{
d_rgb->element.red = unsigned(d_rgb->element.red * (255 - s_rgb[0].element.alpha_channel) + s_rgb[0].element.red * s_rgb[0].element.alpha_channel) / 255;
d_rgb->element.green = unsigned(d_rgb->element.green * (255 - s_rgb[0].element.alpha_channel) + s_rgb[0].element.green * s_rgb[0].element.alpha_channel) / 255;
d_rgb->element.blue = unsigned(d_rgb->element.blue * (255 - s_rgb[0].element.alpha_channel) + s_rgb[0].element.blue * s_rgb[0].element.alpha_channel) / 255;
}
else
*d_rgb = *s_rgb;
}
//1
if(s_rgb[1].element.alpha_channel)
{
if(s_rgb[1].element.alpha_channel != 255)
{
d_rgb[1].element.red = unsigned(d_rgb[1].element.red * (255 - s_rgb[1].element.alpha_channel) + s_rgb[1].element.red * s_rgb[1].element.alpha_channel) / 255;
d_rgb[1].element.green = unsigned(d_rgb[1].element.green * (255 - s_rgb[1].element.alpha_channel) + s_rgb[1].element.green * s_rgb[1].element.alpha_channel) / 255;
d_rgb[1].element.blue = unsigned(d_rgb[1].element.blue * (255 - s_rgb[1].element.alpha_channel) + s_rgb[1].element.blue * s_rgb[1].element.alpha_channel) / 255;
}
else
d_rgb[1] = s_rgb[1];
}
//2
if(s_rgb[2].element.alpha_channel)
{
if(s_rgb[2].element.alpha_channel != 255)
{
d_rgb[2].element.red = unsigned(d_rgb[2].element.red * (255 - s_rgb[2].element.alpha_channel) + s_rgb[2].element.red * s_rgb[2].element.alpha_channel) / 255;
d_rgb[2].element.green = unsigned(d_rgb[2].element.green * (255 - s_rgb[2].element.alpha_channel) + s_rgb[2].element.green * s_rgb[2].element.alpha_channel) / 255;
d_rgb[2].element.blue = unsigned(d_rgb[2].element.blue * (255 - s_rgb[2].element.alpha_channel) + s_rgb[2].element.blue * s_rgb[2].element.alpha_channel) / 255;
}
else
d_rgb[2] = s_rgb[2];
}
//3
if(s_rgb[3].element.alpha_channel)
{
if(s_rgb[3].element.alpha_channel != 255)
{
d_rgb[3].element.red = unsigned(d_rgb[3].element.red * (255 - s_rgb[3].element.alpha_channel) + s_rgb[3].element.red * s_rgb[3].element.alpha_channel) / 255;
d_rgb[3].element.green = unsigned(d_rgb[3].element.green * (255 - s_rgb[3].element.alpha_channel) + s_rgb[3].element.green * s_rgb[3].element.alpha_channel) / 255;
d_rgb[3].element.blue = unsigned(d_rgb[3].element.blue * (255 - s_rgb[3].element.alpha_channel) + s_rgb[3].element.blue * s_rgb[3].element.alpha_channel) / 255;
}
else
d_rgb[3] = s_rgb[3];
}
}
const pixel_argb_t * s_end = s_rgb + rest;
auto rest_d_rgb = d_rgb;
for(auto i = s_rgb; i != s_end; ++i)
{
if(i->element.alpha_channel)
{
if(i->element.alpha_channel != 255)
{
rest_d_rgb->element.red = unsigned(rest_d_rgb->element.red * (255 - i->element.alpha_channel) + i->element.red * i->element.alpha_channel) / 255;
rest_d_rgb->element.green = unsigned(rest_d_rgb->element.green * (255 - i->element.alpha_channel) + i->element.green * i->element.alpha_channel) / 255;
rest_d_rgb->element.blue = unsigned(rest_d_rgb->element.blue * (255 - i->element.alpha_channel) + i->element.blue * i->element.alpha_channel) / 255;
}
else
*rest_d_rgb = *i;
}
++rest_d_rgb;
}
d_rgb = pixel_at(d_rgb, d_step_bytes);
s_rgb = pixel_at(s_rgb, s_step_bytes);
}
}
}
};
//blend
class blend
: public image_process::blend_interface
{
//process
virtual void process(const paint::pixel_buffer& s_pixbuf, const nana::rectangle& s_r, paint::pixel_buffer& d_pixbuf, const nana::point& d_pos, double fade_rate) const
{
auto d_rgb = d_pixbuf.raw_ptr(d_pos.y) + d_pos.x;
auto s_rgb = s_pixbuf.raw_ptr(s_r.y) + s_r.x;
if(d_rgb && s_rgb)
{
auto ptr = detail::alloc_fade_table(fade_rate);//new unsigned char[0x100 * 2];
unsigned char* d_table = ptr.get();
unsigned char* s_table = d_table + 0x100;
const unsigned rest = s_r.width & 0x3;
const unsigned length_align4 = s_r.width - rest;
std::size_t d_step_bytes = d_pixbuf.bytes_per_line() - (s_r.width - rest) * sizeof(pixel_argb_t);
std::size_t s_step_bytes = s_pixbuf.bytes_per_line() - (s_r.width - rest) * sizeof(pixel_argb_t);
for(unsigned line = 0; line < s_r.height; ++line)
{
const auto end = d_rgb + length_align4;
for(; d_rgb < end; d_rgb += 4, s_rgb += 4)
{
//0
d_rgb[0].element.red = unsigned(d_table[d_rgb[0].element.red] + s_table[s_rgb[0].element.red]);
d_rgb[0].element.green = unsigned(d_table[d_rgb[0].element.green] + s_table[s_rgb[0].element.green]);
d_rgb[0].element.blue = unsigned(d_table[d_rgb[0].element.blue] + s_table[s_rgb[0].element.blue]);
//1
d_rgb[1].element.red = unsigned(d_table[d_rgb[1].element.red] + s_table[s_rgb[1].element.red]);
d_rgb[1].element.green = unsigned(d_table[d_rgb[1].element.green] + s_table[s_rgb[1].element.green]);
d_rgb[1].element.blue = unsigned(d_table[d_rgb[1].element.blue] + s_table[s_rgb[1].element.blue]);
//2
d_rgb[2].element.red = unsigned(d_table[d_rgb[2].element.red] + s_table[s_rgb[2].element.red]);
d_rgb[2].element.green = unsigned(d_table[d_rgb[2].element.green] + s_table[s_rgb[2].element.green]);
d_rgb[2].element.blue = unsigned(d_table[d_rgb[2].element.blue] + s_table[s_rgb[2].element.blue]);
//3
d_rgb[3].element.red = unsigned(d_table[d_rgb[3].element.red] + s_table[s_rgb[3].element.red]);
d_rgb[3].element.green = unsigned(d_table[d_rgb[3].element.green] + s_table[s_rgb[3].element.green]);
d_rgb[3].element.blue = unsigned(d_table[d_rgb[3].element.blue] + s_table[s_rgb[3].element.blue]);
}
for(unsigned i = 0; i < rest; ++i)
{
d_rgb[i].element.red = unsigned(d_table[d_rgb[i].element.red] + s_table[s_rgb[i].element.red]);
d_rgb[i].element.green = unsigned(d_table[d_rgb[i].element.green] + s_table[s_rgb[i].element.green]);
d_rgb[i].element.blue = unsigned(d_table[d_rgb[i].element.blue] + s_table[s_rgb[i].element.blue]);
}
d_rgb = pixel_at(d_rgb, d_step_bytes);
s_rgb = pixel_at(s_rgb, s_step_bytes);
}
}
}
};
//class line
class bresenham_line
: public image_process::line_interface
{
virtual void process(paint::pixel_buffer & pixbuf, const nana::point& pos_beg, const nana::point& pos_end, const ::nana::color& clr, double fade_rate) const
{
//Return if it is completely transparent
if (fade_rate <= 0)
return;
auto rgb_color = clr.px_color().value;
const std::size_t bytes_pl = pixbuf.bytes_per_line();
unsigned char * fade_table = nullptr;
std::unique_ptr<unsigned char[]> autoptr;
nana::pixel_argb_t rgb_imd = {};
if(fade_rate < 1)
{
autoptr = detail::alloc_fade_table(1.0 - fade_rate);
fade_table = autoptr.get();
rgb_imd.value = rgb_color;
rgb_imd = detail::fade_color_intermedia(rgb_imd, fade_table);
}
auto i = pixel_at(pixbuf.raw_ptr(0), pos_beg.y * bytes_pl) + pos_beg.x;
auto delta = pos_end - pos_beg;
int step_bytes;
if(delta.y < 0)
{
delta.y = -delta.y;
step_bytes = -static_cast<int>(bytes_pl);
}
else
step_bytes = static_cast<int>(bytes_pl);
if(delta.x == delta.y)
{
step_bytes += sizeof(pixel_argb_t);
++delta.x;
if(fade_table)
{
for(int x = 0; x < delta.x; ++x)
{
*i = detail::fade_color_by_intermedia(*i, rgb_imd, fade_table);
i = pixel_at(i, step_bytes);
}
}
else
{
for(int x = 0; x < delta.x; ++x)
{
i->value = rgb_color;
i = pixel_at(i, step_bytes);
}
}
}
else
{
int dx_2 = delta.x << 1;
int dy_2 = delta.y << 1;
if(delta.x > delta.y)
{
int error = dy_2 - delta.x;
++delta.x; //Include the end poing
if(fade_table)
{
for(int x = 0; x < delta.x; ++x)
{
*i = detail::fade_color_by_intermedia(*i, rgb_imd, fade_table);
if(error >= 0)
{
error -= dx_2;
i = pixel_at(i, step_bytes);
}
error += dy_2;
++i;
}
}
else
{
for(int x = 0; x < delta.x; ++x)
{
i->value = rgb_color;
if(error >= 0)
{
error -= dx_2;
i = pixel_at(i, step_bytes);
}
error += dy_2;
++i;
}
}
}
else
{
int error = dx_2 - delta.y;
++delta.y; //Include the end point
if(fade_table)
{
for (int y = 0; y < delta.y; ++y)
{
*i = detail::fade_color_by_intermedia(*i, rgb_imd, fade_table);
if(error >= 0)
{
error -= dy_2;
++i;
}
error += dx_2;
i = pixel_at(i, step_bytes);
}
}
else
{
for (int y = 0; y < delta.y; ++y)
{
i->value = rgb_color;
if(error >= 0)
{
error -= dy_2;
++i;
}
error += dx_2;
i = pixel_at(i, step_bytes);
}
}
}
}
}
};
class superfast_blur
: public image_process::blur_interface
{
void process(pixel_buffer& pixbuf, const nana::rectangle& area, std::size_t u_radius) const
{
int radius = static_cast<int>(u_radius);
int w = area.width;
int h = area.height;
int wm = w - 1;
int hm = h - 1;
int wh = w * h;
int div = (radius << 1) + 1;
int large_edge = (w > h ? w : h);
const int div_256 = div * 256;
std::unique_ptr<int[]> table_rgb(new int[(wh << 1) + wh + (large_edge << 1) + div_256]);
int * tbl_r = table_rgb.get();
int * tbl_g = tbl_r + wh;
int * tbl_b = tbl_g + wh;
int * vmin = tbl_b + wh;
int * vmax = vmin + large_edge;
int * dv = vmax + large_edge;
const int end_div = div - 1;
for(int i = 0, *dv_block = dv; i < 256; ++i)
{
for(int u = 0; u < end_div; u += 2)
{
dv_block[u] = i;
dv_block[u + 1] = i;
}
dv_block[div - 1] = i;
dv_block += div;
}
auto linepix = pixbuf.raw_ptr(area.y) + area.x;
int yi = 0;
for(int y = 0; y < h; ++y)
{
int sum_r = 0, sum_g = 0, sum_b = 0;
if(radius <= wm)
{
for(int i = - radius; i <= radius; ++i)
{
auto px = linepix[(i > 0 ? i : 0)];
sum_r += px.element.red;
sum_g += px.element.green;
sum_b += px.element.blue;
}
}
else
{
for(int i = - radius; i <= radius; ++i)
{
auto px = linepix[std::min(wm, (i > 0 ? i : 0))];
sum_r += px.element.red;
sum_g += px.element.green;
sum_b += px.element.blue;
}
}
for(int x = 0; x < w; ++x)
{
tbl_r[yi] = dv[sum_r];
tbl_g[yi] = dv[sum_g];
tbl_b[yi] = dv[sum_b];
if(0 == y)
{
vmin[x] = std::min(x + radius + 1, wm);
vmax[x] = std::max(x - radius, 0);
}
auto p1 = linepix[vmin[x]];
auto p2 = linepix[vmax[x]];
sum_r += p1.element.red - p2.element.red;
sum_g += p1.element.green - p2.element.green;
sum_b += p1.element.blue - p2.element.blue;
++yi;
}
linepix = pixbuf.raw_ptr(area.y + y) + area.x;
}
const int yp_init = -radius * w;
const std::size_t bytes_pl = pixbuf.bytes_per_line();
for(int x = 0; x < w; ++x)
{
int sum_r = 0, sum_g = 0, sum_b = 0;
int yp = yp_init;
for(int i = -radius; i <= radius; ++i)
{
if(yp < 1)
{
sum_r += tbl_r[x];
sum_g += tbl_g[x];
sum_b += tbl_b[x];
}
else
{
int yi = yp + x;
sum_r += tbl_r[yi];
sum_g += tbl_g[yi];
sum_b += tbl_b[yi];
}
yp += w;
}
linepix = pixbuf.raw_ptr(area.y) + x + area.x;
for(int y = 0; y < h; ++y)
{
linepix->value = 0xFF000000 | (dv[sum_r] << 16) | (dv[sum_g] << 8) | dv[sum_b];
if(x == 0)
{
vmin[y] = std::min(y + radius + 1, hm) * w;
vmax[y] = std::max(y - radius, 0) * w;
}
int pt1 = x + vmin[y];
int pt2 = x + vmax[y];
sum_r += tbl_r[pt1] - tbl_r[pt2];
sum_g += tbl_g[pt1] - tbl_g[pt2];
sum_b += tbl_b[pt1] - tbl_b[pt2];
linepix = pixel_at(linepix, bytes_pl);
}
}
}
};//end class superfast_blur
}
}
}
}
#endif