Skip to content

Commit df35fa3

Browse files
committed
Test the PS backend in the regression test framework.
Also fixes a bug in the ps backend found by doing so.
1 parent 735df8d commit df35fa3

File tree

6 files changed

+35
-13
lines changed

6 files changed

+35
-13
lines changed

lib/matplotlib/testing/compare.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ def convert(filename):
132132
newname = base + '_' + extension + '.png'
133133
if not os.path.exists(filename):
134134
raise IOError, "'%s' does not exist" % filename
135-
converter[extension](filename, newname)
135+
# Only convert the file if the destination doesn't already exist or
136+
# is out of date.
137+
if (not os.path.exists(newname) or
138+
os.stat(newname).st_mtime < os.stat(filename).st_mtime):
139+
converter[extension](filename, newname)
136140
return newname
137141

138142
verifiers = { }
@@ -163,6 +167,15 @@ def verify(filename):
163167
verifiers['svg'] = lambda filename: [
164168
'xmllint', '--valid', '--nowarning', '--noout', filename]
165169

170+
def crop_to_same(actual_path, actual_image, expected_path, expected_image):
171+
# clip the images to the same size -- this is useful only when
172+
# comparing eps to pdf
173+
if actual_path[-7:-4] == 'eps' and expected_path[-7:-4] == 'pdf':
174+
aw, ah = actual_image.size
175+
ew, eh = expected_image.size
176+
actual_image = actual_image.crop((aw/2-ew/2, ah/2-eh/2, aw/2+ew/2, ah/2+eh/2))
177+
return actual_image, expected_image
178+
166179
def compare_images( expected, actual, tol, in_decorator=False ):
167180
'''Compare two image files - not the greatest, but fast and good enough.
168181
@@ -199,12 +212,15 @@ def compare_images( expected, actual, tol, in_decorator=False ):
199212
# Convert the image to png
200213
extension = expected.split('.')[-1]
201214
if extension != 'png':
202-
actual, expected = convert(actual), convert(expected)
215+
actual = convert(actual)
216+
expected = convert(expected)
203217

204218
# open the image files and remove the alpha channel (if it exists)
205219
expectedImage = Image.open( expected ).convert("RGB")
206220
actualImage = Image.open( actual ).convert("RGB")
207221

222+
actualImage, expectedImage = crop_to_same(actual, actualImage, expected, expectedImage)
223+
208224
# normalize the images
209225
expectedImage = ImageOps.autocontrast( expectedImage, 2 )
210226
actualImage = ImageOps.autocontrast( actualImage, 2 )
@@ -251,10 +267,13 @@ def compare_images( expected, actual, tol, in_decorator=False ):
251267

252268
def save_diff_image( expected, actual, output ):
253269
from PIL import Image
254-
expectedImage = np.array(Image.open( expected ).convert("RGB")).astype(np.float)
255-
actualImage = np.array(Image.open( actual ).convert("RGB")).astype(np.float)
256-
assert expectedImage.ndim==expectedImage.ndim
257-
assert expectedImage.shape==expectedImage.shape
270+
expectedImage = Image.open( expected ).convert("RGB")
271+
actualImage = Image.open( actual ).convert("RGB")
272+
actualImage, expectedImage = crop_to_same(actual, actualImage, expected, expectedImage)
273+
expectedImage = np.array(expectedImage).astype(np.float)
274+
actualImage = np.array(actualImage).astype(np.float)
275+
assert expectedImage.ndim==actualImage.ndim
276+
assert expectedImage.shape==actualImage.shape
258277
absDiffImage = abs(expectedImage-actualImage)
259278
# expand differences in luminance domain
260279
absDiffImage *= 10

lib/matplotlib/testing/decorators.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ def test(self):
9696
fail_msg = 'No failure expected'
9797

9898
orig_expected_fname = os.path.join(baseline_dir, baseline) + '.' + extension
99-
expected_fname = os.path.join(result_dir, 'expected-' + baseline) + '.' + extension
99+
if extension == 'eps' and not os.path.exists(orig_expected_fname):
100+
orig_expected_fname = os.path.join(baseline_dir, baseline) + '.pdf'
101+
expected_fname = os.path.join(result_dir, 'expected-' + os.path.basename(orig_expected_fname))
100102
actual_fname = os.path.join(result_dir, baseline) + '.' + extension
101103
if os.path.exists(orig_expected_fname):
102104
shutil.copyfile(orig_expected_fname, expected_fname)
@@ -110,11 +112,12 @@ def test(self):
110112
def do_test():
111113
figure.savefig(actual_fname)
112114

115+
err = compare_images(expected_fname, actual_fname, self._tol, in_decorator=True)
116+
113117
if not os.path.exists(expected_fname):
114118
raise ImageComparisonFailure(
115119
'image does not exist: %s' % expected_fname)
116120

117-
err = compare_images(expected_fname, actual_fname, self._tol, in_decorator=True)
118121
if err:
119122
raise ImageComparisonFailure(
120123
'images not close: %(actual)s vs. %(expected)s '
@@ -150,7 +153,7 @@ def image_comparison(baseline_images=None, extensions=None, tol=1e-3):
150153

151154
if extensions is None:
152155
# default extensions to test
153-
extensions = ['png', 'pdf', 'svg']
156+
extensions = ['png', 'pdf', 'svg', 'eps']
154157

155158
def compare_images_decorator(func):
156159
# We want to run the setup function (the actual test function

lib/matplotlib/tests/test_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def test_image_clip():
104104

105105
im = ax.imshow(d, extent=(-pi,pi,-pi/2,pi/2))
106106

107-
@image_comparison(baseline_images=['imshow'])
107+
@image_comparison(baseline_images=['imshow'], tol=1.5e-3)
108108
def test_imshow():
109109
import numpy as np
110110
import matplotlib.pyplot as plt

lib/matplotlib/tests/test_legend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from nose.tools import assert_raises
66
from numpy.testing import assert_array_equal
77

8-
@image_comparison(baseline_images=['legend_auto1'])
8+
@image_comparison(baseline_images=['legend_auto1'], tol=1.5e-3)
99
def test_legend_auto1():
1010
'Test automatic legend placement'
1111
fig = plt.figure()

lib/matplotlib/tests/test_png.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import glob
55
import os
66

7-
@image_comparison(baseline_images=['pngsuite'])
7+
@image_comparison(baseline_images=['pngsuite'], extensions=['png'])
88
def test_pngsuite():
99
dirname = os.path.join(
1010
os.path.dirname(__file__),

ttconv/pprdrv_tt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ void ttfont_header(TTStreamWriter& stream, struct TTFONT *font)
437437
stream.putline("/FontMatrix[.001 0 0 .001 0 0]def");
438438
}
439439

440-
stream.printf("/FontBBox[%d %d %d %d]def\n",font->llx,font->lly,font->urx,font->ury);
440+
stream.printf("/FontBBox[%d %d %d %d]def\n",font->llx-1,font->lly-1,font->urx,font->ury);
441441
if (font->target_type == PS_TYPE_42 || font->target_type == PS_TYPE_42_3_HYBRID)
442442
{
443443
stream.printf("/FontType 42 def\n", font->target_type );

0 commit comments

Comments
 (0)