[Spice-devel] [spice-gtk 2/4] mjpeg: restrict use of i and j
Christophe Fergeau
cfergeau at redhat.com
Fri Jul 29 07:50:43 PDT 2011
Using i and j as variable names that are used from one loop to the
other isn't really readable, and makes the code more fragile than
it could be. This commits adds a "lines_read" variable which is more
expressive than "j", restricts "j" lifetime to the loop where it's
used, and it removes the "i" variable and uses counters provided
by libjpeg to iterate all the image lines.
It also has the side-effect that if jpeg_read_scanlines returns a short
read (less lines than expected are read), "dest" won't go out of sync but
will be set to the right place at the end of the loop.
---
gtk/channel-display-mjpeg.c | 19 ++++++++++---------
1 files changed, 10 insertions(+), 9 deletions(-)
diff --git a/gtk/channel-display-mjpeg.c b/gtk/channel-display-mjpeg.c
index dee0ef9..3789c87 100644
--- a/gtk/channel-display-mjpeg.c
+++ b/gtk/channel-display-mjpeg.c
@@ -69,7 +69,6 @@ void stream_mjpeg_data(display_stream *st)
int height = info->stream_height;
uint8_t *dest;
uint8_t *lines[4];
- int i, j;
dest = malloc(width * height * 4);
@@ -103,8 +102,11 @@ void stream_mjpeg_data(display_stream *st)
g_return_if_reached();
}
- for (i = 0; i < height; ) {
- for (j = 0; j < st->mjpeg_cinfo.rec_outbuf_height; j++) {
+ while (st->mjpeg_cinfo.output_scanline < st->mjpeg_cinfo.output_height) {
+ /* only used when JCS_EXTENSIONS is undefined */
+ G_GNUC_UNUSED unsigned int lines_read;
+
+ for (unsigned int j = 0; j < st->mjpeg_cinfo.rec_outbuf_height; j++) {
lines[j] = dest;
#ifdef JCS_EXTENSIONS
dest += 4 * width;
@@ -112,16 +114,16 @@ void stream_mjpeg_data(display_stream *st)
dest += 3 * width;
#endif
}
- j = jpeg_read_scanlines(&st->mjpeg_cinfo, lines,
+ lines_read = jpeg_read_scanlines(&st->mjpeg_cinfo, lines,
st->mjpeg_cinfo.rec_outbuf_height);
// this shouldn't happen either..
- g_return_if_fail(j == st->mjpeg_cinfo.rec_outbuf_height);
+ g_return_if_fail(lines_read == st->mjpeg_cinfo.rec_outbuf_height);
#ifndef JCS_EXTENSIONS
{
uint8_t *s = lines[0];
- uint32_t *d = (uint32_t *)&st->out_frame[i * width * 4];
+ uint32_t *d = (uint32_t *)s;
- for (j = j * width; j > 0; ) {
+ for (unsigned int j = lines_read * width; j > 0; ) {
j -= 1; // reverse order, bad for cache?
d[j] = s[j * 3 + 0] << 16 |
s[j * 3 + 1] << 8 |
@@ -129,8 +131,7 @@ void stream_mjpeg_data(display_stream *st)
}
}
#endif
- i += st->mjpeg_cinfo.rec_outbuf_height;
- dest = &st->out_frame[i * width * 4];
+ dest = &st->out_frame[st->mjpeg_cinfo.output_scanline * width * 4];
}
jpeg_finish_decompress(&st->mjpeg_cinfo);
}
--
1.7.6
More information about the Spice-devel
mailing list