2011-06-17 15:50:14 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
"""This module wraps up libvips in a less awful interface.
|
|
|
|
|
|
|
|
Wrap VipsObject.
|
|
|
|
|
|
|
|
Author: J.Cupitt
|
|
|
|
GNU LESSER GENERAL PUBLIC LICENSE
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
import ctypes
|
|
|
|
|
2011-06-18 18:47:56 +02:00
|
|
|
import gobject
|
|
|
|
|
2011-06-18 14:34:52 +02:00
|
|
|
import finalizable
|
2011-06-17 15:50:14 +02:00
|
|
|
|
|
|
|
# .15 is 7.25+ with the new vips8 API
|
|
|
|
libvips = ctypes.CDLL('libvips.so.15')
|
|
|
|
libvips.vips_init(sys.argv[0])
|
|
|
|
|
2011-06-18 14:34:52 +02:00
|
|
|
# given a class and value, search for a class member with that value
|
|
|
|
# handy for enum classes, use to turn numbers to strings
|
|
|
|
def class_value(classobject, value):
|
|
|
|
for name in dir(classobject):
|
|
|
|
if getattr(classobject, name) == value:
|
|
|
|
return classobject.__name__ + '.' + name
|
|
|
|
|
|
|
|
return 'unknown'
|
|
|
|
|
2011-06-18 18:47:56 +02:00
|
|
|
class VipsError(Exception):
|
2011-06-17 15:50:14 +02:00
|
|
|
|
|
|
|
"""An error from libvips.
|
|
|
|
|
|
|
|
message -- a high-level description of the error
|
|
|
|
detail -- a string with some detailed diagnostics
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, message):
|
|
|
|
self.message = message
|
|
|
|
self.detail = vips_error_buffer()
|
|
|
|
libvips.vips_error_clear()
|
|
|
|
|
2011-06-18 14:34:52 +02:00
|
|
|
logging.debug('vipsobject: Error: %s %s', self.message, self.detail)
|
2011-06-17 15:50:14 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
2011-06-18 14:34:52 +02:00
|
|
|
return '%s %s' %(self.message, self.detail)
|
2011-06-17 15:50:14 +02:00
|
|
|
|
2011-06-18 14:34:52 +02:00
|
|
|
# handy checkers, assign to errcheck
|
|
|
|
def check_int_return(result, func, args):
|
|
|
|
if result != 0:
|
2011-06-18 18:47:56 +02:00
|
|
|
raise VipsError('Error calling vips function %s.' % func.__name__)
|
2011-06-18 14:34:52 +02:00
|
|
|
return result
|
2011-06-17 15:50:14 +02:00
|
|
|
|
2011-06-18 14:34:52 +02:00
|
|
|
def check_pointer_return(result, func, args):
|
|
|
|
if result == None:
|
2011-06-18 18:47:56 +02:00
|
|
|
raise VipsError('Error calling vips function %s.' % func.__name__)
|
2011-06-18 14:34:52 +02:00
|
|
|
return result
|
2011-06-17 15:50:14 +02:00
|
|
|
|
|
|
|
vips_error_buffer = libvips.vips_error_buffer
|
|
|
|
vips_error_buffer.restype = ctypes.c_char_p
|
|
|
|
|
2011-06-18 14:34:52 +02:00
|
|
|
class VipsObject(finalizable.Finalizable):
|
2011-06-17 15:50:14 +02:00
|
|
|
"""Abstract base class for libvips."""
|
|
|
|
|
2011-06-18 14:34:52 +02:00
|
|
|
# attributes we finalize
|
|
|
|
ghost_attributes = ('vipsobject', )
|
|
|
|
|
|
|
|
def __finalize__(self):
|
|
|
|
logging.debug('vipsobject: __finalize__')
|
|
|
|
|
2011-06-17 15:50:14 +02:00
|
|
|
if self.vipsobject != None:
|
2011-06-18 14:34:52 +02:00
|
|
|
logging.debug('vipsobject: unref %s' % hex(self.vipsobject))
|
2011-06-17 15:50:14 +02:00
|
|
|
libvips.g_object_unref(self.vipsobject)
|
|
|
|
self.vipsobject = None
|
|
|
|
|
2011-06-18 14:34:52 +02:00
|
|
|
def enable_finalize(self):
|
|
|
|
self.bind_finalizer(*self.ghost_attributes)
|
|
|
|
|
2011-06-17 15:50:14 +02:00
|
|
|
def __init__(self):
|
|
|
|
logging.debug('vipsobject: init')
|
|
|
|
|
|
|
|
self.vipsobject = None
|