Skip to content

Commit

Permalink
fix crashes on RGBA images with CPU mode, resolve nihui#33
Browse files Browse the repository at this point in the history
Signed-off-by: ArchieMeng <archiemeng@protonmail.com>
  • Loading branch information
ArchieMeng committed Sep 16, 2022
1 parent 395302c commit 02c9c33
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/realcugan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ int RealCUGAN::process_cpu(const ncnn::Mat& inimage, ncnn::Mat& outimage) const
int in_tile_x1 = std::min((xi + 1) * TILE_SIZE_X + prepadding_right, w);

// crop tile
ncnn::Mat in;
ncnn::Mat in, in_nopad;
{
if (channels == 3)
{
Expand All @@ -817,8 +817,10 @@ int RealCUGAN::process_cpu(const ncnn::Mat& inimage, ncnn::Mat& outimage) const
if (channels == 4)
{
#if _WIN32
in_nopad = ncnn::Mat::from_pixels_roi(pixeldata, ncnn::Mat::PIXEL_BGRA2RGBA, w, h, xi * TILE_SIZE_X, yi * TILE_SIZE_Y, tile_w_nopad, tile_h_nopad);
in = ncnn::Mat::from_pixels_roi(pixeldata, ncnn::Mat::PIXEL_BGRA2RGBA, w, h, in_tile_x0, in_tile_y0, in_tile_x1 - in_tile_x0, in_tile_y1 - in_tile_y0);
#else
in_nopad = ncnn::Mat::from_pixels_roi(pixeldata, ncnn::Mat::PIXEL_RGBA, w, h, xi * TILE_SIZE_X, yi * TILE_SIZE_Y, tile_w_nopad, tile_h_nopad);
in = ncnn::Mat::from_pixels_roi(pixeldata, ncnn::Mat::PIXEL_RGBA, w, h, in_tile_x0, in_tile_y0, in_tile_x1 - in_tile_x0, in_tile_y1 - in_tile_y0);
#endif
}
Expand Down Expand Up @@ -849,7 +851,7 @@ int RealCUGAN::process_cpu(const ncnn::Mat& inimage, ncnn::Mat& outimage) const

if (channels == 4)
{
in_alpha_tile = in.channel_range(3, 1).clone();
in_alpha_tile = in_nopad.channel_range(3, 1).clone();
}
}

Expand Down Expand Up @@ -1047,7 +1049,7 @@ int RealCUGAN::process_cpu(const ncnn::Mat& inimage, ncnn::Mat& outimage) const

if (channels == 4)
{
in_alpha_tile = in.channel_range(3, 1).clone();
in_alpha_tile = in_nopad.channel_range(3, 1).clone();
}
}

Expand Down Expand Up @@ -2741,7 +2743,7 @@ int RealCUGAN::process_cpu_se_stage0(const ncnn::Mat& inimage, const std::vector
int in_tile_x1 = std::min((xi + 1) * TILE_SIZE_X + prepadding_right, w);

// crop tile
ncnn::Mat in;
ncnn::Mat in, in_nopad;
{
if (channels == 3)
{
Expand All @@ -2754,8 +2756,10 @@ int RealCUGAN::process_cpu_se_stage0(const ncnn::Mat& inimage, const std::vector
if (channels == 4)
{
#if _WIN32
in_nopad = ncnn::Mat::from_pixels_roi(pixeldata, ncnn::Mat::PIXEL_BGRA2RGBA, w, h, xi * TILE_SIZE_X, yi * TILE_SIZE_Y, tile_w_nopad, tile_h_nopad);
in = ncnn::Mat::from_pixels_roi(pixeldata, ncnn::Mat::PIXEL_BGRA2RGBA, w, h, in_tile_x0, in_tile_y0, in_tile_x1 - in_tile_x0, in_tile_y1 - in_tile_y0);
#else
in_nopad = ncnn::Mat::from_pixels_roi(pixeldata, ncnn::Mat::PIXEL_RGBA, w, h, xi * TILE_SIZE_X, yi * TILE_SIZE_Y, tile_w_nopad, tile_h_nopad);
in = ncnn::Mat::from_pixels_roi(pixeldata, ncnn::Mat::PIXEL_RGBA, w, h, in_tile_x0, in_tile_y0, in_tile_x1 - in_tile_x0, in_tile_y1 - in_tile_y0);
#endif
}
Expand Down Expand Up @@ -2786,7 +2790,7 @@ int RealCUGAN::process_cpu_se_stage0(const ncnn::Mat& inimage, const std::vector

if (channels == 4)
{
in_alpha_tile = in.channel_range(3, 1).clone();
in_alpha_tile = in_nopad.channel_range(3, 1).clone();
}
}

Expand Down Expand Up @@ -2896,7 +2900,7 @@ int RealCUGAN::process_cpu_se_stage0(const ncnn::Mat& inimage, const std::vector

if (channels == 4)
{
in_alpha_tile = in.channel_range(3, 1).clone();
in_alpha_tile = in_nopad.channel_range(3, 1).clone();
}
}

Expand Down Expand Up @@ -2991,7 +2995,7 @@ int RealCUGAN::process_cpu_se_stage2(const ncnn::Mat& inimage, const std::vector
int in_tile_x1 = std::min((xi + 1) * TILE_SIZE_X + prepadding_right, w);

// crop tile
ncnn::Mat in;
ncnn::Mat in, in_nopad;
{
if (channels == 3)
{
Expand All @@ -3004,8 +3008,10 @@ int RealCUGAN::process_cpu_se_stage2(const ncnn::Mat& inimage, const std::vector
if (channels == 4)
{
#if _WIN32
in_nopad = ncnn::Mat::from_pixels_roi(pixeldata, ncnn::Mat::PIXEL_BGRA2RGBA, w, h, xi * TILE_SIZE_X, yi * TILE_SIZE_Y, tile_w_nopad, tile_h_nopad);
in = ncnn::Mat::from_pixels_roi(pixeldata, ncnn::Mat::PIXEL_BGRA2RGBA, w, h, in_tile_x0, in_tile_y0, in_tile_x1 - in_tile_x0, in_tile_y1 - in_tile_y0);
#else
in_nopad = ncnn::Mat::from_pixels_roi(pixeldata, ncnn::Mat::PIXEL_RGBA, w, h, xi * TILE_SIZE_X, yi * TILE_SIZE_Y, tile_w_nopad, tile_h_nopad);
in = ncnn::Mat::from_pixels_roi(pixeldata, ncnn::Mat::PIXEL_RGBA, w, h, in_tile_x0, in_tile_y0, in_tile_x1 - in_tile_x0, in_tile_y1 - in_tile_y0);
#endif
}
Expand Down Expand Up @@ -3036,7 +3042,7 @@ int RealCUGAN::process_cpu_se_stage2(const ncnn::Mat& inimage, const std::vector

if (channels == 4)
{
in_alpha_tile = in.channel_range(3, 1).clone();
in_alpha_tile = in_nopad.channel_range(3, 1).clone();
}
}

Expand Down Expand Up @@ -3242,7 +3248,7 @@ int RealCUGAN::process_cpu_se_stage2(const ncnn::Mat& inimage, const std::vector

if (channels == 4)
{
in_alpha_tile = in.channel_range(3, 1).clone();
in_alpha_tile = in_nopad.channel_range(3, 1).clone();
}
}

Expand Down Expand Up @@ -3523,7 +3529,7 @@ int RealCUGAN::process_cpu_se_very_rough_stage0(const ncnn::Mat& inimage, const
int in_tile_x1 = std::min((xi + 1) * TILE_SIZE_X + prepadding_right, w);

// crop tile
ncnn::Mat in;
ncnn::Mat in, in_nopad;
{
if (channels == 3)
{
Expand All @@ -3536,8 +3542,10 @@ int RealCUGAN::process_cpu_se_very_rough_stage0(const ncnn::Mat& inimage, const
if (channels == 4)
{
#if _WIN32
in_nopad = ncnn::Mat::from_pixels_roi(pixeldata, ncnn::Mat::PIXEL_BGRA2RGBA, w, h, xi * TILE_SIZE_X, yi * TILE_SIZE_Y, tile_w_nopad, tile_h_nopad);
in = ncnn::Mat::from_pixels_roi(pixeldata, ncnn::Mat::PIXEL_BGRA2RGBA, w, h, in_tile_x0, in_tile_y0, in_tile_x1 - in_tile_x0, in_tile_y1 - in_tile_y0);
#else
in_nopad = ncnn::Mat::from_pixels_roi(pixeldata, ncnn::Mat::PIXEL_RGBA, w, h, xi * TILE_SIZE_X, yi * TILE_SIZE_Y, tile_w_nopad, tile_h_nopad);
in = ncnn::Mat::from_pixels_roi(pixeldata, ncnn::Mat::PIXEL_RGBA, w, h, in_tile_x0, in_tile_y0, in_tile_x1 - in_tile_x0, in_tile_y1 - in_tile_y0);
#endif
}
Expand Down Expand Up @@ -3568,7 +3576,7 @@ int RealCUGAN::process_cpu_se_very_rough_stage0(const ncnn::Mat& inimage, const

if (channels == 4)
{
in_alpha_tile = in.channel_range(3, 1).clone();
in_alpha_tile = in_nopad.channel_range(3, 1).clone();
}
}

Expand Down Expand Up @@ -3678,7 +3686,7 @@ int RealCUGAN::process_cpu_se_very_rough_stage0(const ncnn::Mat& inimage, const

if (channels == 4)
{
in_alpha_tile = in.channel_range(3, 1).clone();
in_alpha_tile = in_nopad.channel_range(3, 1).clone();
}
}

Expand Down

0 comments on commit 02c9c33

Please sign in to comment.