Skip to content

Commit

Permalink
added basic image support to agg
Browse files Browse the repository at this point in the history
svn path=/trunk/matplotlib/; revision=177
  • Loading branch information
jdh2358 committed Mar 12, 2004
1 parent 5ead3e0 commit d0f4115
Show file tree
Hide file tree
Showing 13 changed files with 655 additions and 50 deletions.
5 changes: 3 additions & 2 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ GOALS
INSTALL
INTERACTIVE
KNOWN_BUGS
LICENSE
LICENSE_PAINT
MANIFEST
MANIFEST.in
Makefile
Expand Down Expand Up @@ -384,6 +382,9 @@ images/stock_zoom-in.ppm
images/stock_zoom-in.xpm
images/stock_zoom-out.ppm
images/stock_zoom-out.xpm
license/LICENSE
license/LICENSE_PAINT
license/LICENSE_PIL
matplotlib/__init__.py
matplotlib/_matlab_helpers.py
matplotlib/afm.py
Expand Down
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include LICENSE LICENSE_PAINT API_CHANGES CHANGELOG KNOWN_BUGS GOALS INSTALL
include API_CHANGES CHANGELOG KNOWN_BUGS GOALS INSTALL
include INTERACTIVE TODO
include Makefile MANIFEST.in MANIFEST
include .matplotlibrc
Expand All @@ -8,6 +8,7 @@ include images/*
include test/*.py
include test/README
recursive-include fonts *.ttf *.afm *.TXT
recursive-include license LICENSE*
recursive-include examples README *.py *.glade
prune examples/_tmp_*
recursive-include src *.cpp *.c *.h
Expand Down
6 changes: 5 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,8 @@

-- DONE 2004-03-02 - why are aliased lines of varying widths on agg?

-- update dynamic_demo_wx
-- DONE update dynamic_demo_wx

-- DONE add figure legend

-- fix newlines across backends
Binary file added examples/data/ct.raw
Binary file not shown.
20 changes: 20 additions & 0 deletions examples/image_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from matplotlib.image import ASPECT_FREE, ASPECT_PRESERVE
from matplotlib.image import NEAREST, BILINEAR, BICUBIC, SPLINE16
from matplotlib.matlab import *


s = file('data/ct.raw', 'rb').read()
A = fromstring(s, typecode=UInt16).astype(Float)
A *= 1.0/max(A)
A.shape = 512, 512

im = imshow(A)
#im.set_interpolation(BICUBIC)
#im.set_interpolation(NEAREST)
im.set_interpolation(BILINEAR)
#im.set_preserve_aspect(ASPECT_PRESERVE)



show()

2 changes: 1 addition & 1 deletion examples/mathtext_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
tex = r'$\cal{R}\prod_{i=\alpha_{i+1}}^\infty a_i\rm{sin}(2 \pi f x_i)$'
#tex = r'$\alpha\beta\gamma$'
text(1, 2.6, tex, fontsize=20)
title(r'$\Delta_i \rm{versus} \Delta_{i+1}$', fontsize=15)
title(r'$\Delta_i\rm{ versus }\Delta_{i+1}$', fontsize=15)
#savefig('mathtext_demo', dpi=100)
show()
2 changes: 1 addition & 1 deletion examples/object_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def set_color(button):
marker = line.get_marker()
if marker is None: marker = 'None'
styles = [marker]
for key in lineMarkers.keys():
for key in keys:
if key == marker: continue
styles.append(key)

Expand Down
2 changes: 1 addition & 1 deletion examples/simple_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
figure(1)
t = arange(0.0, 1.0, 0.01)
s = sin(2*2*pi*t)
plot(t, s, linewidth=1.0)
plot(t, s, antialiased=False)

xlabel('time (s)')
ylabel('voltage (mV)')
Expand Down
11 changes: 9 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# Requires freetype2, and libz
BUILD_FT2FONT = 1

# build the image support module - requires agg
BUILD_IMAGE = 1

# Build the fonttools and TTFQuery packages, required by the Paint,
# Agg and GD backends.
BUILD_FONTTOOLS = 1
Expand Down Expand Up @@ -37,7 +40,7 @@
import sys,os
import glob
from setupext import build_gtkgd, build_agg, build_fonttools, build_gtkagg, \
build_tkagg, build_ft2font
build_tkagg, build_ft2font, build_image
import distutils.sysconfig

data = []
Expand Down Expand Up @@ -77,6 +80,10 @@
BUILD_FONTTOOLS = 1
build_ft2font(ext_modules, packages)

if BUILD_IMAGE:
BUILD_IMAGE = 1
build_image(ext_modules, packages)

if BUILD_FONTTOOLS:
build_fonttools(ext_modules, packages)
# we need to manually install FontTools.pth since we can't use
Expand All @@ -92,7 +99,7 @@


setup(name="matplotlib",
version= '0.51.1',
version= '0.52b',
description = "Matlab style python plotting package",
author = "John D. Hunter",
author_email="jdhunter@ace.bsd.uchicago.edu",
Expand Down
31 changes: 25 additions & 6 deletions setupext.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
If you are sufficiently masochistic that you want to build this
yourself, contact me and I'll send you win32_static as a zip file.
python setup.py build --compiler=mingw32 bdist_wininst --install-script postinstall.py > build23.out
> python setup.py build --compiler=mingw32 bdist_wininst --install-script postinstall.py > build23.out
"""

Expand All @@ -49,10 +49,11 @@

BUILT_AGG = False
BUILT_FONTTOOLS = False
BUILT_GTKGD = False
BUILT_FT2FONT = False
BUILT_GTKAGG = False
BUILT_GTKGD = False
BUILT_IMAGE = False
BUILT_TKAGG = False
BUILT_FT2FONT = False

def getoutput(s):
'get the output of a system command'
Expand All @@ -67,8 +68,6 @@ def add_agg_flags(module):
# before adding the freetype flags since -z comes later
module.libraries.append('png')

add_ft2font_flags(module)

module.include_dirs.extend(['src','agg2/include'])

# put these later for correct link order
Expand Down Expand Up @@ -181,7 +180,8 @@ def build_gtkagg(ext_modules, packages):

# add agg flags before pygtk because agg only supports freetype1
# and pygtk includes freetype2. This is a bit fragile.


add_ft2font_flags(module)
add_agg_flags(module)
add_pygtk_flags(module)

Expand All @@ -201,7 +201,9 @@ def build_tkagg(ext_modules, packages):
# add agg flags before pygtk because agg only supports freetype1
# and pygtk includes freetype2. This is a bit fragile.


add_tk_flags(module) # do this first
add_ft2font_flags(module)
add_agg_flags(module)


Expand All @@ -221,9 +223,26 @@ def build_agg(ext_modules, packages):
deps
,
)
add_ft2font_flags(module)
add_agg_flags(module)
ext_modules.append(module)
BUILT_AGG = True

def build_image(ext_modules, packages):
global BUILT_IMAGE
if BUILT_IMAGE: return # only build it if you you haven't already

deps = ['src/image.cpp']
deps.extend(glob.glob('agg2/src/*.cpp'))

module = Extension(
'matplotlib.image',
deps
,
)
add_agg_flags(module)
ext_modules.append(module)
BUILT_IMAGE = True

def build_fonttools(ext_modules, packages):

Expand Down
65 changes: 30 additions & 35 deletions src/_backend_agg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <png.h>
#include "ft2font.h"
#include "_backend_agg.h"
#include "_image.h"

static PyObject *ErrorObject;

Expand Down Expand Up @@ -371,7 +372,7 @@ RendererAgg_dealloc(RendererAggObject *self)
delete self->ren;

delete self->ras;
delete self->buffer;
delete [] self->buffer;
}


Expand Down Expand Up @@ -561,6 +562,27 @@ RendererAgg_draw_rectangle(RendererAggObject *renderer, PyObject* args) {

}

char RendererAgg_draw_image__doc__[] =
"draw_image(x, y, image)\n"
"\n"
"Render the Image instance into the buffer, "
"starting at pixel locations x,y"
;
PyObject *
RendererAgg_draw_image(RendererAggObject *renderer, PyObject* args) {

ImageObject *image;
long x, y;
if (!PyArg_ParseTuple(args, "iiO", &x, &y, &image))
return NULL;

//todo: handle x and y
renderer->rbuf->copy_from(*image->rbufOut);
Py_INCREF(Py_None);
return Py_None;

}


static PyObject *
RendererAgg_draw_lines(RendererAggObject *renderer, PyObject* args) {
Expand Down Expand Up @@ -639,10 +661,12 @@ RendererAgg_draw_lines(RendererAggObject *renderer, PyObject* args) {

double thisX, thisY;
unsigned winHeight = renderer->rbase->height();
int antialiased = _gc_antialiased(gc);

if (Nx==2) {
// this is a little hack - len(2) lines are probably grid and
// ticks so I'm going to snap to pixel
//printf("snapto %d\n", Nx);
thisX = (int)(_seqitem_as_double(x, 0))+0.5;
thisY = (int)(winHeight - _seqitem_as_double(y, 0))+0.5;
path.move_to(thisX, thisY);
Expand Down Expand Up @@ -709,8 +733,8 @@ RendererAgg_draw_lines(RendererAggObject *renderer, PyObject* args) {
Py_XDECREF(dashSeq);
}

if ( _gc_antialiased(gc) ) {

if ( antialiased ) {
//if ( 0 ) {
renderer->ren->color(*color);
renderer->ras->render(*renderer->sline_p8, *renderer->ren);
Expand All @@ -734,36 +758,6 @@ RendererAgg_draw_lines(RendererAggObject *renderer, PyObject* args) {



static char RendererAgg_rgb__doc__[] =
"rgb(r, g, b)\n"
"\n"
"Create an rgba color value with a = 0xff.";

static PyObject *RendererAgg_rgb(PyObject *self, PyObject *args)
{
int r, g, b;

if (!PyArg_ParseTuple(args, "iii", &r, &g, &b))
return NULL;

return (PyObject*)PyInt_FromLong((r << 24) + (g << 16) + (b << 8) + 0xff);
}

static char RendererAgg_rgba__doc__[] =
"rgba(r, g, b, a)\n"
"\n"
"Create an rgba color value.";

static PyObject *RendererAgg_rgba(PyObject *self, PyObject *args)
{
int r, g, b, a;

if (!PyArg_ParseTuple(args, "iiii", &r, &g, &b, &a))
return NULL;

return (PyObject*)PyInt_FromLong((r << 24) + (g << 16) + (b << 8) + a);
}

char RendererAgg_draw_text__doc__[] =
"draw_text(font, x, y, rgba)\n"
"\n"
Expand Down Expand Up @@ -813,6 +807,7 @@ RendererAgg_draw_text(RendererAggObject *renderer, PyObject* args) {

}


static PyObject *
RendererAgg_write_rgba(RendererAggObject *renderer, PyObject* args) {

Expand Down Expand Up @@ -920,12 +915,12 @@ static PyMethodDef RendererAgg_methods[] = {
{ "draw_ellipse", (PyCFunction)RendererAgg_draw_ellipse, METH_VARARGS},
{ "draw_rectangle", (PyCFunction)RendererAgg_draw_rectangle, METH_VARARGS},
{ "draw_polygon", (PyCFunction)RendererAgg_draw_polygon, METH_VARARGS},
{ "draw_image", (PyCFunction)RendererAgg_draw_image, METH_VARARGS, RendererAgg_draw_image__doc__},
{ "draw_lines", (PyCFunction)RendererAgg_draw_lines, METH_VARARGS},

{ "draw_text", (PyCFunction)RendererAgg_draw_text, METH_VARARGS, RendererAgg_draw_text__doc__},
{ "write_rgba", (PyCFunction)RendererAgg_write_rgba, METH_VARARGS},
{ "write_png", (PyCFunction)RendererAgg_write_png, METH_VARARGS},
{ "rgb", (PyCFunction)RendererAgg_rgb, METH_VARARGS, RendererAgg_rgb__doc__ },
{ "rgba", (PyCFunction)RendererAgg_rgba, METH_VARARGS, RendererAgg_rgba__doc__ },

{NULL, NULL} /* sentinel */
};
Expand Down
Loading

0 comments on commit d0f4115

Please sign in to comment.