00001 import unittest 00002 import numpy as np 00003 import ros_numpy 00004 from sensor_msgs.msg import PointCloud2, PointField, Image 00005 00006 class TestImages(unittest.TestCase): 00007 def test_roundtrip_rgb8(self): 00008 arr = np.random.randint(0, 256, size=(240, 360, 3)).astype(np.uint8) 00009 msg = ros_numpy.msgify(Image, arr, encoding='rgb8') 00010 arr2 = ros_numpy.numpify(msg) 00011 00012 np.testing.assert_equal(arr, arr2) 00013 00014 def test_roundtrip_mono(self): 00015 arr = np.random.randint(0, 256, size=(240, 360)).astype(np.uint8) 00016 msg = ros_numpy.msgify(Image, arr, encoding='mono8') 00017 arr2 = ros_numpy.numpify(msg) 00018 00019 np.testing.assert_equal(arr, arr2) 00020 00021 def test_roundtrip_big_endian(self): 00022 arr = np.random.randint(0, 256, size=(240, 360)).astype('>u2') 00023 msg = ros_numpy.msgify(Image, arr, encoding='mono16') 00024 self.assertEqual(msg.is_bigendian, True) 00025 arr2 = ros_numpy.numpify(msg) 00026 00027 np.testing.assert_equal(arr, arr2) 00028 00029 def test_roundtrip_little_endian(self): 00030 arr = np.random.randint(0, 256, size=(240, 360)).astype('<u2') 00031 msg = ros_numpy.msgify(Image, arr, encoding='mono16') 00032 self.assertEqual(msg.is_bigendian, False) 00033 arr2 = ros_numpy.numpify(msg) 00034 00035 np.testing.assert_equal(arr, arr2) 00036 00037 00038 def test_bad_encodings(self): 00039 mono_arr = np.random.randint(0, 256, size=(240, 360)).astype(np.uint8) 00040 mono_arrf = np.random.randint(0, 256, size=(240, 360)).astype(np.float32) 00041 rgb_arr = np.random.randint(0, 256, size=(240, 360, 3)).astype(np.uint8) 00042 rgb_arrf = np.random.randint(0, 256, size=(240, 360, 3)).astype(np.float32) 00043 00044 with self.assertRaises(TypeError): 00045 msg = ros_numpy.msgify(Image, rgb_arr, encoding='mono8') 00046 with self.assertRaises(TypeError): 00047 msg = ros_numpy.msgify(Image, mono_arrf, encoding='mono8') 00048 00049 with self.assertRaises(TypeError): 00050 msg = ros_numpy.msgify(Image, rgb_arrf, encoding='rgb8') 00051 with self.assertRaises(TypeError): 00052 msg = ros_numpy.msgify(Image, mono_arr, encoding='rgb8') 00053 00054 00055 if __name__ == '__main__': 00056 unittest.main()