test_imgfile.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """Simple test script for imgfile.c
  2. Roger E. Masse
  3. """
  4. from test.test_support import verbose, unlink, findfile, import_module
  5. imgfile = import_module('imgfile', deprecated=True)
  6. import uu
  7. def testimage(name):
  8. """Run through the imgfile's battery of possible methods
  9. on the image passed in name.
  10. """
  11. import sys
  12. import os
  13. outputfile = '/tmp/deleteme'
  14. # try opening the name directly
  15. try:
  16. # This function returns a tuple (x, y, z) where x and y are the size
  17. # of the image in pixels and z is the number of bytes per pixel. Only
  18. # 3 byte RGB pixels and 1 byte greyscale pixels are supported.
  19. sizes = imgfile.getsizes(name)
  20. except imgfile.error:
  21. # get a more qualified path component of the script...
  22. if __name__ == '__main__':
  23. ourname = sys.argv[0]
  24. else: # ...or the full path of the module
  25. ourname = sys.modules[__name__].__file__
  26. parts = ourname.split(os.sep)
  27. parts[-1] = name
  28. name = os.sep.join(parts)
  29. sizes = imgfile.getsizes(name)
  30. if verbose:
  31. print 'Opening test image: %s, sizes: %s' % (name, str(sizes))
  32. # This function reads and decodes the image on the specified file,
  33. # and returns it as a python string. The string has either 1 byte
  34. # greyscale pixels or 4 byte RGBA pixels. The bottom left pixel
  35. # is the first in the string. This format is suitable to pass
  36. # to gl.lrectwrite, for instance.
  37. image = imgfile.read(name)
  38. # This function writes the RGB or greyscale data in data to
  39. # image file file. x and y give the size of the image, z is
  40. # 1 for 1 byte greyscale images or 3 for RGB images (which
  41. # are stored as 4 byte values of which only the lower three
  42. # bytes are used). These are the formats returned by gl.lrectread.
  43. if verbose:
  44. print 'Writing output file'
  45. imgfile.write (outputfile, image, sizes[0], sizes[1], sizes[2])
  46. if verbose:
  47. print 'Opening scaled test image: %s, sizes: %s' % (name, str(sizes))
  48. # This function is identical to read but it returns an image that
  49. # is scaled to the given x and y sizes. If the filter and blur
  50. # parameters are omitted scaling is done by simply dropping
  51. # or duplicating pixels, so the result will be less than perfect,
  52. # especially for computer-generated images. Alternatively,
  53. # you can specify a filter to use to smoothen the image after
  54. # scaling. The filter forms supported are 'impulse', 'box',
  55. # 'triangle', 'quadratic' and 'gaussian'. If a filter is
  56. # specified blur is an optional parameter specifying the
  57. # blurriness of the filter. It defaults to 1.0. readscaled
  58. # makes no attempt to keep the aspect ratio correct, so that
  59. # is the users' responsibility.
  60. if verbose:
  61. print 'Filtering with "impulse"'
  62. simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'impulse', 2.0)
  63. # This function sets a global flag which defines whether the
  64. # scan lines of the image are read or written from bottom to
  65. # top (flag is zero, compatible with SGI GL) or from top to
  66. # bottom(flag is one, compatible with X). The default is zero.
  67. if verbose:
  68. print 'Switching to X compatibility'
  69. imgfile.ttob (1)
  70. if verbose:
  71. print 'Filtering with "triangle"'
  72. simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'triangle', 3.0)
  73. if verbose:
  74. print 'Switching back to SGI compatibility'
  75. imgfile.ttob (0)
  76. if verbose: print 'Filtering with "quadratic"'
  77. simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'quadratic')
  78. if verbose: print 'Filtering with "gaussian"'
  79. simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'gaussian', 1.0)
  80. if verbose:
  81. print 'Writing output file'
  82. imgfile.write (outputfile, simage, sizes[0]/2, sizes[1]/2, sizes[2])
  83. os.unlink(outputfile)
  84. def test_main():
  85. uu.decode(findfile('testrgb.uue'), 'test.rgb')
  86. uu.decode(findfile('greyrgb.uue'), 'greytest.rgb')
  87. # Test a 3 byte color image
  88. testimage('test.rgb')
  89. # Test a 1 byte greyscale image
  90. testimage('greytest.rgb')
  91. unlink('test.rgb')
  92. unlink('greytest.rgb')
  93. if __name__ == '__main__':
  94. test_main()