1import sys 2import vtk 3import array 4from vtk.test import Testing 5 6 7class TestDataEncoder(Testing.vtkTest): 8 def testEncodings(self): 9 # Render something 10 cylinder = vtk.vtkCylinderSource() 11 cylinder.SetResolution(8) 12 13 cylinderMapper = vtk.vtkPolyDataMapper() 14 cylinderMapper.SetInputConnection(cylinder.GetOutputPort()) 15 16 cylinderActor = vtk.vtkActor() 17 cylinderActor.SetMapper(cylinderMapper) 18 cylinderActor.RotateX(30.0) 19 cylinderActor.RotateY(-45.0) 20 21 ren = vtk.vtkRenderer() 22 renWin = vtk.vtkRenderWindow() 23 renWin.AddRenderer(ren) 24 ren.AddActor(cylinderActor) 25 renWin.SetSize(200, 200) 26 27 ren.ResetCamera() 28 ren.GetActiveCamera().Zoom(1.5) 29 renWin.Render() 30 31 # Get a vtkImageData with the rendered output 32 w2if = vtk.vtkWindowToImageFilter() 33 w2if.SetInput(renWin) 34 w2if.SetShouldRerender(1) 35 w2if.SetReadFrontBuffer(0) 36 w2if.Update() 37 imgData = w2if.GetOutput() 38 39 # Use vtkDataEncoder to convert the image to PNG format and Base64 encode it 40 encoder = vtk.vtkDataEncoder() 41 base64String = encoder.EncodeAsBase64Png(imgData).encode('ascii') 42 43 # Now Base64 decode the string back to PNG image data bytes 44 inputArray = array.array('B', base64String) 45 outputBuffer = bytearray(len(inputArray)) 46 47 utils = None 48 try: 49 utils = vtk.vtkIOCore.vtkBase64Utilities() 50 except: 51 try: 52 utils = vtkIOCore.vtkBase64Utilities() 53 except: 54 print('Unable to import required vtkIOCore.vtkBase64Utilities') 55 return 56 57 actualLength = utils.DecodeSafely(inputArray, len(inputArray), outputBuffer, len(outputBuffer)) 58 outputArray = bytearray(actualLength) 59 outputArray[:] = outputBuffer[0:actualLength] 60 61 # And write those bytes to the disk as an actual PNG image file 62 with open('TestDataEncoder.png', 'wb') as fd: 63 fd.write(outputArray) 64 65 # Create a vtkTesting object and specify a baseline image 66 rtTester = vtk.vtkTesting() 67 for arg in sys.argv[1:]: 68 rtTester.AddArgument(arg) 69 rtTester.AddArgument("-V") 70 rtTester.AddArgument("TestDataEncoder.png") 71 72 # Perform the image comparison test and print out the result. 73 result = rtTester.RegressionTest("TestDataEncoder.png", 0.0) 74 75 if result == 0: 76 raise Exception("TestDataEncoder failed.") 77 78if __name__ == "__main__": 79 Testing.main([(TestDataEncoder, 'test')]) 80