From 90e2e43f3e27c107b6e134e220d6f255f61e66b3 Mon Sep 17 00:00:00 2001 From: hsoft Date: Sat, 3 Oct 2009 11:49:53 +0000 Subject: [PATCH] [#67 state:fixed] Fixed block.pyx so it takes Qt pixel alignment into account. --HG-- extra : convert_revision : svn%3Ac306627e-7827-47d3-bdf0-9a457c9553a1/trunk%40172 --- pe/qt/modules/block/block.pyx | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/pe/qt/modules/block/block.pyx b/pe/qt/modules/block/block.pyx index 22dc20d2..2f2c6825 100644 --- a/pe/qt/modules/block/block.pyx +++ b/pe/qt/modules/block/block.pyx @@ -6,7 +6,7 @@ # http://www.hardcoded.net/licenses/hs_license cdef object getblock(object image): - cdef int width, height, pixel_count, red, green, blue, i, offset + cdef int width, height, pixel_count, red, green, blue, i, offset, bytes_per_line cdef char *s cdef unsigned char r, g, b width = image.width() @@ -14,16 +14,21 @@ cdef object getblock(object image): if width: pixel_count = width * height red = green = blue = 0 + bytes_per_line = image.bytesPerLine() tmp = image.bits().asstring(image.numBytes()) s = tmp - for i in range(pixel_count): - offset = i * 3 - r = s[offset] - g = s[offset + 1] - b = s[offset + 2] - red += r - green += g - blue += b + # Qt aligns all its lines on 32bit, which means that if the number of bytes per + # line for image is not divisible by 4, there's going to be crap inserted in "s" + # We have to take this into account when calculating offsets + for i in range(height): + for j in range(width): + offset = i * bytes_per_line + j * 3 + r = s[offset] + g = s[offset + 1] + b = s[offset + 2] + red += r + green += g + blue += b return (red // pixel_count, green // pixel_count, blue // pixel_count) else: return (0, 0, 0) @@ -38,9 +43,9 @@ def getblocks(image, int block_count_per_side): block_height = max(height // block_count_per_side, 1) result = [] for ih in range(block_count_per_side): - top = min(ih * block_height, height - block_height) + top = min(ih*block_height, height-block_height-1) for iw in range(block_count_per_side): - left = min(iw * block_width, width - block_width) + left = min(iw*block_width, width-block_width-1) crop = image.copy(left, top, block_width, block_height) result.append(getblock(crop)) return result