Skip to content

Commit

Permalink
Applied patch by James Evans for better C++/C compliance (for MSVC an…
Browse files Browse the repository at this point in the history
…d g++ --pedantic). -ADS

svn path=/trunk/matplotlib/; revision=2141
  • Loading branch information
astraw committed Mar 12, 2006
1 parent 7a28d0f commit 953128b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 42 deletions.
98 changes: 59 additions & 39 deletions src/_backend_agg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,10 @@ RendererAgg::draw_polygon(const Py::Tuple& args) {
return Py::Object();


// dump the x.y vertices into a double array for faster look ahread
// dump the x.y vertices into a double array for faster look ahead
// and behind access
double xs[Npoints];
double ys[Npoints];
double *xs = new double[Npoints];
double *ys = new double[Npoints];

for (size_t i=0; i<Npoints; i++) {
Py::SeqBase<Py::Object> xy(points[i]);
Expand All @@ -406,6 +406,10 @@ RendererAgg::draw_polygon(const Py::Tuple& args) {
path.close_polygon();

_fill_and_stroke(path, gc, face, false);

delete [] xs;
delete [] ys;

_VERBOSE("RendererAgg::draw_polygon DONE");
return Py::Object();

Expand Down Expand Up @@ -960,10 +964,10 @@ RendererAgg::draw_poly_collection(const Py::Tuple& args) {
double xo = Py::Float(pos[0]);
double yo = Py::Float(pos[1]);
try {
xyo = transOffset->operator()(xo, yo);
xyo = transOffset->operator()(xo, yo);
}
catch (...) {
throw Py::ValueError("Domain error on transOffset->operator in draw_line_collection");
throw Py::ValueError("Domain error on transOffset->operator in draw_line_collection");
}

}
Expand All @@ -976,22 +980,25 @@ RendererAgg::draw_poly_collection(const Py::Tuple& args) {

// dump the verts to double arrays so we can do more efficient
// look aheads and behinds when doing snapto pixels
double xs[Nverts], ys[Nverts];
double *xs = new double[Nverts];
double *ys = new double[Nverts];
for (size_t j=0; j<Nverts; j++) {
thisvert = thisverts[j];
double x = Py::Float(thisvert[0]);
double y = Py::Float(thisvert[1]);
try {
xy = transform->operator()(x, y);
xy = transform->operator()(x, y);
}
catch(...) {
throw Py::ValueError("Domain error on eval_scalars in RendererAgg::draw_poly_collection");
delete [] xs;
delete [] ys;
throw Py::ValueError("Domain error on eval_scalars in RendererAgg::draw_poly_collection");
}


if (usingOffsets) {
xy.first += xyo.first;
xy.second += xyo.second;
xy.first += xyo.first;
xy.second += xyo.second;
}

xy.second = height - xy.second;
Expand All @@ -1006,21 +1013,21 @@ RendererAgg::draw_poly_collection(const Py::Tuple& args) {
double y = ys[j];

if (j==0) {
if (xs[j] == xs[Nverts-1]) x = (int)xs[j] + 0.5;
if (ys[j] == ys[Nverts-1]) y = (int)ys[j] + 0.5;
if (xs[j] == xs[Nverts-1]) x = (int)xs[j] + 0.5;
if (ys[j] == ys[Nverts-1]) y = (int)ys[j] + 0.5;
}
else if (j==Nverts-1) {
if (xs[j] == xs[0]) x = (int)xs[j] + 0.5;
if (ys[j] == ys[0]) y = (int)ys[j] + 0.5;
if (xs[j] == xs[0]) x = (int)xs[j] + 0.5;
if (ys[j] == ys[0]) y = (int)ys[j] + 0.5;
}

if (j < Nverts-1) {
if (xs[j] == xs[j+1]) x = (int)xs[j] + 0.5;
if (ys[j] == ys[j+1]) y = (int)ys[j] + 0.5;
if (xs[j] == xs[j+1]) x = (int)xs[j] + 0.5;
if (ys[j] == ys[j+1]) y = (int)ys[j] + 0.5;
}
if (j>0) {
if (xs[j] == xs[j-1]) x = (int)xs[j] + 0.5;
if (ys[j] == ys[j-1]) y = (int)ys[j] + 0.5;
if (xs[j] == xs[j-1]) x = (int)xs[j] + 0.5;
if (ys[j] == ys[j-1]) y = (int)ys[j] + 0.5;
}

if (j==0) path.move_to(x,y);
Expand All @@ -1041,12 +1048,12 @@ RendererAgg::draw_poly_collection(const Py::Tuple& args) {
theRasterizer->add_path(path);

if (isaa) {
rendererAA->color(facecolor);
agg::render_scanlines(*theRasterizer, *slineP8, *rendererAA);
rendererAA->color(facecolor);
agg::render_scanlines(*theRasterizer, *slineP8, *rendererAA);
}
else {
rendererBin->color(facecolor);
agg::render_scanlines(*theRasterizer, *slineBin, *rendererBin);
rendererBin->color(facecolor);
agg::render_scanlines(*theRasterizer, *slineBin, *rendererBin);
}
} //renderer face

Expand All @@ -1069,15 +1076,18 @@ RendererAgg::draw_poly_collection(const Py::Tuple& args) {

// render antialiased or not
if ( isaa ) {
rendererAA->color(edgecolor);
agg::render_scanlines(*theRasterizer, *slineP8, *rendererAA);
rendererAA->color(edgecolor);
agg::render_scanlines(*theRasterizer, *slineP8, *rendererAA);
}
else {
rendererBin->color(edgecolor);
agg::render_scanlines(*theRasterizer, *slineBin, *rendererBin);
rendererBin->color(edgecolor);
agg::render_scanlines(*theRasterizer, *slineBin, *rendererBin);
}
} //rendered edge

delete [] xs;
delete [] ys;

} // for every poly
return Py::Object();
}
Expand Down Expand Up @@ -1127,8 +1137,8 @@ RendererAgg::draw_regpoly_collection(const Py::Tuple& args) {
double thisx, thisy;

// dump the x.y vertices into a double array for faster access
double xverts[Nverts];
double yverts[Nverts];
double *xverts = new double[Nverts];
double *yverts = new double[Nverts];
Py::SeqBase<Py::Object> xy;
for (size_t i=0; i<Nverts; i++) {
xy = Py::SeqBase<Py::Object>(verts[i]);
Expand All @@ -1145,6 +1155,8 @@ RendererAgg::draw_regpoly_collection(const Py::Tuple& args) {
offsetPair = transOffset->operator()(xo, yo);
}
catch(...) {
delete [] xverts;
delete [] yverts;
throw Py::ValueError("Domain error on eval_scalars in RendererAgg::draw_regpoly_collection");
}

Expand Down Expand Up @@ -1178,12 +1190,12 @@ RendererAgg::draw_regpoly_collection(const Py::Tuple& args) {
theRasterizer->add_path(path);

if (isaa) {
rendererAA->color(facecolor);
agg::render_scanlines(*theRasterizer, *slineP8, *rendererAA);
rendererAA->color(facecolor);
agg::render_scanlines(*theRasterizer, *slineP8, *rendererAA);
}
else {
rendererBin->color(facecolor);
agg::render_scanlines(*theRasterizer, *slineBin, *rendererBin);
rendererBin->color(facecolor);
agg::render_scanlines(*theRasterizer, *slineBin, *rendererBin);
}
} //renderer face

Expand All @@ -1205,16 +1217,18 @@ RendererAgg::draw_regpoly_collection(const Py::Tuple& args) {

// render antialiased or not
if ( isaa ) {
rendererAA->color(edgecolor);
agg::render_scanlines(*theRasterizer, *slineP8, *rendererAA);
rendererAA->color(edgecolor);
agg::render_scanlines(*theRasterizer, *slineP8, *rendererAA);
}
else {
rendererBin->color(edgecolor);
agg::render_scanlines(*theRasterizer, *slineBin, *rendererBin);
rendererBin->color(edgecolor);
agg::render_scanlines(*theRasterizer, *slineBin, *rendererBin);
}
} //rendered edge

} // for every poly
delete [] xverts;
delete [] yverts;
return Py::Object();
}

Expand Down Expand Up @@ -1355,7 +1369,7 @@ RendererAgg::_render_lines_path(PathSource &path, const GCAgg& gc) {

//path_t transpath(path, xytrans);

if (gc.dasha==NULL ) { //no dashes
if (gc.dasha==NULL ) { //no dashes
stroke_t stroke(path);
stroke.width(gc.linewidth);
stroke.line_cap(gc.cap);
Expand Down Expand Up @@ -1920,32 +1934,37 @@ RendererAgg::write_png(const Py::Tuple& args)
struct png_color_8_struct sig_bit;
png_uint_32 row;

png_bytep row_pointers[height];
png_bytep *row_pointers = new png_bytep[height];
for (row = 0; row < height; ++row) {
row_pointers[row] = pixBuffer + row * width * 4;
}


if (fp == NULL)
if (fp == NULL) {
delete [] row_pointers;
throw Py::RuntimeError("Could not open file");
}


png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
if (fpclose) fclose(fp);
delete [] row_pointers;
throw Py::RuntimeError("Could not create write struct");
}

info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
if (fpclose) fclose(fp);
png_destroy_write_struct(&png_ptr, &info_ptr);
delete [] row_pointers;
throw Py::RuntimeError("Could not create info struct");
}

if (setjmp(png_ptr->jmpbuf)) {
if (fpclose) fclose(fp);
png_destroy_write_struct(&png_ptr, &info_ptr);
delete [] row_pointers;
throw Py::RuntimeError("Error building image");
}

Expand Down Expand Up @@ -1975,6 +1994,7 @@ RendererAgg::write_png(const Py::Tuple& args)

png_destroy_write_struct(&png_ptr, &info_ptr);

delete [] row_pointers;

if (fpclose) fclose(fp);

Expand Down
12 changes: 10 additions & 2 deletions src/_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,13 +629,15 @@ Image::write_png(const Py::Tuple& args)
png_uint_32 row=0;

//todo: allocate on heap
png_bytep row_pointers[rowsOut];
png_bytep *row_pointers = new png_bytep[rowsOut];

for (row = 0; row < rowsOut; ++row)
row_pointers[row] = bufpair.first + row * colsOut * 4;

fp = fopen(file_name, "wb");
if (fp == NULL) {
if (bufpair.second) delete [] bufpair.first;
delete [] row_pointers;
throw Py::RuntimeError(Printf("Could not open file %s", file_name).str());
}

Expand All @@ -644,6 +646,7 @@ Image::write_png(const Py::Tuple& args)
if (png_ptr == NULL) {
if (bufpair.second) delete [] bufpair.first;
fclose(fp);
delete [] row_pointers;
throw Py::RuntimeError("Could not create write struct");
}

Expand All @@ -652,13 +655,15 @@ Image::write_png(const Py::Tuple& args)
if (bufpair.second) delete [] bufpair.first;
fclose(fp);
png_destroy_write_struct(&png_ptr, &info_ptr);
delete [] row_pointers;
throw Py::RuntimeError("Could not create info struct");
}

if (setjmp(png_ptr->jmpbuf)) {
if (bufpair.second) delete [] bufpair.first;
fclose(fp);
png_destroy_write_struct(&png_ptr, &info_ptr);
delete [] row_pointers;
throw Py::RuntimeError("Error building image");
}

Expand All @@ -683,6 +688,8 @@ Image::write_png(const Py::Tuple& args)
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);

delete [] row_pointers;

if (bufpair.second) delete [] bufpair.first;
return Py::Object();
}
Expand Down Expand Up @@ -886,7 +893,7 @@ _image_module::readpng(const Py::Tuple& args) {
if (setjmp(png_jmpbuf(png_ptr)))
throw Py::RuntimeError("_image_module::readpng: error during read_image");

png_bytep row_pointers[height];
png_bytep *row_pointers = new png_bytep[height];

for (png_uint_32 row = 0; row < height; row++)
row_pointers[row] = new png_byte[png_get_rowbytes(png_ptr,info_ptr)];
Expand Down Expand Up @@ -923,6 +930,7 @@ _image_module::readpng(const Py::Tuple& args) {
fclose(fp);
for (png_uint_32 row = 0; row < height; row++)
delete [] row_pointers[row];
delete [] row_pointers;
return Py::asObject((PyObject*)A);
}

Expand Down
2 changes: 1 addition & 1 deletion src/cntr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1548,13 +1548,13 @@ Cntr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static int
Cntr_init(Cntr *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"x", "y", "z", "mask", NULL};
PyObject *xarg, *yarg, *zarg, *marg;
PyArrayObject *xpa, *ypa, *zpa, *mpa;
long iMax, jMax;
char *mask;

marg = NULL;
static char *kwlist[] = {"x", "y", "z", "mask", NULL};

if (! PyArg_ParseTupleAndKeywords(args, kwds, "OOO|O", kwlist,
&xarg, &yarg, &zarg, &marg))
Expand Down

0 comments on commit 953128b

Please sign in to comment.