Skip to content

Commit

Permalink
Manual clamp
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Nov 25, 2024
1 parent 36fd2cd commit eceee29
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 55 deletions.
20 changes: 10 additions & 10 deletions src/rgb_to_nv_p16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn rgbx_to_yuv_bi_planar_10_impl<
let y_0 =
(r0 * transform.yr + g0 * transform.yg + b0 * transform.yb + bias_y) >> PRECISION;
y_dst[0] = transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
y_0.clamp(i_bias_y, i_cap_y),
y_0.max(i_bias_y).min(i_cap_y),
);

let rgba1 = &rgba[channels..channels * 2];
Expand All @@ -129,7 +129,7 @@ fn rgbx_to_yuv_bi_planar_10_impl<
let y_1 =
(r1 * transform.yr + g1 * transform.yg + b1 * transform.yb + bias_y) >> PRECISION;
y_dst[1] = transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
y_1.clamp(i_bias_y, i_cap_y),
y_1.max(i_bias_y).min(i_cap_y),
);

if compute_chroma {
Expand All @@ -143,11 +143,11 @@ fn rgbx_to_yuv_bi_planar_10_impl<
>> PRECISION;
uv_dst[nv_order.get_u_position()] =
transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
cb.clamp(i_bias_y, i_cap_uv),
cb.max(i_bias_y).min(i_cap_uv),
);
uv_dst[nv_order.get_v_position()] =
transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
cr.clamp(i_bias_y, i_cap_uv),
cr.max(i_bias_y).min(i_cap_uv),
);
}
}
Expand All @@ -164,7 +164,7 @@ fn rgbx_to_yuv_bi_planar_10_impl<
let y_0 =
(r0 * transform.yr + g0 * transform.yg + b0 * transform.yb + bias_y) >> PRECISION;
y_dst[0] = transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
y_0.clamp(i_bias_y, i_cap_y),
y_0.max(i_bias_y).min(i_cap_y),
);

if compute_chroma {
Expand All @@ -176,11 +176,11 @@ fn rgbx_to_yuv_bi_planar_10_impl<
>> PRECISION;
uv_dst[nv_order.get_u_position()] =
transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
cb.clamp(i_bias_y, i_cap_uv),
cb.max(i_bias_y).min(i_cap_uv),
);
uv_dst[nv_order.get_v_position()] =
transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
cr.clamp(i_bias_y, i_cap_uv),
cr.max(i_bias_y).min(i_cap_uv),
);
}
}
Expand Down Expand Up @@ -219,7 +219,7 @@ fn rgbx_to_yuv_bi_planar_10_impl<
let y_0 = (r0 * transform.yr + g0 * transform.yg + b0 * transform.yb + bias_y)
>> PRECISION;
*y_dst = transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
y_0.clamp(i_bias_y, i_cap_y),
y_0.max(i_bias_y).min(i_cap_y),
);
let cb =
(r0 * transform.cb_r + g0 * transform.cb_g + b0 * transform.cb_b + bias_uv)
Expand All @@ -229,11 +229,11 @@ fn rgbx_to_yuv_bi_planar_10_impl<
>> PRECISION;
uv_dst[nv_order.get_u_position()] =
transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
cb.clamp(i_bias_y, i_cap_uv),
cb.max(i_bias_y).min(i_cap_uv),
);
uv_dst[nv_order.get_v_position()] =
transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
cr.clamp(i_bias_y, i_cap_uv),
cr.max(i_bias_y).min(i_cap_uv),
);
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/rgb_to_y.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fn rgbx_to_y<const ORIGIN_CHANNELS: u8>(
let g = rgba[source_channels.get_g_channel_offset()] as i32;
let b = rgba[source_channels.get_b_channel_offset()] as i32;
let y = (r * transform.yr + g * transform.yg + b * transform.yb + bias_y) >> PRECISION;
*y_dst = y.clamp(i_bias_y, i_cap_y) as u8;
*y_dst = y.max(i_bias_y).min(i_cap_y) as u8;
}
});

Expand Down
20 changes: 10 additions & 10 deletions src/rgb_to_yuv_p16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ fn rgbx_to_yuv_ant<
let y_0 =
(r0 * transform.yr + g0 * transform.yg + b0 * transform.yb + bias_y) >> PRECISION;
y_dst[0] = transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
y_0.clamp(i_bias_y, i_cap_y),
y_0.max(i_bias_y).min(i_cap_y),
);

let r1 = rgba[channels + src_chans.get_r_channel_offset()] as i32;
Expand All @@ -230,7 +230,7 @@ fn rgbx_to_yuv_ant<
let y_1 =
(r1 * transform.yr + g1 * transform.yg + b1 * transform.yb + bias_y) >> PRECISION;
y_dst[1] = transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
y_1.clamp(i_bias_y, i_cap_y),
y_1.max(i_bias_y).min(i_cap_y),
);

if compute_chroma_row {
Expand All @@ -243,10 +243,10 @@ fn rgbx_to_yuv_ant<
let cr = (r * transform.cr_r + g * transform.cr_g + b * transform.cr_b + bias_uv)
>> PRECISION;
*u_dst = transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
cb.clamp(i_bias_y, i_cap_uv),
cb.max(i_bias_y).min(i_cap_uv),
);
*v_dst = transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
cr.clamp(i_bias_y, i_cap_uv),
cr.max(i_bias_y).min(i_cap_uv),
);
}
}
Expand All @@ -264,7 +264,7 @@ fn rgbx_to_yuv_ant<
let y_0 =
(r0 * transform.yr + g0 * transform.yg + b0 * transform.yb + bias_y) >> PRECISION;
*y_last = transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
y_0.clamp(i_bias_y, i_cap_y),
y_0.max(i_bias_y).min(i_cap_y),
);

if compute_chroma_row {
Expand All @@ -275,10 +275,10 @@ fn rgbx_to_yuv_ant<
(r0 * transform.cr_r + g0 * transform.cr_g + b0 * transform.cr_b + bias_uv)
>> PRECISION;
*u_last = transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
cb.clamp(i_bias_y, i_cap_uv),
cb.max(i_bias_y).min(i_cap_uv),
);
*v_last = transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
cr.clamp(i_bias_y, i_cap_uv),
cr.max(i_bias_y).min(i_cap_uv),
);
}
}
Expand Down Expand Up @@ -326,7 +326,7 @@ fn rgbx_to_yuv_ant<
let y_0 = (r0 * transform.yr + g0 * transform.yg + b0 * transform.yb + bias_y)
>> PRECISION;
*y_dst = transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
y_0.clamp(i_bias_y, i_cap_y),
y_0.max(i_bias_y).min(i_cap_y),
);

let cb =
Expand All @@ -336,10 +336,10 @@ fn rgbx_to_yuv_ant<
(r0 * transform.cr_r + g0 * transform.cr_g + b0 * transform.cr_b + bias_uv)
>> PRECISION;
*u_dst = transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
cb.clamp(i_bias_y, i_cap_uv),
cb.max(i_bias_y).min(i_cap_uv),
);
*v_dst = transform_integer::<ENDIANNESS, BYTES_POSITION, BIT_DEPTH>(
cr.clamp(i_bias_y, i_cap_uv),
cr.max(i_bias_y).min(i_cap_uv),
);
}
});
Expand Down
20 changes: 10 additions & 10 deletions src/rgba_to_nv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ fn rgbx_to_nv<const ORIGIN_CHANNELS: u8, const UV_ORDER: u8, const SAMPLING: u8>
let b0 = rgba0[src_chans.get_b_channel_offset()] as i32;
let y_0 =
(r0 * transform.yr + g0 * transform.yg + b0 * transform.yb + bias_y) >> PRECISION;
y_dst[0] = y_0.clamp(i_bias_y, i_cap_y) as u8;
y_dst[0] = y_0.max(i_bias_y).min(i_cap_y) as u8;

let rgba1 = &rgba[channels..channels * 2];

Expand All @@ -176,7 +176,7 @@ fn rgbx_to_nv<const ORIGIN_CHANNELS: u8, const UV_ORDER: u8, const SAMPLING: u8>

let y_1 =
(r1 * transform.yr + g1 * transform.yg + b1 * transform.yb + bias_y) >> PRECISION;
y_dst[1] = y_1.clamp(i_bias_y, i_cap_y) as u8;
y_dst[1] = y_1.max(i_bias_y).min(i_cap_y) as u8;

if compute_chroma {
let r = (r0 + r1 + 1) >> 1;
Expand All @@ -187,8 +187,8 @@ fn rgbx_to_nv<const ORIGIN_CHANNELS: u8, const UV_ORDER: u8, const SAMPLING: u8>
>> PRECISION;
let cr = (r * transform.cr_r + g * transform.cr_g + b * transform.cr_b + bias_uv)
>> PRECISION;
uv_dst[order.get_u_position()] = cb.clamp(i_bias_y, i_cap_uv) as u8;
uv_dst[order.get_v_position()] = cr.clamp(i_bias_y, i_cap_uv) as u8;
uv_dst[order.get_u_position()] = cb.max(i_bias_y).min(i_cap_uv) as u8;
uv_dst[order.get_v_position()] = cr.max(i_bias_y).min(i_cap_uv) as u8;
}
}

Expand All @@ -203,7 +203,7 @@ fn rgbx_to_nv<const ORIGIN_CHANNELS: u8, const UV_ORDER: u8, const SAMPLING: u8>
let b0 = rgba[src_chans.get_b_channel_offset()] as i32;
let y_0 =
(r0 * transform.yr + g0 * transform.yg + b0 * transform.yb + bias_y) >> PRECISION;
y_dst[0] = y_0.clamp(i_bias_y, i_cap_y) as u8;
y_dst[0] = y_0.max(i_bias_y).min(i_cap_y) as u8;

if compute_chroma {
let cb =
Expand All @@ -212,8 +212,8 @@ fn rgbx_to_nv<const ORIGIN_CHANNELS: u8, const UV_ORDER: u8, const SAMPLING: u8>
let cr =
(r0 * transform.cr_r + g0 * transform.cr_g + b0 * transform.cr_b + bias_uv)
>> PRECISION;
uv_dst[order.get_u_position()] = cb.clamp(i_bias_y, i_cap_uv) as u8;
uv_dst[order.get_v_position()] = cr.clamp(i_bias_y, i_cap_uv) as u8;
uv_dst[order.get_u_position()] = cb.max(i_bias_y).min(i_cap_uv) as u8;
uv_dst[order.get_v_position()] = cr.max(i_bias_y).min(i_cap_uv) as u8;
}
}
};
Expand Down Expand Up @@ -253,15 +253,15 @@ fn rgbx_to_nv<const ORIGIN_CHANNELS: u8, const UV_ORDER: u8, const SAMPLING: u8>
let b0 = rgba[src_chans.get_b_channel_offset()] as i32;
let y_0 = (r0 * transform.yr + g0 * transform.yg + b0 * transform.yb + bias_y)
>> PRECISION;
*y_dst = y_0.clamp(i_bias_y, i_cap_y) as u8;
*y_dst = y_0.max(i_bias_y).min(i_cap_y) as u8;
let cb =
(r0 * transform.cb_r + g0 * transform.cb_g + b0 * transform.cb_b + bias_uv)
>> PRECISION;
let cr =
(r0 * transform.cr_r + g0 * transform.cr_g + b0 * transform.cr_b + bias_uv)
>> PRECISION;
uv_dst[order.get_u_position()] = cb.clamp(i_bias_y, i_cap_uv) as u8;
uv_dst[order.get_v_position()] = cr.clamp(i_bias_y, i_cap_uv) as u8;
uv_dst[order.get_u_position()] = cb.max(i_bias_y).min(i_cap_uv) as u8;
uv_dst[order.get_v_position()] = cr.max(i_bias_y).min(i_cap_uv) as u8;
}
});
} else if chroma_subsampling == YuvChromaSubsampling::Yuv422 {
Expand Down
20 changes: 10 additions & 10 deletions src/rgba_to_yuv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,14 @@ fn rgbx_to_yuv8<const ORIGIN_CHANNELS: u8, const SAMPLING: u8>(
let b0 = rgba[src_chans.get_b_channel_offset()] as i32;
let y_0 =
(r0 * transform.yr + g0 * transform.yg + b0 * transform.yb + bias_y) >> PRECISION;
y_dst[0] = y_0.clamp(i_bias_y, i_cap_y) as u8;
y_dst[0] = y_0.max(i_bias_y).min(i_cap_y) as u8;

let r1 = rgba[channels + src_chans.get_r_channel_offset()] as i32;
let g1 = rgba[channels + src_chans.get_g_channel_offset()] as i32;
let b1 = rgba[channels + src_chans.get_b_channel_offset()] as i32;
let y_1 =
(r1 * transform.yr + g1 * transform.yg + b1 * transform.yb + bias_y) >> PRECISION;
y_dst[1] = y_1.clamp(i_bias_y, i_cap_y) as u8;
y_dst[1] = y_1.max(i_bias_y).min(i_cap_y) as u8;

if compute_chroma_row {
let r = (r0 + r1 + 1) >> 1;
Expand All @@ -228,8 +228,8 @@ fn rgbx_to_yuv8<const ORIGIN_CHANNELS: u8, const SAMPLING: u8>(
>> PRECISION;
let cr = (r * transform.cr_r + g * transform.cr_g + b * transform.cr_b + bias_uv)
>> PRECISION;
*u_dst = cb.clamp(i_bias_y, i_cap_uv) as u8;
*v_dst = cr.clamp(i_bias_y, i_cap_uv) as u8;
*u_dst = cb.max(i_bias_y).min(i_cap_uv) as u8;
*v_dst = cr.max(i_bias_y).min(i_cap_uv) as u8;
}
}

Expand All @@ -245,7 +245,7 @@ fn rgbx_to_yuv8<const ORIGIN_CHANNELS: u8, const SAMPLING: u8>(

let y_0 =
(r0 * transform.yr + g0 * transform.yg + b0 * transform.yb + bias_y) >> PRECISION;
*y_last = y_0.clamp(i_bias_y, i_cap_y) as u8;
*y_last = y_0.max(i_bias_y).min(i_cap_y) as u8;

if compute_chroma_row {
let cb =
Expand All @@ -254,8 +254,8 @@ fn rgbx_to_yuv8<const ORIGIN_CHANNELS: u8, const SAMPLING: u8>(
let cr =
(r0 * transform.cr_r + g0 * transform.cr_g + b0 * transform.cr_b + bias_uv)
>> PRECISION;
*u_last = cb.clamp(i_bias_y, i_cap_uv) as u8;
*v_last = cr.clamp(i_bias_y, i_cap_uv) as u8;
*u_last = cb.max(i_bias_y).min(i_cap_uv) as u8;
*v_last = cr.max(i_bias_y).min(i_cap_uv) as u8;
}
}
};
Expand Down Expand Up @@ -301,16 +301,16 @@ fn rgbx_to_yuv8<const ORIGIN_CHANNELS: u8, const SAMPLING: u8>(
let b0 = rgba[src_chans.get_b_channel_offset()] as i32;
let y_0 = (r0 * transform.yr + g0 * transform.yg + b0 * transform.yb + bias_y)
>> PRECISION;
*y_dst = y_0.clamp(i_bias_y, i_cap_y) as u8;
*y_dst = y_0.max(i_bias_y).min(i_cap_y) as u8;

let cb =
(r0 * transform.cb_r + g0 * transform.cb_g + b0 * transform.cb_b + bias_uv)
>> PRECISION;
let cr =
(r0 * transform.cr_r + g0 * transform.cr_g + b0 * transform.cr_b + bias_uv)
>> PRECISION;
*u_dst = cb.clamp(i_bias_y, i_cap_uv) as u8;
*v_dst = cr.clamp(i_bias_y, i_cap_uv) as u8;
*u_dst = cb.max(i_bias_y).min(i_cap_uv) as u8;
*v_dst = cr.max(i_bias_y).min(i_cap_uv) as u8;
}
});
} else if chroma_subsampling == YuvChromaSubsampling::Yuv422 {
Expand Down
28 changes: 14 additions & 14 deletions src/sharpyuv/sharp_rgba_to_yuv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn sharpen_row420<const ORIGIN_CHANNELS: u8, const SAMPLING: u8, const PRECISION
let b0 = rgba[src_chans.get_b_channel_offset()] as i32;

let y_0 = (r0 * transform.yr + g0 * transform.yg + b0 * transform.yb + bias_y) >> PRECISION;
y_dst[0] = y_0.clamp(i_bias_y, i_cap_y) as u8;
y_dst[0] = y_0.max(i_bias_y).min(i_cap_y) as u8;

let rgba_2 = &rgba[channels..channels * 2];

Expand All @@ -85,7 +85,7 @@ fn sharpen_row420<const ORIGIN_CHANNELS: u8, const SAMPLING: u8, const PRECISION
let b1 = rgba_2[src_chans.get_b_channel_offset()] as i32;

let y_1 = (r1 * transform.yr + g1 * transform.yg + b1 * transform.yb + bias_y) >> PRECISION;
y_dst[1] = y_1.clamp(i_bias_y, i_cap_y) as u8;
y_dst[1] = y_1.max(i_bias_y).min(i_cap_y) as u8;

if y_even_row {
let sharp_r_c = rgb_linearized[src_chans.get_r_channel_offset()];
Expand Down Expand Up @@ -143,8 +143,8 @@ fn sharpen_row420<const ORIGIN_CHANNELS: u8, const SAMPLING: u8, const PRECISION
+ corrected_b * transform.cr_b
+ bias_uv)
>> PRECISION;
*u_dst = cb.clamp(i_bias_y, i_cap_uv) as u8;
*v_dst = cr.clamp(i_bias_y, i_cap_uv) as u8;
*u_dst = cb.max(i_bias_y).min(i_cap_uv) as u8;
*v_dst = cr.max(i_bias_y).min(i_cap_uv) as u8;
}
}

Expand All @@ -158,7 +158,7 @@ fn sharpen_row420<const ORIGIN_CHANNELS: u8, const SAMPLING: u8, const PRECISION
let b0 = rgba[src_chans.get_b_channel_offset()] as i32;

let y_1 = (r0 * transform.yr + g0 * transform.yg + b0 * transform.yb + bias_y) >> PRECISION;
*y_last = y_1.clamp(i_bias_y, i_cap_y) as u8;
*y_last = y_1.max(i_bias_y).min(i_cap_y) as u8;

if y_even_row {
let rgba_lin = rgb_layout.chunks_exact(3).last().unwrap();
Expand Down Expand Up @@ -198,8 +198,8 @@ fn sharpen_row420<const ORIGIN_CHANNELS: u8, const SAMPLING: u8, const PRECISION
>> PRECISION;
let u_last = u_plane.last_mut().unwrap();
let v_last = v_plane.last_mut().unwrap();
*u_last = cb.clamp(i_bias_y, i_cap_uv) as u8;
*v_last = cr.clamp(i_bias_y, i_cap_uv) as u8;
*u_last = cb.max(i_bias_y).min(i_cap_uv) as u8;
*v_last = cr.max(i_bias_y).min(i_cap_uv) as u8;
}
}
}
Expand Down Expand Up @@ -242,7 +242,7 @@ fn sharpen_row422<const ORIGIN_CHANNELS: u8, const SAMPLING: u8, const PRECISION
let sharp_b_c = rgb_linearized[src_chans.get_b_channel_offset()];

let y_0 = (r0 * transform.yr + g0 * transform.yg + b0 * transform.yb + bias_y) >> PRECISION;
y_dst[0] = y_0.clamp(i_bias_y, i_cap_y) as u8;
y_dst[0] = y_0.max(i_bias_y).min(i_cap_y) as u8;

let rgba_2 = &rgba[channels..channels * 2];

Expand All @@ -251,7 +251,7 @@ fn sharpen_row422<const ORIGIN_CHANNELS: u8, const SAMPLING: u8, const PRECISION
let b1 = rgba_2[src_chans.get_b_channel_offset()] as i32;

let y_1 = (r1 * transform.yr + g1 * transform.yg + b1 * transform.yb + bias_y) >> PRECISION;
y_dst[1] = y_1.clamp(i_bias_y, i_cap_y) as u8;
y_dst[1] = y_1.max(i_bias_y).min(i_cap_y) as u8;

let rgb_linearized_2 = &rgb_linearized[3..(3 + 3)];

Expand Down Expand Up @@ -282,8 +282,8 @@ fn sharpen_row422<const ORIGIN_CHANNELS: u8, const SAMPLING: u8, const PRECISION
+ corrected_b * transform.cr_b
+ bias_uv)
>> PRECISION;
*u_dst = cb.clamp(i_bias_y, i_cap_uv) as u8;
*v_dst = cr.clamp(i_bias_y, i_cap_uv) as u8;
*u_dst = cb.max(i_bias_y).min(i_cap_uv) as u8;
*v_dst = cr.max(i_bias_y).min(i_cap_uv) as u8;
}

let rem_rgba = rgba.chunks_exact(channels * 2).remainder();
Expand All @@ -296,7 +296,7 @@ fn sharpen_row422<const ORIGIN_CHANNELS: u8, const SAMPLING: u8, const PRECISION
let b0 = rgba[src_chans.get_b_channel_offset()] as i32;

let y_1 = (r0 * transform.yr + g0 * transform.yg + b0 * transform.yb + bias_y) >> PRECISION;
*y_last = y_1.clamp(i_bias_y, i_cap_y) as u8;
*y_last = y_1.max(i_bias_y).min(i_cap_y) as u8;

let cb = (r0 * transform.cb_r + g0 * transform.cb_g + b0 * transform.cb_b + bias_uv)
>> PRECISION;
Expand All @@ -305,8 +305,8 @@ fn sharpen_row422<const ORIGIN_CHANNELS: u8, const SAMPLING: u8, const PRECISION

let u_last = u_plane.last_mut().unwrap();
let v_last = v_plane.last_mut().unwrap();
*u_last = cb.clamp(i_bias_y, i_cap_uv) as u8;
*v_last = cr.clamp(i_bias_y, i_cap_uv) as u8;
*u_last = cb.max(i_bias_y).min(i_cap_uv) as u8;
*v_last = cr.max(i_bias_y).min(i_cap_uv) as u8;
}
}

Expand Down

0 comments on commit eceee29

Please sign in to comment.