diff options
author | Gary Martin <gary@garycmartin.com> | 2009-05-31 17:36:14 (GMT) |
---|---|---|
committer | Gary Martin <gary@garycmartin.com> | 2009-05-31 17:36:14 (GMT) |
commit | 9ecb13061f362aadb842ffdf263304cdd1421e3c (patch) | |
tree | 3070df161aded2ff7bde96c1eb90224526b41217 | |
parent | 84c9f787d5490cc1fdfeae2a5ccd5ca57b1c2c96 (diff) |
Finally managed to fix PDF exporting of images!!!
-rw-r--r-- | src/ImageThought.py | 8 | ||||
-rw-r--r-- | src/utils.py | 32 |
2 files changed, 24 insertions, 16 deletions
diff --git a/src/ImageThought.py b/src/ImageThought.py index c9c4d07..459c713 100644 --- a/src/ImageThought.py +++ b/src/ImageThought.py @@ -103,10 +103,12 @@ class ImageThought (ResizableThought): if hasattr(context, "set_source_pixbuf"): context.set_source_pixbuf (self.pic, self.pic_location[0]+move_x, self.pic_location[1]+move_y) elif hasattr(context, "set_source_surface"): - pixel_array = utils.pixbuf_to_cairo (self.pic.get_pixels_array()) - image_surface = cairo.ImageSurface.create_for_data(pixel_array, cairo.FORMAT_ARGB32, self.width, self.height, -1) + raw_pixels = self.pic.get_pixels_array() + pixel_array = utils.pixbuf_to_cairo (raw_pixels) + image_surface = cairo.ImageSurface.create_for_data(pixel_array, cairo.FORMAT_ARGB32, len(raw_pixels[0]), len(raw_pixels), -1) context.set_source_surface (image_surface, self.pic_location[0]+move_x, self.pic_location[1]+move_y) - context.rectangle (self.pic_location[0]+move_x, self.pic_location[1]+move_y, self.width, self.height) + + context.rectangle (self.pic_location[0]+move_x, self.pic_location[1]+move_y, len(raw_pixels[0]), len(raw_pixels)) context.fill () context.set_source_rgb (0,0,0) diff --git a/src/utils.py b/src/utils.py index 9d83aa7..36cd159 100644 --- a/src/utils.py +++ b/src/utils.py @@ -26,6 +26,7 @@ import sys from os.path import * import os +from array import array # Not available on OLPC's XO, but not needed neither #from Numeric import * @@ -235,16 +236,21 @@ def export_thought_outline (context, ul, lr, background_color, am_root = False, draw_thought_extended (context, real_ul, real_lr, False, am_primary, background_color, style == STYLE_EXTENDED_CONTENT) def pixbuf_to_cairo (pixel_array): - result = [] - for y in pixel_array: - row = [] - for x in y: - color = [int(x[2][0]), int(x[1][0]), int(x[0][0])] - if len(x) == 3: - color.append(255) - elif len(x) == 4: - color.append(int(x[3][0])) - row.append(color) - result.append(row) - return array(result, 'b') - + imgW, imgH = len(pixel_array[0]), len(pixel_array) + data = array('B', [0] * imgW * imgH * 4) + for y in range(imgH): + for x in range(imgW): + # cairo.FORMAT_ARGB32 uses pre-multiplied alpha + try: + alpha = pixel_array[y][x][3] + except: + alpha = 255 + alpha_mul = float(alpha) / 255 + + offset = (x + (y * imgW)) * 4 + data[offset] = int(int(pixel_array[y][x][2]) * alpha_mul) # B + data[offset+1] = int(int(pixel_array[y][x][1]) * alpha_mul) # G + data[offset+2] = int(int(pixel_array[y][x][0]) * alpha_mul) # R + data[offset+3] = alpha # A + + return data |