Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 19b7e10

Browse files
committed
Optimize C code
1 parent 78f9c08 commit 19b7e10

File tree

5 files changed

+51
-37
lines changed

5 files changed

+51
-37
lines changed

lib/matplotlib/tests/test_axes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4930,8 +4930,11 @@ def test_lines_with_colors(fig_test, fig_ref, data):
49304930
colors=expect_color, linewidth=5)
49314931

49324932

4933-
@image_comparison(['step_linestyle', 'step_linestyle'], remove_text=True)
4933+
@image_comparison(['step_linestyle', 'step_linestyle'], remove_text=True,
4934+
tol=0.2)
49344935
def test_step_linestyle():
4936+
# Tolerance caused by reordering of floating-point operations
4937+
# Remove when regenerating the images
49354938
x = y = np.arange(10)
49364939

49374940
# First illustrate basic pyplot interface, using defaults where possible.

lib/matplotlib/tests/test_lines.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ def test_invalid_line_data():
9797
line.set_ydata(0)
9898

9999

100-
@image_comparison(['line_dashes'], remove_text=True)
100+
@image_comparison(['line_dashes'], remove_text=True, tol=0.002)
101101
def test_line_dashes():
102+
# Tolerance introduced after reordering of floating-point operations
103+
# Remove when regenerating the images
102104
fig, ax = plt.subplots()
103105

104106
ax.plot(range(10), linestyle=(0, (3, 3)), lw=5)

src/_backend_agg.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ inline void RendererAgg::draw_text_image(GCAgg &gc, ImageArray &image, int x, in
745745

746746
agg::trans_affine mtx;
747747
mtx *= agg::trans_affine_translation(0, -image.dim(0));
748-
mtx *= agg::trans_affine_rotation(-angle * agg::pi / 180.0);
748+
mtx *= agg::trans_affine_rotation(-angle * (agg::pi / 180.0));
749749
mtx *= agg::trans_affine_translation(x, y);
750750

751751
agg::path_storage rect;
@@ -773,8 +773,10 @@ inline void RendererAgg::draw_text_image(GCAgg &gc, ImageArray &image, int x, in
773773
} else {
774774
agg::rect_i fig, text;
775775

776+
int deltay = y - image.dim(0);
777+
776778
fig.init(0, 0, width, height);
777-
text.init(x, y - image.dim(0), x + image.dim(1), y);
779+
text.init(x, deltay, x + image.dim(1), y);
778780
text.clip(fig);
779781

780782
if (gc.cliprect.x1 != 0.0 || gc.cliprect.y1 != 0.0 || gc.cliprect.x2 != 0.0 || gc.cliprect.y2 != 0.0) {
@@ -788,9 +790,11 @@ inline void RendererAgg::draw_text_image(GCAgg &gc, ImageArray &image, int x, in
788790
}
789791

790792
if (text.x2 > text.x1) {
793+
int deltax = text.x2 - text.x1;
794+
int deltax2 = text.x1 - x;
791795
for (int yi = text.y1; yi < text.y2; ++yi) {
792-
pixFmt.blend_solid_hspan(text.x1, yi, (text.x2 - text.x1), gc.color,
793-
&image(yi - (y - image.dim(0)), text.x1 - x));
796+
pixFmt.blend_solid_hspan(text.x1, yi, deltax, gc.color,
797+
&image(yi - deltay, deltax2));
794798
}
795799
}
796800
}
@@ -945,6 +949,7 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc,
945949
facepair_t face;
946950
face.first = Nfacecolors != 0;
947951
agg::trans_affine trans;
952+
bool do_clip = !face.first && !gc.has_hatchpath();
948953

949954
for (int i = 0; i < (int)N; ++i) {
950955
typename PathGenerator::path_iterator path = path_generator(i);
@@ -992,8 +997,6 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc,
992997
}
993998
}
994999

995-
bool do_clip = !face.first && !gc.has_hatchpath();
996-
9971000
if (check_snap) {
9981001
gc.isaa = antialiaseds(i % Naa);
9991002

src/_backend_agg_basic_types.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,19 @@ class Dashes
5252
template <class T>
5353
void dash_to_stroke(T &stroke, double dpi, bool isaa)
5454
{
55+
double scaleddpi = dpi / 72.0;
5556
for (dash_t::const_iterator i = dashes.begin(); i != dashes.end(); ++i) {
5657
double val0 = i->first;
5758
double val1 = i->second;
58-
val0 = val0 * dpi / 72.0;
59-
val1 = val1 * dpi / 72.0;
59+
val0 = val0 * scaleddpi;
60+
val1 = val1 * scaleddpi;
6061
if (!isaa) {
6162
val0 = (int)val0 + 0.5;
6263
val1 = (int)val1 + 0.5;
6364
}
6465
stroke.add_dash(val0, val1);
6566
}
66-
stroke.dash_start(get_dash_offset() * dpi / 72.0);
67+
stroke.dash_start(get_dash_offset() * scaleddpi);
6768
}
6869
};
6970

src/ft2font.cpp

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,9 @@ void FT2Image::draw_rect(unsigned long x0, unsigned long y0, unsigned long x1, u
160160
}
161161

162162
for (size_t j = y0 + 1; j < y1; ++j) {
163-
m_buffer[x0 + j * m_width] = 255;
164-
m_buffer[x1 + j * m_width] = 255;
163+
size_t offset = j * m_width;
164+
m_buffer[x0 + offset] = 255;
165+
m_buffer[x1 + offset] = 255;
165166
}
166167

167168
m_dirty = true;
@@ -176,8 +177,9 @@ FT2Image::draw_rect_filled(unsigned long x0, unsigned long y0, unsigned long x1,
176177
y1 = std::min(y1 + 1, m_height);
177178

178179
for (size_t j = y0; j < y1; j++) {
180+
size_t offset = j * m_width;
179181
for (size_t i = x0; i < x1; i++) {
180-
m_buffer[i + j * m_width] = 255;
182+
m_buffer[i + offset] = 255;
181183
}
182184
}
183185

@@ -236,8 +238,8 @@ ft_outline_move_to(FT_Vector const* to, void* user)
236238
*(d->vertices++) = 0;
237239
*(d->codes++) = CLOSEPOLY;
238240
}
239-
*(d->vertices++) = to->x / 64.;
240-
*(d->vertices++) = to->y / 64.;
241+
*(d->vertices++) = to->x * (1. / 64.);
242+
*(d->vertices++) = to->y * (1. / 64.);
241243
*(d->codes++) = MOVETO;
242244
}
243245
d->index += d->index ? 2 : 1;
@@ -249,8 +251,8 @@ ft_outline_line_to(FT_Vector const* to, void* user)
249251
{
250252
ft_outline_decomposer* d = reinterpret_cast<ft_outline_decomposer*>(user);
251253
if (d->codes) {
252-
*(d->vertices++) = to->x / 64.;
253-
*(d->vertices++) = to->y / 64.;
254+
*(d->vertices++) = to->x * (1. / 64.);
255+
*(d->vertices++) = to->y * (1. / 64.);
254256
*(d->codes++) = LINETO;
255257
}
256258
d->index++;
@@ -262,10 +264,10 @@ ft_outline_conic_to(FT_Vector const* control, FT_Vector const* to, void* user)
262264
{
263265
ft_outline_decomposer* d = reinterpret_cast<ft_outline_decomposer*>(user);
264266
if (d->codes) {
265-
*(d->vertices++) = control->x / 64.;
266-
*(d->vertices++) = control->y / 64.;
267-
*(d->vertices++) = to->x / 64.;
268-
*(d->vertices++) = to->y / 64.;
267+
*(d->vertices++) = control->x * (1. / 64.);
268+
*(d->vertices++) = control->y * (1. / 64.);
269+
*(d->vertices++) = to->x * (1. / 64.);
270+
*(d->vertices++) = to->y * (1. / 64.);
269271
*(d->codes++) = CURVE3;
270272
*(d->codes++) = CURVE3;
271273
}
@@ -279,12 +281,12 @@ ft_outline_cubic_to(
279281
{
280282
ft_outline_decomposer* d = reinterpret_cast<ft_outline_decomposer*>(user);
281283
if (d->codes) {
282-
*(d->vertices++) = c1->x / 64.;
283-
*(d->vertices++) = c1->y / 64.;
284-
*(d->vertices++) = c2->x / 64.;
285-
*(d->vertices++) = c2->y / 64.;
286-
*(d->vertices++) = to->x / 64.;
287-
*(d->vertices++) = to->y / 64.;
284+
*(d->vertices++) = c1->x * (1. / 64.);
285+
*(d->vertices++) = c1->y * (1. / 64.);
286+
*(d->vertices++) = c2->x * (1. / 64.);
287+
*(d->vertices++) = c2->y * (1. / 64.);
288+
*(d->vertices++) = to->x * (1. / 64.);
289+
*(d->vertices++) = to->y * (1. / 64.);
288290
*(d->codes++) = CURVE4;
289291
*(d->codes++) = CURVE4;
290292
*(d->codes++) = CURVE4;
@@ -485,13 +487,16 @@ void FT2Font::set_text(
485487
{
486488
FT_Matrix matrix; /* transformation matrix */
487489

488-
angle = angle / 360.0 * 2 * M_PI;
490+
angle = angle * (2 * M_PI / 360.0);
489491

490492
// this computes width and height in subpixels so we have to multiply by 64
491-
matrix.xx = (FT_Fixed)(cos(angle) * 0x10000L);
492-
matrix.xy = (FT_Fixed)(-sin(angle) * 0x10000L);
493-
matrix.yx = (FT_Fixed)(sin(angle) * 0x10000L);
494-
matrix.yy = (FT_Fixed)(cos(angle) * 0x10000L);
493+
FT_Fixed ftcosangle = (FT_Fixed)(cos(angle) * 64.0);
494+
double sinangle = sin(angle) * 64.0;
495+
496+
matrix.xx = ftcosangle;
497+
matrix.xy = (FT_Fixed)(-sinangle);
498+
matrix.yx = (FT_Fixed)(sinangle);
499+
matrix.yy = ftcosangle;
495500

496501
clear();
497502

@@ -762,8 +767,8 @@ void FT2Font::draw_glyphs_to_bitmap(bool antialiased)
762767
// now, draw to our target surface (convert position)
763768

764769
// bitmap left and top in pixel, string bbox in subpixel
765-
FT_Int x = (FT_Int)(bitmap->left - (bbox.xMin / 64.));
766-
FT_Int y = (FT_Int)((bbox.yMax / 64.) - bitmap->top + 1);
770+
FT_Int x = (FT_Int)(bitmap->left - (bbox.xMin * (1. / 64.)));
771+
FT_Int y = (FT_Int)((bbox.yMax * (1. / 64.)) - bitmap->top + 1);
767772

768773
image.draw_bitmap(&bitmap->bitmap, x, y);
769774
}
@@ -782,8 +787,8 @@ void FT2Font::get_xys(bool antialiased, std::vector<double> &xys)
782787
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[n];
783788

784789
// bitmap left and top in pixel, string bbox in subpixel
785-
FT_Int x = (FT_Int)(bitmap->left - bbox.xMin / 64.);
786-
FT_Int y = (FT_Int)(bbox.yMax / 64. - bitmap->top + 1);
790+
FT_Int x = (FT_Int)(bitmap->left - bbox.xMin * (1. / 64.));
791+
FT_Int y = (FT_Int)(bbox.yMax * (1. / 64.) - bitmap->top + 1);
787792
// make sure the index is non-neg
788793
x = x < 0 ? 0 : x;
789794
y = y < 0 ? 0 : y;

0 commit comments

Comments
 (0)