Skip to content

Commit

Permalink
Merge pull request #162 from HappySeaFox/feature/cpp-more-ctors-and-l…
Browse files Browse the repository at this point in the history
…ess-with

Feature/cpp more ctors and less with
  • Loading branch information
HappySeaFox authored Apr 6, 2022
2 parents cce6763 + c1e8e00 commit 5e0391e
Show file tree
Hide file tree
Showing 32 changed files with 586 additions and 414 deletions.
7 changes: 4 additions & 3 deletions examples/c++/qt/advanced-with-c++-api/qtsail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ sail_status_t QtSail::loadImage(const QString &path, QVector<QImage> *qimages, Q

// Mutate alpha into a green color.
//
sail::conversion_options options;
options.with_options(SAIL_CONVERSION_OPTION_BLEND_ALPHA)
.with_background(sail_rgb24_t{ 0, 255, 0 });
const sail::conversion_options options{
SAIL_CONVERSION_OPTION_BLEND_ALPHA,
sail_rgb24_t{ 0, 255, 0 }
};

if (!first_image.is_valid()) {
first_image = image;
Expand Down
80 changes: 16 additions & 64 deletions src/bindings/c++/codec_info-c++.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ codec_info& codec_info::operator=(const codec_info &ci)
{
d->sail_codec_info_c = ci.d->sail_codec_info_c;

with_version(ci.version())
.with_name(ci.name())
.with_description(ci.description())
.with_magic_numbers(ci.magic_numbers())
.with_extensions(ci.extensions())
.with_mime_types(ci.mime_types())
.with_read_features(ci.read_features())
.with_write_features(ci.write_features());
d->version = ci.version();
d->name = ci.name();
d->description = ci.description();
d->magic_numbers = ci.magic_numbers();
d->extensions = ci.extensions();
d->mime_types = ci.mime_types();
d->read_features = ci.read_features();
d->write_features = ci.write_features();

return *this;
}
Expand Down Expand Up @@ -242,62 +242,14 @@ codec_info::codec_info(const sail_codec_info *ci)
mime_types.push_back(mime_type_node->string);
}

with_version(ci->version)
.with_name(ci->name)
.with_description(ci->description)
.with_magic_numbers(magic_numbers)
.with_extensions(extensions)
.with_mime_types(mime_types)
.with_read_features(sail::read_features(ci->read_features))
.with_write_features(sail::write_features(ci->write_features));
}

codec_info& codec_info::with_version(const std::string &version)
{
d->version = version;
return *this;
}

codec_info& codec_info::with_name(const std::string &name)
{
d->name = name;
return *this;
}

codec_info& codec_info::with_description(const std::string &description)
{
d->description = description;
return *this;
}

codec_info& codec_info::with_magic_numbers(const std::vector<std::string> &magic_numbers)
{
d->magic_numbers = magic_numbers;
return *this;
}

codec_info& codec_info::with_extensions(const std::vector<std::string> &extensions)
{
d->extensions = extensions;
return *this;
}

codec_info& codec_info::with_mime_types(const std::vector<std::string> &mime_types)
{
d->mime_types = mime_types;
return *this;
}

codec_info& codec_info::with_read_features(const sail::read_features &read_features)
{
d->read_features = read_features;
return *this;
}

codec_info& codec_info::with_write_features(const sail::write_features &write_features)
{
d->write_features = write_features;
return *this;
d->version = ci->version;
d->name = ci->name;
d->description = ci->description;
d->magic_numbers = magic_numbers;
d->extensions = extensions;
d->mime_types = mime_types;
d->read_features = sail::read_features(ci->read_features);
d->write_features = sail::write_features(ci->write_features);
}

const sail_codec_info* codec_info::sail_codec_info_c() const
Expand Down
9 changes: 0 additions & 9 deletions src/bindings/c++/codec_info-c++.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,6 @@ class SAIL_EXPORT codec_info
*/
explicit codec_info(const sail_codec_info *ci);

codec_info& with_version(const std::string &version);
codec_info& with_name(const std::string &name);
codec_info& with_description(const std::string &description);
codec_info& with_magic_numbers(const std::vector<std::string> &magic_numbers);
codec_info& with_extensions(const std::vector<std::string> &extensions);
codec_info& with_mime_types(const std::vector<std::string> &mime_types);
codec_info& with_read_features(const sail::read_features &read_features);
codec_info& with_write_features(const sail::write_features &write_features);

const sail_codec_info* sail_codec_info_c() const;

private:
Expand Down
32 changes: 20 additions & 12 deletions src/bindings/c++/conversion_options-c++.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ conversion_options::conversion_options()
{
}

conversion_options::conversion_options(int options, const sail_rgb48_t &rgb48)
: conversion_options()
{
set_options(options);
set_background(rgb48);
}

conversion_options::conversion_options(int options, const sail_rgb24_t &rgb24)
: conversion_options()
{
set_options(options);
set_background(rgb24);
}

conversion_options::conversion_options(const conversion_options &co)
: conversion_options()
{
Expand All @@ -62,9 +76,9 @@ conversion_options::conversion_options(const conversion_options &co)

conversion_options& conversion_options::operator=(const conversion_options &co)
{
with_options(co.options())
.with_background(co.background48())
.with_background(co.background24());
set_options(co.options());
set_background(co.background48());
set_background(co.background24());

return *this;
}
Expand Down Expand Up @@ -100,14 +114,12 @@ sail_rgb24_t conversion_options::background24() const
return d->conversion_options->background24;
}

conversion_options& conversion_options::with_options(int options)
void conversion_options::set_options(int options)
{
d->conversion_options->options = options;

return *this;
}

conversion_options& conversion_options::with_background(const sail_rgb48_t &rgb48)
void conversion_options::set_background(const sail_rgb48_t &rgb48)
{
d->conversion_options->background48 = rgb48;

Expand All @@ -116,11 +128,9 @@ conversion_options& conversion_options::with_background(const sail_rgb48_t &rgb4
static_cast<std::uint8_t>(rgb48.component2 / 257),
static_cast<std::uint8_t>(rgb48.component3 / 257)
};

return *this;
}

conversion_options& conversion_options::with_background(const sail_rgb24_t &rgb24)
void conversion_options::set_background(const sail_rgb24_t &rgb24)
{
d->conversion_options->background24 = rgb24;

Expand All @@ -129,8 +139,6 @@ conversion_options& conversion_options::with_background(const sail_rgb24_t &rgb2
static_cast<std::uint16_t>(rgb24.component2 * 257),
static_cast<std::uint16_t>(rgb24.component3 * 257)
};

return *this;
}

sail_status_t conversion_options::to_sail_conversion_options(sail_conversion_options **conversion_options) const
Expand Down
22 changes: 19 additions & 3 deletions src/bindings/c++/conversion_options-c++.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ class SAIL_EXPORT conversion_options
*/
conversion_options();

/*
* Constructs a new conversion options object out of the or-ed SailConversionOption-s
* and the 48-bit color to blend 48-bit images.
* If the options argument is zero, SAIL_CONVERSION_OPTION_DROP_ALPHA is assumed.
* Additionally, calculates and sets a new 24-bit background color to blend 24-bit images.
*/
conversion_options(int options, const sail_rgb48_t &rgb48);

/*
* Constructs a new conversion options object out of the or-ed SailConversionOption-s
* and the 24-bit color to blend 24-bit images.
* If the options argument is zero, SAIL_CONVERSION_OPTION_DROP_ALPHA is assumed.
* Additionally, calculates and sets a new 48-bit background color to blend 48-bit images.
*/
conversion_options(int options, const sail_rgb24_t &rgb24);

/*
* Copies the conversion options object.
*/
Expand Down Expand Up @@ -99,19 +115,19 @@ class SAIL_EXPORT conversion_options
/*
* Sets new or-ed SailConversionOption-s. If zero, SAIL_CONVERSION_OPTION_DROP_ALPHA is assumed.
*/
conversion_options& with_options(int options);
void set_options(int options);

/*
* Sets a new 48-bit background color to blend 48-bit images.
* Additionally, calculates and sets a new 24-bit background color to blend 24-bit images.
*/
conversion_options& with_background(const sail_rgb48_t &rgb48);
void set_background(const sail_rgb48_t &rgb48);

/*
* Sets a new 24-bit background color to blend 24-bit images.
* Additionally, calculates and sets a new 48-bit background color to blend 48-bit images.
*/
conversion_options& with_background(const sail_rgb24_t &rgb24);
void set_background(const sail_rgb24_t &rgb24);

private:
sail_status_t to_sail_conversion_options(sail_conversion_options **conversion_options) const;
Expand Down
35 changes: 12 additions & 23 deletions src/bindings/c++/iccp-c++.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ iccp::iccp()
iccp::iccp(const void *data, unsigned data_length)
: iccp()
{
with_data(data, data_length);
set_data(data, data_length);
}

iccp::iccp(const arbitrary_data &data)
: iccp()
{
with_data(data);
set_data(data);
}

iccp::iccp(const sail::iccp &ic)
Expand All @@ -74,7 +74,7 @@ iccp& iccp::operator=(const sail::iccp &iccp)
d->reset();

if (iccp.is_valid()) {
with_data(iccp.data());
set_data(iccp.data());
}

return *this;
Expand Down Expand Up @@ -106,21 +106,16 @@ const arbitrary_data& iccp::data() const
return d->data;
}

iccp& iccp::with_data(const void *data, unsigned data_length)
void iccp::set_data(const void *data, unsigned data_length)
{
d->reset();

SAIL_TRY_OR_EXECUTE(copy(data, data_length),
/* on error */ return *this);

return *this;
copy(data, data_length);
}

iccp& iccp::with_data(const arbitrary_data &data)
void iccp::set_data(const arbitrary_data &data)
{
with_data(data.data(), static_cast<unsigned>(data.size()));

return *this;
set_data(data.data(), static_cast<unsigned>(data.size()));
}

iccp::iccp(const sail_iccp *ic)
Expand All @@ -131,7 +126,7 @@ iccp::iccp(const sail_iccp *ic)
return;
}

with_data(ic->data, ic->data_length);
set_data(ic->data, ic->data_length);
}

sail_status_t iccp::to_sail_iccp(sail_iccp **iccp) const
Expand All @@ -146,19 +141,13 @@ sail_status_t iccp::to_sail_iccp(sail_iccp **iccp) const
return SAIL_OK;
}

sail_status_t iccp::copy(const void *data, unsigned data_length)
void iccp::copy(const void *data, unsigned data_length)
{
SAIL_CHECK_PTR(data);

if (data_length == 0) {
SAIL_LOG_ERROR("ICCP data length is 0");
SAIL_LOG_AND_RETURN(SAIL_ERROR_INVALID_ARGUMENT);
}

d->data.resize(data_length);
memcpy(d->data.data(), data, data_length);

return SAIL_OK;
if (data_length > 0) {
memcpy(d->data.data(), data, data_length);
}
}

}
6 changes: 3 additions & 3 deletions src/bindings/c++/iccp-c++.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ class SAIL_EXPORT iccp
/*
* Sets new ICC profile binary data. The data is deep copied.
*/
iccp& with_data(const void *data, unsigned data_length);
void set_data(const void *data, unsigned data_length);

/*
* Sets new ICC profile binary data.
*/
iccp& with_data(const arbitrary_data &data);
void set_data(const arbitrary_data &data);

private:
/*
Expand All @@ -121,7 +121,7 @@ class SAIL_EXPORT iccp

sail_status_t to_sail_iccp(sail_iccp **iccp) const;

sail_status_t copy(const void *data, unsigned data_length);
void copy(const void *data, unsigned data_length);

private:
class pimpl;
Expand Down
Loading

0 comments on commit 5e0391e

Please sign in to comment.