def test_not_implemented(self): """ fix me: Currently not implemented, so we just ensure that any call to it throws an exception. """ gc = GraphicsContextArray((1,1), pix_format="rgb24") gc.rotate_ctm(1.0)
def test_not_implemented(self): """ fix me: Currently not implemented, so we just ensure that any call to it throws an exception. """ gc = GraphicsContextArray((1, 1), pix_format="rgb24") gc.rotate_ctm(1.0)
def test_clip_to_rect_rotated(self): # FIXME: test skipped # This test raises an exception currently because the # underlying library doesn't handle clipping to a rotated # rectangle. For now, we catch the the case with an # exception, so that people can't screw up. In the future, # we should actually support this functionality. gc = GraphicsContextArray((1, 1), pix_format="rgb24") gc.rotate_ctm(1.0) self.assertRaises(NotImplementedError, gc.clip_to_rect, 0, 0, 1, 1)
def test_clip_to_rect_rotated(self): # FIXME: test skipped # This test raises an exception currently because the # underlying library doesn't handle clipping to a rotated # rectangle. For now, we catch the the case with an # exception, so that people can't screw up. In the future, # we should actually support this functionality. raise nose.SkipTest gc = GraphicsContextArray((1,1), pix_format="rgb24") gc.rotate_ctm(1.0) self.assertRaises(NotImplementedError, gc.clip_to_rect, 0, 0, 1, 1)
def _kiva_array_from_numpy_array(self, data): if data.shape[2] not in KIVA_DEPTH_MAP: msg = "Unknown colormap depth value: {}" raise RuntimeError(msg.format(data.shape[2])) kiva_depth = KIVA_DEPTH_MAP[data.shape[2]] # Data presented to the GraphicsContextArray needs to be contiguous. data = np.ascontiguousarray(data) return GraphicsContextArray(data, pix_format=kiva_depth)
def save(gc, filename, file_format=None, pil_options=None): """ Save the GraphicsContext to a file. Output files are always saved in RGB or RGBA format; if this GC is not in one of these formats, it is automatically converted. If filename includes an extension, the image format is inferred from it. file_format is only required if the format can't be inferred from the filename (e.g. if you wanted to save a PNG file as a .dat or .bin). filename may also be "file-like" object such as a StringIO, in which case a file_format must be supplied pil_options is a dict of format-specific options that are passed down to the PIL image file writer. If a writer doesn't recognize an option, it is silently ignored. If the image has an alpha channel and the specified output file format does not support alpha, the image is saved in rgb24 format. """ FmtsWithoutAlpha = ('jpg', 'bmp', 'eps', "jpeg") from PIL import Image as PilImage size = (gc.width(), gc.height()) fmt = gc.format() # determine the output pixel format and PIL format if fmt.endswith("32"): pilformat = "RGBA" pixelformat = "rgba32" if (isinstance(filename, basestring) and filename[-3:].lower() in FmtsWithoutAlpha) or \ (file_format is not None and file_format.lower() in FmtsWithoutAlpha): pilformat = "RGB" pixelformat = "rgb24" elif fmt.endswith("24"): pilformat = "RGB" pixelformat = "rgb24" # perform a conversion if necessary if fmt != pixelformat: newimg = GraphicsContextArray(size, fmt) newimg.draw_image(gc) newimg.convert_pixel_format(pixelformat, 1) bmp = newimg.bmp_array else: bmp = gc.bmp_array img = PilImage.frombytes(pilformat, size, bmp.tostring()) img.save(filename, format=file_format, options=pil_options)
def test_reset_path(self): """ clip_to_rect() should clear the current path. This is to maintain compatibility with the version of kiva that sits on top of Apple's Quartz engine. """ desired = array([ [255, 255, 0, 0], [255, 255, 0, 0], [255, 255, 0, 0], [255, 255, 0, 0], ]) shp = tuple(transpose(desired.shape)) gc = GraphicsContextArray(shp, pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) gc.rect(0, 0, 2, 4) gc.clip_to_rect(0, 0, 4, 4) gc.rect(2, 0, 2, 4) # These settings allow the fastest path. gc.set_fill_color((0.0, 0.0, 0.0)) # black gc.fill_path() # test a single color channel actual = gc.bmp_array[:, :, 0] self.assertRavelEqual(desired, actual)
def successive_clip_helper(self, desired, scale, clip_rect1, clip_rect2): """ desired -- 2D array with a single channels expected byte pattern. scale -- used in scale_ctm() to change the ctm. clip_rect1 -- 1st clipping path. clip_rect2 -- 2nd clipping path. """ shp = tuple(transpose(desired.shape)) gc = GraphicsContextArray(shp, pix_format="rgb24") gc.scale_ctm(scale, scale) # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) gc.clip_to_rect(*clip_rect1) gc.clip_to_rect(*clip_rect2) gc.rect(0, 0, 4, 4) # These settings allow the fastest path. gc.set_fill_color((0.0, 0.0, 0.0)) # black gc.fill_path() # test a single color channel actual = gc.bmp_array[:, :, 0] self.assertRavelEqual(desired, actual)
def test_save_restore_clip_state(self): desired1 = array([ [255, 255, 255, 255], [255, 0, 0, 255], [255, 0, 0, 255], [255, 255, 255, 255], ]) desired2 = array([ [255, 0, 0, 0], [255, 0, 0, 0], [255, 0, 0, 0], [255, 255, 255, 255], ]) gc = GraphicsContextArray((4, 4), pix_format="rgb24") gc.clear((1.0, 1.0, 1.0)) gc.set_fill_color((0.0, 0.0, 0.0)) gc.clip_to_rect(1, 1, 3, 3) gc.save_state() gc.clip_to_rect(1, 1, 2, 2) gc.rect(0, 0, 4, 4) gc.fill_path() actual1 = gc.bmp_array[:, :, 0] self.assertRavelEqual(desired1, actual1) gc.restore_state() gc.rect(0, 0, 4, 4) gc.fill_path() actual2 = gc.bmp_array[:, :, 0] self.assertRavelEqual(desired2, actual2)
def dash(sz=(1000, 1000)): gc = GraphicsContextArray(sz) gc.set_fill_color((1.0, 0.0, 0.0, 0.1)) gc.set_stroke_color((0.0, 1.0, 0.0, 0.6)) width = 10 gc.set_line_width(10) phase = width * 2.5 pattern = width * numpy.array((5, 5)) gc.set_line_dash(pattern, phase) gc.set_line_cap(constants.CAP_BUTT) t1 = perf_counter() gc.move_to(10, 10) gc.line_to(sz[0] - 10, sz[1] - 10) gc.line_to(10, sz[1] - 10) gc.close_path() gc.draw_path() t2 = perf_counter() with tempfile.NamedTemporaryFile(suffix=".bmp") as fid: gc.save(fid.name) image = Image.from_file(fid.name, resist_width="weak", resist_height="weak") tot_time = t2 - t1 print("time:", tot_time) return image
def __init__(self, x, y, radius, color='red', line_width=2): super(Circle, self).__init__(x, y, color=color, line_width=line_width) self.radius = radius def draw(self, image): image.set_stroke_color(color_dict[self.color]) image.set_line_width(self.line_width) image.arc(self.x, self.y, self.radius, 0, 6.28318) image.close_path() image.stroke_path() # Create an image that we can draw our shapes into image_size = (300,300) image = Image(image_size) # Create a box and add it to the image. box = Square(30, 30, 100, color='green') box.draw(image) line = Line(50, 250, 250, 50) line.draw(image) rect = Rectangle( 50, 50, 30, 50) rect.draw(image) circle = Circle( 150, 150, 60, color='blue') circle.draw(image) # Save the image out as a png image.
def test_save_restore_clip_path(self): desired = array([[255, 255, 255, 255], [255, 0, 0, 255], [255, 0, 0, 255], [255, 255, 255, 255]]) # this is the clipping path we hope to see. clip_rect1 = (1, 1, 2, 2) # this will be a second path that will push/pop that should # never be seen. clip_rect2 = (1, 1, 1, 1) shp = tuple(transpose(desired.shape)) gc = GraphicsContextArray(shp, pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) gc.clip_to_rect(*clip_rect1) # push and then pop a path that shouldn't affect the drawing gc.save_state() gc.clip_to_rect(*clip_rect2) gc.restore_state() gc.rect(0, 0, 4, 4) # These settings allow the fastest path. gc. set_fill_color((0.0, 0.0, 0.0)) # black gc.fill_path() # test a single color channel actual = gc.bmp_array[:,:,0] self.assertRavelEqual(desired, actual)
def test_alias_width_two_scanline_aa(self): """ When width > 1, alias text is drawn using a couple of different paths through the underlying C++ code. This test the slower of the two which uses the agg::rasterizer_scanline_aa C++ code. We've set the line join to bevel to trigger this path. """ gc = GraphicsContextArray((3, 2), pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) # single horizontal line across bottom of buffer gc.move_to(0, 0) gc.line_to(3, 0) # Settings allow the 2nd fastest path. gc.set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(False) gc.set_line_width(2) gc.set_line_cap(kiva.CAP_ROUND) gc.set_line_join(kiva.JOIN_BEVEL) gc.stroke_path() # test a single color channel. desired = array(((255, 255, 255), (0, 0, 0))) actual = gc.bmp_array[:, :, 0] self.assertRavelEqual(actual, desired)
def test_alias_cap_square(self): """ Square caps should extend beyond the end of the line. by half the width of the line. """ gc = GraphicsContextArray((6,6), pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) # single horizontal line across bottom of buffer gc.move_to(2, 3) gc.line_to(4, 3) # Set up line gc. set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(False) gc.set_line_width(2) gc.set_line_cap(kiva.CAP_SQUARE) gc.set_line_join(kiva.JOIN_MITER) gc.stroke_path() desired = array(((255, 255, 255, 255, 255, 255), (255, 255, 255, 255, 255, 255), (255, 0, 0, 0, 0, 255), (255, 0, 0, 0, 0, 255), (255, 255, 255, 255, 255, 255), (255, 255, 255, 255, 255, 255))) actual = gc.bmp_array[:,:,0] self.assertRavelEqual(desired, actual)
def test_alias_cap_round(self): """ Round caps should extend beyond the end of the line. We don't really test the shape here. To do this, a test of a wider line would be needed. fix me: This is rendering antialiased end points currently. """ gc = GraphicsContextArray((6,6), pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) # single horizontal line across bottom of buffer gc.move_to(2, 3) gc.line_to(4, 3) # Set up line gc. set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(False) gc.set_line_width(2) gc.set_line_cap(kiva.CAP_ROUND) gc.set_line_join(kiva.JOIN_MITER) gc.stroke_path() desired = array(((255, 255, 255, 255, 255, 255), (255, 255, 255, 255, 255, 255), (255, 0, 0, 0, 0, 255), (255, 0, 0, 0, 0, 255), (255, 255, 255, 255, 255, 255), (255, 255, 255, 255, 255, 255))) actual = gc.bmp_array[:,:,0] self.assertRavelEqual(desired, actual)
def test_alias_width_two_scanline_aa(self): """ When width > 1, alias text is drawn using a couple of different paths through the underlying C++ code. This test the slower of the two which uses the agg::rasterizer_scanline_aa C++ code. We've set the line join to bevel to trigger this path. """ gc = GraphicsContextArray((3, 2), pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) # single horizontal line across bottom of buffer gc.move_to(0, 0) gc.line_to(3, 0) # Settings allow the 2nd fastest path. gc. set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(False) gc.set_line_width(2) gc.set_line_cap(kiva.CAP_ROUND) gc.set_line_join(kiva.JOIN_BEVEL) gc.stroke_path() # test a single color channel. desired = array(((255, 255, 255), ( 0, 0, 0))) actual = gc.bmp_array[:,:,0] self.assertRavelEqual(actual, desired)
def test_alias_width_two_outline_aa(self): """ When width>1, alias text is drawn using a couple of different paths through the underlying C++ code. This test the faster of the two which uses the agg::rasterizer_outline_aa C++ code. It is only used when 2<=width<=10, and cap is ROUND or BUTT, and join is MITER The C++ classes used in the underlying C++ code for this is agg::rasterizer_outline_aa and agg::renderer_outline_aa """ gc = GraphicsContextArray((3, 2), pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) # single horizontal line across bottom of buffer gc.move_to(0, 0) gc.line_to(3, 0) # Settings allow the 2nd fastest path. gc.set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(False) gc.set_line_width(2) gc.set_line_cap(kiva.CAP_ROUND) gc.set_line_join(kiva.JOIN_MITER) gc.stroke_path() # test a single color channel. desired = array(((255, 255, 255), (0, 0, 0))) actual = gc.bmp_array[:, :, 0] self.assertRavelEqual(actual, desired)
def test_alias_width_one(self): """ The fastest path through the stroke path code is for aliased path with width=1. It is reasonably safe here not to worry with testing all the CAP/JOIN combinations because they are all rendered the same for this case. It is handled by the agg::rasterizer_outline and the agg::renderer_primitives classes in C++. Energy for an aliased horizontal line of width=1 falls within a single line of pixels. With y=0 for this line, the engine should paint a single row of zeros along the bottom edge of the bmp array. """ gc = GraphicsContextArray((3, 2), pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) # single horizontal line across bottom of buffer gc.move_to(0, 0) gc.line_to(3, 0) # These settings allow the fastest path. gc.set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(False) gc.set_line_width(1) gc.stroke_path() # test a single color channel. desired = array(((255, 255, 255), (0, 0, 0))) actual = gc.bmp_array[:, :, 0] self.assertRavelEqual(actual, desired)
def test_curve_to(self): """ curve_to conv_curve happens early in the agg rendering pipeline, so it isn't neccessary to test every combination of antialias, line_cap, line_join, etc. If it works for one, we should be in good shape for the others (until the implementation is changed of course...) """ gc = GraphicsContextArray((10, 10), pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) # single horizontal line across bottom of buffer x0, y0 = 1.0, 5.0 x1, y1 = 4.0, 9.0 x2, y2 = 6.0, 1.0 x3, y3 = 9.0, 5.0 gc.move_to(x0, y0) gc.curve_to(x1, y1, x2, y2, x3, y3) # Set up stroke gc.set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(True) gc.set_line_width(1) gc.set_line_cap(kiva.CAP_BUTT) gc.set_line_join(kiva.JOIN_MITER) gc.stroke_path() gc.set_stroke_color((0.0, 1.0, 1.0)) gc.move_to(x0, y0) gc.line_to(x1, y1) gc.move_to(x2, y2) gc.line_to(x3, y3) gc.stroke_path() # test a single color channel. # note: This is a "screen capture" from running this # test. It looks right, but hasn't been check closely. desired = array([[255, 255, 255, 230, 255, 255, 255, 255, 255, 255], [255, 255, 231, 25, 212, 255, 255, 255, 255, 255], [255, 252, 65, 128, 255, 255, 255, 255, 255, 255], [255, 103, 26, 143, 229, 255, 255, 255, 255, 255], [179, 2, 115, 96, 23, 189, 255, 255, 204, 255], [255, 205, 255, 255, 189, 23, 97, 116, 2, 179], [255, 255, 255, 255, 255, 229, 142, 25, 103, 255], [255, 255, 255, 255, 255, 255, 127, 66, 252, 255], [255, 255, 255, 255, 255, 212, 26, 231, 255, 255], [255, 255, 255, 255, 255, 255, 231, 255, 255, 255]]) actual = gc.bmp_array[:, :, 0] self.assertRavelEqual(desired, actual)
def test_antialias_width_slower_path(self): """ An anti-aliased horizontal line of width=1 has its energy centered between the bottom row of pixels and the next lower row of pixels (which is off the page). It dumps half its energy in each, so we end up with a single line of 127,127,127 pixel values in the last row of pixels. This particular set of flags is handled by the agg::rasterizer_scanline_aa path through the C++ code. """ gc = GraphicsContextArray((3, 2), pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) # single horizontal line across bottom of buffer gc.move_to(0, 0) gc.line_to(3, 0) # Set up stroke gc.set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(True) gc.set_line_width(1) gc.set_line_cap(kiva.CAP_BUTT) gc.set_line_join(kiva.JOIN_BEVEL) gc.stroke_path() # test a single color channel. desired = array(((255, 255, 255), (127, 127, 127))) actual = gc.bmp_array[:, :, 0] self.assertRavelEqual(desired, actual)
def test_alias_cap_square(self): """ Square caps should extend beyond the end of the line. by half the width of the line. """ gc = GraphicsContextArray((6, 6), pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) # single horizontal line across bottom of buffer gc.move_to(2, 3) gc.line_to(4, 3) # Set up line gc.set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(False) gc.set_line_width(2) gc.set_line_cap(kiva.CAP_SQUARE) gc.set_line_join(kiva.JOIN_MITER) gc.stroke_path() desired = array( ((255, 255, 255, 255, 255, 255), (255, 255, 255, 255, 255, 255), (255, 0, 0, 0, 0, 255), (255, 0, 0, 0, 0, 255), (255, 255, 255, 255, 255, 255), (255, 255, 255, 255, 255, 255))) actual = gc.bmp_array[:, :, 0] self.assertRavelEqual(desired, actual)
def test_save_restore_clip_state(self): desired1 = array([[255, 255, 255, 255], [255, 0, 0, 255], [255, 0, 0, 255], [255, 255, 255, 255]]) desired2 = array([[255, 0, 0, 0], [255, 0, 0, 0], [255, 0, 0, 0], [255, 255, 255, 255]]) gc = GraphicsContextArray((4,4), pix_format="rgb24") gc.clear((1.0, 1.0, 1.0)) gc.set_fill_color((0.0, 0.0, 0.0)) gc.clip_to_rect(1, 1, 3, 3) gc.save_state() gc.clip_to_rect(1, 1, 2, 2) gc.rect(0, 0, 4, 4) gc.fill_path() actual1 = gc.bmp_array[:,:,0] self.assertRavelEqual(desired1, actual1) gc.restore_state() gc.rect(0, 0, 4, 4) gc.fill_path() actual2 = gc.bmp_array[:,:,0] self.assertRavelEqual(desired2, actual2)
def test_antialias_width_slower_path(self): """ An anti-aliased horizontal line of width=1 has its energy centered between the bottom row of pixels and the next lower row of pixels (which is off the page). It dumps half its energy in each, so we end up with a single line of 127,127,127 pixel values in the last row of pixels. This particular set of flags is handled by the agg::rasterizer_scanline_aa path through the C++ code. """ gc = GraphicsContextArray((3, 2), pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) # single horizontal line across bottom of buffer gc.move_to(0, 0) gc.line_to(3, 0) # Set up stroke gc. set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(True) gc.set_line_width(1) gc.set_line_cap(kiva.CAP_BUTT) gc.set_line_join(kiva.JOIN_BEVEL) gc.stroke_path() # test a single color channel. desired = array(((255, 255, 255), (127, 127, 127))) actual = gc.bmp_array[:,:,0] self.assertRavelEqual(desired, actual)
def successive_clip_helper(self, desired, scale, clip_rect1, clip_rect2): """ desired -- 2D array with a single channels expected byte pattern. scale -- used in scale_ctm() to change the ctm. clip_rect1 -- 1st clipping path. clip_rect2 -- 2nd clipping path. """ shp = tuple(transpose(desired.shape)) gc = GraphicsContextArray(shp, pix_format="rgb24") gc.scale_ctm(scale, scale) # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) gc.clip_to_rect(*clip_rect1) gc.clip_to_rect(*clip_rect2) gc.rect(0, 0, 4, 4) # These settings allow the fastest path. gc. set_fill_color((0.0, 0.0, 0.0)) # black gc.fill_path() # test a single color channel actual = gc.bmp_array[:,:,0] self.assertRavelEqual(desired, actual)
def test_curve_to(self): """ curve_to conv_curve happens early in the agg rendering pipeline, so it isn't neccessary to test every combination of antialias, line_cap, line_join, etc. If it works for one, we should be in good shape for the others (until the implementation is changed of course...) """ gc = GraphicsContextArray((10, 10), pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) # single horizontal line across bottom of buffer x0, y0 = 1.0, 5.0 x1, y1 = 4.0, 9.0 x2, y2 = 6.0, 1.0 x3, y3 = 9.0, 5.0 gc.move_to(x0, y0) gc.curve_to(x1, y1, x2, y2, x3, y3); # Set up stroke gc. set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(True) gc.set_line_width(1) gc.set_line_cap(kiva.CAP_BUTT) gc.set_line_join(kiva.JOIN_MITER) gc.stroke_path() gc. set_stroke_color((0.0, 1.0, 1.0)) gc.move_to(x0, y0) gc.line_to(x1, y1) gc.move_to(x2, y2) gc.line_to(x3, y3) gc.stroke_path() # test a single color channel. # note: This is a "screen capture" from running this # test. It looks right, but hasn't been check closely. desired = array([[255, 255, 255, 230, 255, 255, 255, 255, 255, 255], [255, 255, 231, 25, 212, 255, 255, 255, 255, 255], [255, 252, 65, 128, 255, 255, 255, 255, 255, 255], [255, 103, 26, 143, 229, 255, 255, 255, 255, 255], [179, 2, 115, 96, 23, 189, 255, 255, 204, 255], [255, 205, 255, 255, 189, 23, 97, 116, 2, 179], [255, 255, 255, 255, 255, 229, 142, 25, 103, 255], [255, 255, 255, 255, 255, 255, 127, 66, 252, 255], [255, 255, 255, 255, 255, 212, 26, 231, 255, 255], [255, 255, 255, 255, 255, 255, 231, 255, 255, 255]]) actual = gc.bmp_array[:,:,0] self.assertRavelEqual(desired, actual)
def test_reset_path(self): """ clip_to_rect() should clear the current path. This is to maintain compatibility with the version of kiva that sits on top of Apple's Quartz engine. """ desired = array([[255, 255, 0, 0], [255, 255, 0, 0], [255, 255, 0, 0], [255, 255, 0, 0]]) shp = tuple(transpose(desired.shape)) gc = GraphicsContextArray(shp, pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) gc.rect(0, 0, 2, 4) gc.clip_to_rect(0, 0, 4, 4) gc.rect(2, 0, 2, 4) # These settings allow the fastest path. gc. set_fill_color((0.0, 0.0, 0.0)) # black gc.fill_path() # test a single color channel actual = gc.bmp_array[:,:,0] self.assertRavelEqual(desired, actual)
def test_alias_width_one(self): """ The fastest path through the stroke path code is for aliased path with width=1. It is reasonably safe here not to worry with testing all the CAP/JOIN combinations because they are all rendered the same for this case. It is handled by the agg::rasterizer_outline and the agg::renderer_primitives classes in C++. Energy for an aliased horizontal line of width=1 falls within a single line of pixels. With y=0 for this line, the engine should paint a single row of zeros along the bottom edge of the bmp array. """ gc = GraphicsContextArray((3, 2), pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) # single horizontal line across bottom of buffer gc.move_to(0, 0) gc.line_to(3, 0) # These settings allow the fastest path. gc. set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(False) gc.set_line_width(1) gc.stroke_path() # test a single color channel. desired = array(((255,255,255), ( 0, 0, 0))) actual = gc.bmp_array[:,:,0] self.assertRavelEqual(actual, desired)
def clip_to_rect_helper(self, desired, scale, clip_rects): """ desired -- 2D array with a single channels expected byte pattern. scale -- used in scale_ctm() to change the ctm. clip_args -- passed in as *clip_args to clip_to_rect. """ shp = tuple(transpose(desired.shape)) gc = GraphicsContextArray(shp, pix_format="rgb24") gc.scale_ctm(scale, scale) # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) if isinstance(clip_rects, tuple): gc.clip_to_rect(*clip_rects) else: for rect in clip_rects: gc.clip_to_rect(*rect) gc.rect(0, 0, 4, 4) # These settings allow the fastest path. gc.set_fill_color((0.0, 0.0, 0.0)) # black gc.fill_path() # test a single color channel actual = gc.bmp_array[:,:,0] self.assertRavelEqual(desired, actual)
def test_alias_width_two_outline_aa(self): """ When width>1, alias text is drawn using a couple of different paths through the underlying C++ code. This test the faster of the two which uses the agg::rasterizer_outline_aa C++ code. It is only used when 2<=width<=10, and cap is ROUND or BUTT, and join is MITER The C++ classes used in the underlying C++ code for this is agg::rasterizer_outline_aa and agg::renderer_outline_aa """ gc = GraphicsContextArray((3, 2), pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) # single horizontal line across bottom of buffer gc.move_to(0, 0) gc.line_to(3, 0) # Settings allow the 2nd fastest path. gc. set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(False) gc.set_line_width(2) gc.set_line_cap(kiva.CAP_ROUND) gc.set_line_join(kiva.JOIN_MITER) gc.stroke_path() # test a single color channel. desired = array(((255, 255, 255), ( 0, 0, 0))) actual = gc.bmp_array[:,:,0] self.assertRavelEqual(actual, desired)
def dash(sz=(1000,1000)): gc = GraphicsContextArray(sz) gc.set_fill_color((1.0,0.0,0.0,0.1)) gc.set_stroke_color((0.0,1.0,0.0,0.6)) width = 10 gc.set_line_width(10) phase = width * 2.5; pattern = width * numpy.array((5,5)) gc.set_line_dash(pattern,phase) gc.set_line_cap(constants.CAP_BUTT) t1 = time.clock() gc.move_to(10,10) gc.line_to(sz[0]-10,sz[1]-10) gc.line_to(10,sz[1]-10) gc.close_path() gc.draw_path() t2 = time.clock() gc.save("dash.bmp") tot_time = t2 - t1 print 'time:', tot_time
def clip_to_rect_helper(self, desired, scale, clip_rects): """ desired -- 2D array with a single channels expected byte pattern. scale -- used in scale_ctm() to change the ctm. clip_args -- passed in as *clip_args to clip_to_rect. """ shp = tuple(transpose(desired.shape)) gc = GraphicsContextArray(shp, pix_format="rgb24") gc.scale_ctm(scale, scale) # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) if isinstance(clip_rects, tuple): gc.clip_to_rect(*clip_rects) else: for rect in clip_rects: gc.clip_to_rect(*rect) gc.rect(0, 0, 4, 4) # These settings allow the fastest path. gc.set_fill_color((0.0, 0.0, 0.0)) # black gc.fill_path() # test a single color channel actual = gc.bmp_array[:, :, 0] self.assertRavelEqual(desired, actual)
def _compute_cached_image(self, data=None): """ Computes the correct sub-image coordinates and renders an image into self._cached_image. The parameter *data* is for subclasses that might not store an RGB(A) image as the value, but need to compute one to display (colormaps, etc.). """ if data is None: data = self.value.data (lpt, upt) = self.index.get_bounds() ll_x, ll_y = self.map_screen([lpt])[0] ur_x, ur_y = self.map_screen([upt])[0] if "right" in self.origin: ll_x, ur_x = ur_x, ll_x if "top" in self.origin: ll_y, ur_y = ur_y, ll_y virtual_width = ur_x - ll_x virtual_height = ur_y - ll_y args = self.position \ + self.bounds \ + [ll_x, ll_y, virtual_width, virtual_height] img_pixels, gc_rect = self._calc_zoom_coords(*args) # Grab the appropriate sub-image, if necessary if img_pixels is not None: i1, j1, i2, j2 = img_pixels if "top" in self.origin: y_length = self.value.get_array_bounds()[1][1] j1 = y_length - j1 j2 = y_length - j2 # swap so that j1 < j2 j1, j2 = j2, j1 if "right" in self.origin: x_length = self.value.get_array_bounds()[0][1] i1 = x_length - i1 i2 = x_length - i2 # swap so that i1 < i2 i1, i2 = i2, i1 # Since data is row-major, j1 and j2 go first data = data[j1:j2, i1:i2] # Furthermore, the data presented to the GraphicsContextArray needs to # be contiguous. If it is not, we need to make a copy. if not data.flags['C_CONTIGUOUS']: data = data.copy() if data.shape[2] == 3: kiva_depth = "rgb24" elif data.shape[2] == 4: kiva_depth = "rgba32" else: raise RuntimeError, "Unknown colormap depth value: %i" \ % data.value_depth self._cached_image = GraphicsContextArray(data, pix_format=kiva_depth) if gc_rect is not None: self._cached_dest_rect = gc_rect else: self._cached_dest_rect = (ll_x, ll_y, virtual_width, virtual_height) self._image_cache_valid = True
def dash(sz=(1000, 1000)): gc = GraphicsContextArray(sz) gc.set_fill_color((1.0, 0.0, 0.0, 0.1)) gc.set_stroke_color((0.0, 1.0, 0.0, 0.6)) width = 10 gc.set_line_width(10) phase = width * 2.5 pattern = width * numpy.array((5, 5)) gc.set_line_dash(pattern, phase) gc.set_line_cap(constants.CAP_BUTT) t1 = time.clock() gc.move_to(10, 10) gc.line_to(sz[0] - 10, sz[1] - 10) gc.line_to(10, sz[1] - 10) gc.close_path() gc.draw_path() t2 = time.clock() gc.save("dash.bmp") tot_time = t2 - t1 print('time:', tot_time)
def test_save_restore_clip_path(self): desired = array([ [255, 255, 255, 255], [255, 0, 0, 255], [255, 0, 0, 255], [255, 255, 255, 255], ]) # this is the clipping path we hope to see. clip_rect1 = (1, 1, 2, 2) # this will be a second path that will push/pop that should # never be seen. clip_rect2 = (1, 1, 1, 1) shp = tuple(transpose(desired.shape)) gc = GraphicsContextArray(shp, pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) gc.clip_to_rect(*clip_rect1) # push and then pop a path that shouldn't affect the drawing gc.save_state() gc.clip_to_rect(*clip_rect2) gc.restore_state() gc.rect(0, 0, 4, 4) # These settings allow the fastest path. gc.set_fill_color((0.0, 0.0, 0.0)) # black gc.fill_path() # test a single color channel actual = gc.bmp_array[:, :, 0] self.assertRavelEqual(desired, actual)
def test_show_text_at_point(self): gc = GraphicsContextArray((100, 100)) gc.set_font(Font()) gc.show_text_at_point(str('asdf'), 5, 5)
def helper(self, antialias, width, line_cap, line_join, size=(10,10)): gc = GraphicsContextArray(size, pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) # single horizontal line across bottom of buffer gc.move_to(1, 3) gc.line_to(7, 3) gc.line_to(7, 9) # Settings allow the faster outline path through C++ code gc. set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(antialias) gc.set_line_width(width) gc.set_line_cap(line_cap) gc.set_line_join(line_join) gc.stroke_path() return gc
def test_alias_cap_round(self): """ Round caps should extend beyond the end of the line. We don't really test the shape here. To do this, a test of a wider line would be needed. fix me: This is rendering antialiased end points currently. """ gc = GraphicsContextArray((6, 6), pix_format="rgb24") # clear background to white values (255, 255, 255) gc.clear((1.0, 1.0, 1.0)) # single horizontal line across bottom of buffer gc.move_to(2, 3) gc.line_to(4, 3) # Set up line gc.set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(False) gc.set_line_width(2) gc.set_line_cap(kiva.CAP_ROUND) gc.set_line_join(kiva.JOIN_MITER) gc.stroke_path() desired = array( ((255, 255, 255, 255, 255, 255), (255, 255, 255, 255, 255, 255), (255, 0, 0, 0, 0, 255), (255, 0, 0, 0, 0, 255), (255, 255, 255, 255, 255, 255), (255, 255, 255, 255, 255, 255))) actual = gc.bmp_array[:, :, 0] self.assertRavelEqual(desired, actual)
from __future__ import print_function import time from kiva.fonttools import Font from kiva.constants import MODERN from kiva.agg import AffineMatrix, GraphicsContextArray gc = GraphicsContextArray((200,200)) font = Font(family=MODERN) #print font.size font.size=8 gc.set_font(font) t1 = time.clock() # consecutive printing of text. with gc: gc.set_antialias(False) gc.set_fill_color((0,1,0)) gc.translate_ctm(50,50) gc.rotate_ctm(3.1416/4) gc.show_text("hello") gc.translate_ctm(-50,-50) gc.set_text_matrix(AffineMatrix()) gc.set_fill_color((0,1,1)) gc.show_text("hello") t2 = time.clock() print('aliased:', t2 - t1)
def test_show_text_at_point(self): gc = GraphicsContextArray((100,100)) gc.set_font(Font()) gc.show_text_at_point(six.text_type('asdf'), 5,5)