diff options
Diffstat (limited to 'src/gen-image.c')
-rw-r--r-- | src/gen-image.c | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/src/gen-image.c b/src/gen-image.c index 170d694..d053d79 100644 --- a/src/gen-image.c +++ b/src/gen-image.c @@ -618,8 +618,15 @@ static void image_format_yuv_packed(const struct image *input, struct image *out o_c[2*x + u_offset] = idata[3*x + 1]; o_c[2*x + v_offset] = idata[3*x + 2]; } else { - o_c[2*x + u_offset] = (idata[3*x + 1] + idata[3*x + 4]) / 2; - o_c[2*x + v_offset] = (idata[3*x + 2] + idata[3*x + 5]) / 2; + unsigned int u = idata[3*x + 1] + idata[3*x + 4]; + unsigned int v = idata[3*x + 2] + idata[3*x + 5]; + + /* Round towards the middle value. */ + u += u < 0x100 ? 1 : 0; + v += v < 0x100 ? 1 : 0; + + o_c[2*x + u_offset] = u / 2; + o_c[2*x + v_offset] = v / 2; } } @@ -674,8 +681,15 @@ static void image_format_yuv_planar(const struct image *input, struct image *out } } else { for (x = 0; x < output->width; x += xsub) { - o_u[x*c_stride/xsub] = (idata[3*x + 1] + idata[3*x + 4]) / 2; - o_v[x*c_stride/xsub] = (idata[3*x + 2] + idata[3*x + 5]) / 2; + unsigned int u = idata[3*x + 1] + idata[3*x + 4]; + unsigned int v = idata[3*x + 2] + idata[3*x + 5]; + + /* Round towards the middle value. */ + u += u < 0x100 ? 1 : 0; + v += v < 0x100 ? 1 : 0; + + o_u[x*c_stride/xsub] = u / 2; + o_v[x*c_stride/xsub] = v / 2; } } |