Skip to content

Commit

Permalink
ADD: Start on alpha support for Plane scaling #41
Browse files Browse the repository at this point in the history
  • Loading branch information
spillerrec committed Sep 16, 2018
1 parent 93d1ce2 commit 03af824
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/planes/ImageEx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ ImageEx ImageEx::toColorSpace( ColorSpace to ) const{
auto img_size = out.getSize();
for( auto& info : out.planes )
if( info.p.getSize() != img_size )
info.p = info.p.scale_cubic( img_size );
info.p = info.p.scale_cubic( alpha_plane(), img_size );

#pragma omp parallel for
for( unsigned iy=0; iy<img_size.height(); iy++ ){
Expand Down
8 changes: 4 additions & 4 deletions src/planes/ImageEx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ class ImageEx{

void scale( Point<unsigned> size, ScalingFunction scaling=ScalingFunction::SCALE_MITCHELL ){
for( auto& info : planes )
info.p = info.p.scale_select( size, scaling );
info.p = info.p.scale_select( alpha_plane(), size, scaling );
if( alpha_plane() )
alpha_plane() = alpha_plane().scale_select( size, scaling );
alpha_plane() = alpha_plane().scale_select( {}, size, scaling );
}
void scaleFactor( Size<double> factor, ScalingFunction scaling=ScalingFunction::SCALE_MITCHELL ){
for( auto& info : planes )
info.p = info.p.scale_select( ( info.p.getSize() * factor ).round(), scaling );
info.p = info.p.scale_select( alpha_plane(), ( info.p.getSize() * factor ).round(), scaling );
if( alpha_plane() )
alpha_plane() = alpha_plane().scale_select( ( alpha_plane().getSize() * factor ).round(), scaling );
alpha_plane() = alpha_plane().scale_select( {}, ( alpha_plane().getSize() * factor ).round(), scaling );
}
Point<unsigned> crop( unsigned left, unsigned top, unsigned right, unsigned bottom );
void crop( Point<unsigned> offset, Size<unsigned> size );
Expand Down
56 changes: 55 additions & 1 deletion src/planes/Plane-scaling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
using namespace std;
using namespace Overmix;

Plane Plane::scale_nearest( Point<unsigned> wanted ) const{
Plane Plane::scale_nearest( const Plane& alpha, Point<unsigned> wanted ) const{
//TODO: Support alpha
Timer t( "scale_nearest" );
Plane scaled( wanted );

Expand Down Expand Up @@ -192,3 +193,56 @@ Plane Plane::scale_generic( Point<unsigned> wanted, double window, Plane::Filter
}


Plane Plane::scale_generic_alpha( const Plane& alpha, Point<unsigned> wanted, double window, Plane::Filter f, Point<double> offset ) const{
//Do non-alpha version if alpha is not valid
if( !alpha.valid() )
return scale_generic( wanted, window, f, offset );
//TODO: check alpha size

Timer t( "scale_generic_alpha" );
if( !*this || wanted == getSize() )
return *this;

Plane scaled( wanted );

//Calculate all x-weights
std::vector<ScalePoint> points;
points.reserve( wanted.width() );
for( unsigned ix=0; ix<wanted.width(); ++ix )
points.emplace_back( ix, size.width(), wanted.width(), offset.x, window, f );

#pragma omp parallel for
for( unsigned iy=0; iy<wanted.height(); ++iy ){
color_type *out = scaled.scan_line( iy ).begin();
ScalePoint ver( iy, get_height(), scaled.get_height(), offset.y, window, f );

for( auto& x : points ){
double avg = 0;
auto row = scan_line( ver.start ).begin() + x.start;
auto row_a = alpha.scan_line( ver.start ).begin() + x.start;

for( auto wy : ver.weights ){
double alpha_avg = 0;
auto row2 = row ;
auto row2_a = row_a;

double local_avg = 0;
for( auto wx : x.weights ){
auto alpha = color::asDouble(*(row2_a++)) * wx;
local_avg += *(row2++) * alpha;
alpha_avg += alpha;
}
avg += local_avg / alpha_avg * wy;
//TODO: How is the total average affected by this

row += get_line_width();
}

*(out++) = color::truncate( avg + 0.5 );
}
}

return scaled;
}


43 changes: 22 additions & 21 deletions src/planes/Plane.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,31 +122,32 @@ class Plane : public PlaneBase<color_type>{
static double lancozs5( double x ){ return lancozs( x, 5 ); }
static double lancozs7( double x ){ return lancozs( x, 7 ); }
Plane scale_generic( Point<unsigned> size, double window, Filter f, Point<double> offset={0.0,0.0} ) const;
Plane scale_generic_alpha( const Plane& alpha, Point<unsigned> size, double window, Filter f, Point<double> offset={0.0,0.0} ) const;
public:
Plane scale_nearest( Point<unsigned> size ) const;
Plane scale_linear( Point<unsigned> size, Point<double> offset={0.0,0.0} ) const{
return scale_generic( size, 1.5, linear, offset );
Plane scale_nearest( const Plane& alpha, Point<unsigned> size ) const;
Plane scale_linear( const Plane& alpha, Point<unsigned> size, Point<double> offset={0.0,0.0} ) const{
return scale_generic_alpha( alpha, size, 1.5, linear, offset );
}
Plane scale_cubic( Point<unsigned> size, Point<double> offset={0.0,0.0} ) const{
return scale_generic( size, 2.5, mitchell, offset );
Plane scale_cubic( const Plane& alpha, Point<unsigned> size, Point<double> offset={0.0,0.0} ) const{
return scale_generic_alpha( alpha, size, 2.5, mitchell, offset );
}
Plane scale_lanczos3( Point<unsigned> size, Point<double> offset={0.0,0.0} ) const
{ return scale_generic( size, 3.5, lancozs3, offset ); }
Plane scale_lanczos5( Point<unsigned> size, Point<double> offset={0.0,0.0} ) const
{ return scale_generic( size, 5.5, lancozs5, offset ); }
Plane scale_lanczos7( Point<unsigned> size, Point<double> offset={0.0,0.0} ) const
{ return scale_generic( size, 7.5, lancozs7, offset ); }

Plane scale_select( Point<unsigned> size, ScalingFunction scaling, Point<double> offset={0.0,0.0} ) const{
Plane scale_lanczos3( const Plane& alpha, Point<unsigned> size, Point<double> offset={0.0,0.0} ) const
{ return scale_generic_alpha( alpha, size, 3.5, lancozs3, offset ); }
Plane scale_lanczos5( const Plane& alpha, Point<unsigned> size, Point<double> offset={0.0,0.0} ) const
{ return scale_generic_alpha( alpha, size, 5.5, lancozs5, offset ); }
Plane scale_lanczos7( const Plane& alpha, Point<unsigned> size, Point<double> offset={0.0,0.0} ) const
{ return scale_generic_alpha( alpha, size, 7.5, lancozs7, offset ); }

Plane scale_select( const Plane& alpha, Point<unsigned> size, ScalingFunction scaling, Point<double> offset={0.0,0.0} ) const{
switch( scaling ){
case ScalingFunction::SCALE_NEAREST : return scale_nearest( size );
case ScalingFunction::SCALE_LINEAR : return scale_linear( size, offset );
case ScalingFunction::SCALE_MITCHELL: return scale_cubic( size, offset );
case ScalingFunction::SCALE_CATROM : return scale_generic( size, 2.5, catrom, offset );
case ScalingFunction::SCALE_SPLINE : return scale_generic( size, 2.5, spline, offset );
case ScalingFunction::SCALE_LANCZOS_3 : return scale_lanczos3( size, offset );
case ScalingFunction::SCALE_LANCZOS_5 : return scale_lanczos5( size, offset );
case ScalingFunction::SCALE_LANCZOS_7 : return scale_lanczos7( size, offset );
case ScalingFunction::SCALE_NEAREST : return scale_nearest( alpha, size );
case ScalingFunction::SCALE_LINEAR : return scale_linear( alpha, size, offset );
case ScalingFunction::SCALE_MITCHELL: return scale_cubic( alpha, size, offset );
case ScalingFunction::SCALE_CATROM : return scale_generic_alpha( alpha, size, 2.5, catrom, offset );
case ScalingFunction::SCALE_SPLINE : return scale_generic_alpha( alpha, size, 2.5, spline, offset );
case ScalingFunction::SCALE_LANCZOS_3 : return scale_lanczos3( alpha, size, offset );
case ScalingFunction::SCALE_LANCZOS_5 : return scale_lanczos5( alpha, size, offset );
case ScalingFunction::SCALE_LANCZOS_7 : return scale_lanczos7( alpha, size, offset );
default: return Plane();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/renders/AnimRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ AnimRender::AnimRender( const AContainer& aligner, ARender& render, AProcessWatc
auto full_size = img.getSize();
for( unsigned i=0; i<img.size(); i++ )
if( img[i].getSize() != full_size )
img[i] = img[i].scale_cubic( full_size );
img[i] = img[i].scale_cubic( img.alpha_plane(), full_size );

frames.addImage( modify( img, size, pos, expand ) );
old.emplace_back( pos, img.getSize() );
Expand Down
3 changes: 2 additions & 1 deletion src/renders/AverageRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ ImageEx AverageRender::render( const AContainer& aligner, AProcessWatcher* watch
ProgressWrapper( watcher ).add();
}

img.addPlane( sum.average() );
auto scaled_plane = sum.average().scale_select( sum.alpha(), full, ScalingFunction::SCALE_MITCHELL ); //TODO: make adjustable
img.addPlane( std::move( scaled_plane ) );

if( c == 0 && use_plane_alpha )
img.alpha_plane() = sum.alpha();
Expand Down
6 changes: 4 additions & 2 deletions src/renders/EstimatorRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ Plane EstimatorRender::degrade( const Plane& original, const Parameters& para )
out = out.blur_gaussian( bluring*upscale_factor.x, bluring*upscale_factor.y );

//Degrade - resolution
out = out.scale_select( out.getSize() / upscale_factor, scale_method/*, pos - pos.to<int>().to<double>()*/ ); //TODO: offset
out = out.scale_select( Plane(), out.getSize() / upscale_factor, scale_method/*, pos - pos.to<int>().to<double>()*/ ); //TODO: offset
//TODO: Alpha


return out;
Expand All @@ -90,7 +91,8 @@ void sign( Plane& out, const Plane& p1, const Plane& p2, Point<double> offset, d
val.first = signFloat( val.first, val.second, beta );

//Upscale delta to fit
delta = delta.scale_select( delta.getSize()*scale, ScalingFunction::SCALE_MITCHELL );
delta = delta.scale_select( Plane(), delta.getSize()*scale, ScalingFunction::SCALE_MITCHELL );
//TODO: Alpha

for( unsigned iy=0; iy<delta.get_height(); iy++ ){
auto row_out = out .scan_line( iy+pos.y );
Expand Down
2 changes: 1 addition & 1 deletion src/renders/PixelatorRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ ImageEx PixelatorRender::render( const AContainer& aligner, AProcessWatcher* wat
//Get edge detected image
ImageEx gray( avg );
gray.to_grayscale();
auto edges = gray[0].edge_sobel().scale_cubic( gray[0].getSize()*upscale );
auto edges = gray[0].edge_sobel().scale_cubic( gray.alpha_plane(), gray[0].getSize()*upscale );
edges.binarize_threshold( edges.mean_value() );

Histogram histo_h( edges.get_width() , color::BLACK );
Expand Down
2 changes: 1 addition & 1 deletion src/renders/RobustSrRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ ImageEx RobustSrRender::render(const AContainer &group, AProcessWatcher *watcher

auto est = AverageRender().render(group); //Starting estimate
for( unsigned c=0; c<planes_amount; ++c ){
auto output = imageToMatrix( est[c].scale_cubic( group.image(0).getSize()*upscale_factor ) ); //TODO: support real upscale
auto output = imageToMatrix( est[c].scale_cubic( est.alpha_plane(), group.image(0).getSize()*upscale_factor ) ); //TODO: support real upscale
qDebug() << "Output size: " << output.size();
vector<MatrixImg> lowres;
for( auto g : group )
Expand Down
2 changes: 1 addition & 1 deletion src/utils/PlaneUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ using ModifiedPlane = Modified<Plane>;

inline ModifiedPlane getScaled( const Plane& p, Size<unsigned> size ){
if( p && p.getSize() != size )
return { p.scale_cubic( size ) };
return { p.scale_cubic( Plane(), size ) }; //TODO: alpha?
else
return { p };
}
Expand Down

0 comments on commit 03af824

Please sign in to comment.