[#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
This commit is contained in:
hsoft 2009-10-03 11:49:53 +00:00
parent df6c9b3740
commit 90e2e43f3e
1 changed files with 16 additions and 11 deletions

View File

@ -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