oc_jpegencode 1.0 JPEGencoder

test_jpeg_top.py
Go to the documentation of this file.
1 """
2 Example of using Cocotb to send image files in to the opencores JPEG Encoder
3 and check that the output is sufficiently similar to the input.
4
5 NB Limited to 96x96 images since we're using a static JPEG header.
6 """
7 import os
8
9 import logging
10 from itertools import izip
11 from PIL import Image
12
13 import cocotb
14 from cocotb.result import TestFailure
15 from cocotb.regression import TestFactory
16 from cocotb.clock import Clock
17
18 from interfaces import ImageDriver, JpegMonitor
19
20 def compare(i1, i2):
21 """
22 Compare the similarity of two images
23
24 From http://rosettacode.org/wiki/Percentage_difference_between_images
25 """
26 assert i1.mode == i2.mode, "Different kinds of images."
27 assert i1.size == i2.size, "Different sizes."
28
29 pairs = izip(i1.getdata(), i2.getdata())
30 dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
31 ncomponents = i1.size[0] * i1.size[1] * 3
32 return (dif / 255.0 * 100) / ncomponents
33
34
35 @cocotb.coroutine
36 def process_image(dut, filename="", debug=False, threshold=0.22):
37 """Run an image file through the jpeg encoder and compare the result"""
38 cocotb.fork(Clock(dut.clk, 100).start())
39
40 driver = ImageDriver(dut)
41 monitor = JpegMonitor(dut)
42
43 if debug: # pragma: no cover
44 driver.log.setLevel(logging.DEBUG)
45 monitor.log.setLevel(logging.DEBUG)
46
47 stimulus = Image.open(filename)
48 yield driver.send(stimulus)
49 output = yield monitor.wait_for_recv()
50
51 if debug: # pragma: no cover
52 output.save(filename + "_process.jpg")
53
54 difference = compare(stimulus, output)
55
56 dut.log.info("Compressed image differs to original by %f%%" % (difference))
57
58 if difference > threshold: # pragma: no cover
59 raise TestFailure("Resulting image file was too different (%f > %f)" %
60 (difference, threshold))
61
62 tf = TestFactory(process_image)
63 tf.add_option("filename", [os.path.join('test_images', f)
64 for f in os.listdir('test_images')])
65 tf.generate_tests()
test_jpeg_top.process_image
def process_image( dut, filename="", debug=False, threshold=0.22)
Definition: test_jpeg_top.py:36
test_jpeg_top.compare
def compare( i1, i2)
Definition: test_jpeg_top.py:20