Skip to contentSkip to conversation

Correct computation of bytes per pixel from bits per pixel

PATCH Superseded BALATON Zoltan 3 participants 3 messages Aug 21, 2012 – Aug 22, 2012

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Correct computation of bytes per pixel from bits per pixel

---
 console.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
console.c +2 -2
index 4525cc7..f698b77 100644
@@ -1612,7 +1612,7 @@ PixelFormat qemu_different_endianness_pixelformat(int bpp)
     memset(&pf, 0x00, sizeof(PixelFormat));
 
     pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = (bpp + 7) >> 3;
     pf.depth = bpp == 32 ? 24 : bpp;
 
     switch (bpp) {
@@ -1661,7 +1661,7 @@ PixelFormat qemu_default_pixelformat(int bpp)
     memset(&pf, 0x00, sizeof(PixelFormat));
 
     pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = (bpp + 7) >> 3;
     pf.depth = bpp == 32 ? 24 : bpp;
 
     switch (bpp) {
Signature · 1 line
1.7.10
#1 lore raw

Can you use this free space to provide a scenario where the missing round-up caused a problem?

Quoted 11 lines · BALATON Zoltan
---
 console.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
console.c +0 -0
index 4525cc7..f698b77 100644

console.c PixelFormat · line 1616

Quoted 6 lines
console.c +1 -1
@@ -1612,7 +1612,7 @@ PixelFormat
 qemu_different_endianness_pixelformat(int bpp)
     memset(&pf, 0x00, sizeof(PixelFormat));
 
     pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = (bpp + 7) >> 3;

Compilers are smart enough to substitute such divisions by shift operations but for humans the explicit form is easier to read. So please keep it.

console.c PixelFormat qemu_default_pixelformat(int bpp) · line 1667

Quoted 12 lines
console.c +0 -0
     pf.depth = bpp == 32 ? 24 : bpp;
 
     switch (bpp) {
console.c +1 -1
@@ -1661,7 +1661,7 @@ PixelFormat qemu_default_pixelformat(int bpp)
     memset(&pf, 0x00, sizeof(PixelFormat));
 
     pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = (bpp + 7) >> 3;
     pf.depth = bpp == 32 ? 24 : bpp;
 
     switch (bpp) {

Jan

#2 lore raw
In reply to BALATON Zoltan

On 21 August 2012 22:32, BALATON Zoltan <[email protected]> wrote:

If you make the Subject "console: Correct computation of bytes per pixel from bits per pixel" it's easier to tell what bit of qemu the patch is dealing with.

Quoted 10 lines
---
 console.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
console.c +0 -0
index 4525cc7..f698b77 100644

console.c PixelFormat qemu_different_endianness_pixelformat(int · line 1616

Quoted 6 lines
console.c +1 -1
@@ -1612,7 +1612,7 @@ PixelFormat qemu_different_endianness_pixelformat(int
 bpp)
     memset(&pf, 0x00, sizeof(PixelFormat));
 
     pf.bits_per_pixel = bpp;
-    pf.bytes_per_pixel = bpp / 8;
+    pf.bytes_per_pixel = (bpp + 7) >> 3;

You could write this as

       pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);

-- PMM