Skip to content

Commit

Permalink
Reduce code duplication in scaling IPFs
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Mar 21, 2016
1 parent 00fa1b4 commit ca7fded
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 128 deletions.
107 changes: 20 additions & 87 deletions src/image_modifications.cpp
Expand Up @@ -362,25 +362,13 @@ const surface& light_modification::get_surface() const

surface scale_modification::operator()(const surface& src) const
{
const int old_w = src->w;
const int old_h = src->h;
int w = w_;
int h = h_;
std::pair<int,int> sz = calculate_size(src);

if(w <= 0) {
if(w < 0) {
ERR_DP << "width of SCALE is negative - resetting to original width" << std::endl;
}
w = old_w;
}
if(h <= 0) {
if(h < 0) {
ERR_DP << "height of SCALE is negative - resetting to original height" << std::endl;
}
h = old_h;
if(nn_) {
return scale_surface_sharp(src, sz.first, sz.second);
} else {
return scale_surface(src, sz.first, sz.second);
}

return scale_surface(src, w, h);
}

int scale_modification::get_w() const
Expand All @@ -393,45 +381,35 @@ int scale_modification::get_h() const
return h_;
}

surface scale_sharp_modification::operator()(const surface& src) const
std::pair<int,int> scale_exact_modification::calculate_size(const surface& src) const
{
const int old_w = src->w;
const int old_h = src->h;
int w = w_;
int h = h_;
int w = get_w();
int h = get_h();

if(w <= 0) {
if(w < 0) {
ERR_DP << "width of SCALE_SHARP is negative - resetting to original width" << std::endl;
ERR_DP << "width of " << fn_ << " is negative - resetting to original width" << std::endl;
}
w = old_w;
}
if(h <= 0) {
if(h < 0) {
ERR_DP << "height of SCALE_SHARP is negative - resetting to original height" << std::endl;
ERR_DP << "height of " << fn_ << " is negative - resetting to original height" << std::endl;
}
h = old_h;
}

return scale_surface_sharp(src, w, h);
}

int scale_sharp_modification::get_w() const
{
return w_;
}

int scale_sharp_modification::get_h() const
{
return h_;

return std::make_pair(w, h);
}

surface scale_into_modification::operator()(const surface& src) const
std::pair<int,int> scale_into_modification::calculate_size(const surface& src) const
{
const int old_w = src->w;
const int old_h = src->h;
long double w = w_;
long double h = h_;
long double w = get_w();
long double h = get_h();

if(w <= 0) {
if(w < 0) {
Expand All @@ -448,52 +426,7 @@ surface scale_into_modification::operator()(const surface& src) const

long double ratio = std::min(w / old_w, h / old_h);

return scale_surface(src, old_w * ratio, old_h * ratio);
}

int scale_into_modification::get_w() const
{
return w_;
}

int scale_into_modification::get_h() const
{
return h_;
}

surface scale_into_sharp_modification::operator()(const surface& src) const
{
const int old_w = src->w;
const int old_h = src->h;
long double w = w_;
long double h = h_;

if(w <= 0) {
if(w < 0) {
ERR_DP << "width of SCALE_INTO_SHARP is negative - resetting to original width" << std::endl;
}
w = old_w;
}
if(h <= 0) {
if(h < 0) {
ERR_DP << "height of SCALE_INTO_SHARP is negative - resetting to original height" << std::endl;
}
h = old_h;
}

long double ratio = std::min(w / old_w, h / old_h);

return scale_surface_sharp(src, old_w * ratio, old_h * ratio);
}

int scale_into_sharp_modification::get_w() const
{
return w_;
}

int scale_into_sharp_modification::get_h() const
{
return h_;
return std::make_pair(old_w * ratio, old_h * ratio);
}

surface xbrz_modification::operator()(const surface& src) const
Expand Down Expand Up @@ -1126,7 +1059,7 @@ REGISTER_MOD_PARSER(SCALE, args)
h = lexical_cast_default<int, const std::string&>(scale_params[1]);
}

return new scale_modification(w, h);
return new scale_exact_modification(w, h, "SCALE", false);
}

REGISTER_MOD_PARSER(SCALE_SHARP, args)
Expand All @@ -1147,7 +1080,7 @@ REGISTER_MOD_PARSER(SCALE_SHARP, args)
h = lexical_cast_default<int, const std::string&>(scale_params[1]);
}

return new scale_sharp_modification(w, h);
return new scale_exact_modification(w, h, "SCALE_SHARP", true);
}

REGISTER_MOD_PARSER(SCALE_INTO, args)
Expand All @@ -1168,7 +1101,7 @@ REGISTER_MOD_PARSER(SCALE_INTO, args)
h = lexical_cast_default<int, const std::string&>(scale_params[1]);
}

return new scale_into_modification(w, h);
return new scale_into_modification(w, h, "SCALE_INTO", false);
}

REGISTER_MOD_PARSER(SCALE_INTO_SHARP, args)
Expand All @@ -1189,7 +1122,7 @@ REGISTER_MOD_PARSER(SCALE_INTO_SHARP, args)
h = lexical_cast_default<int, const std::string&>(scale_params[1]);
}

return new scale_into_sharp_modification(w, h);
return new scale_into_modification(w, h, "SCALE_INTO_SHARP", true);
}

// xBRZ
Expand Down
58 changes: 17 additions & 41 deletions src/image_modifications.hpp
Expand Up @@ -368,73 +368,49 @@ class light_modification : public modification
};

/**
* Scale (SCALE) modification. (Uses bilinear interpolation.)
* Scaling modifications base class.
*/
class scale_modification : public modification
{
public:
scale_modification(int width, int height)
: w_(width), h_(height)
scale_modification(int width, int height, std::string fn, bool use_nn)
: w_(width), h_(height), nn_(use_nn), fn_(fn)
{}
virtual surface operator()(const surface& src) const;
virtual std::pair<int,int> calculate_size(const surface& src) const = 0;
int get_w() const;
int get_h() const;

private:
int w_, h_;
bool nn_;
protected:
const std::string fn_;
};

/**
* Scale sharp (SCALE_SHARP) modification. (Uses nearest neighbor.)
* Scale exact modification. (SCALE, SCALE_SHARP)
*/
class scale_sharp_modification : public modification
class scale_exact_modification : public scale_modification
{
public:
scale_sharp_modification(int width, int height)
: w_(width), h_(height)
scale_exact_modification(int width, int height, std::string fn, bool use_nn)
: scale_modification(width, height, fn, use_nn)
{}
virtual surface operator()(const surface& src) const;
int get_w() const;
int get_h() const;

private:
int w_, h_;
};

/**
* Scale into (SCALE_INTO) modification. (Uses bilinear interpolation.)
* Preserves aspect ratio.
*/
class scale_into_modification : public modification
{
public:
scale_into_modification(int width, int height)
: w_(width), h_(height)
{}
virtual surface operator()(const surface& src) const;
int get_w() const;
int get_h() const;

private:
int w_, h_;
virtual std::pair<int,int> calculate_size(const surface& src) const;
};

/**
* Scale into (SCALE_INTO_SHARP) modification. (Uses nearest neighbor.)
* Scale into (SCALE_INTO) modification. (SCALE_INTO, SCALE_INTO_SHARP)
* Preserves aspect ratio.
*/
class scale_into_sharp_modification : public modification
class scale_into_modification : public scale_modification
{
public:
scale_into_sharp_modification(int width, int height)
: w_(width), h_(height)
scale_into_modification(int width, int height, std::string fn, bool use_nn)
: scale_modification(width, height, fn, use_nn)
{}
virtual surface operator()(const surface& src) const;
int get_w() const;
int get_h() const;

private:
int w_, h_;
virtual std::pair<int,int> calculate_size(const surface& src) const;
};

/**
Expand Down

0 comments on commit ca7fded

Please sign in to comment.