Update of /cvsroot/freevo/freevo/lib/mevas/mevas/imagelib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31476/mevas/mevas/imagelib

Added Files:
        __init__.py base.py imlib2_backend.py pygame_backend.py 
Log Message:
mevas lib for future gui usage

--- NEW FILE: pygame_backend.py ---
import pygame, types
import base

def get_capabilities():
        return {
                "to-raw-formats": [ "RGBA", "RGB" ],
                "from-raw-formats": [ "RGBA", "RGB" ],
                "preferred-format": "RGBA",  # Native format
                "shmem": False,
                "pickling": True,
                "unicode": True,
                "layer-alpha": True,
                "alpha-mask": False,
                "cache": False 

        }


# TODO: Finish me.


class Image(base.Image):
        def __init__(self, image_or_filename):
                if isinstance(image_or_filename, Image):
                        self._image = image_or_filename._image
                elif type(image_or_filename) == pygame.Surface:
                        self._image = image_or_filename
                elif type(image_or_filename) in types.StringTypes:
                        self._image = pygame.image.load(image_or_filename)
                else:
                        raise ValueError, "Unsupported image type: %s" % 
type(image_or_filename)


        def __getattr__(self, attr):
                if attr == "size":
                        return self._image.get_size()
                elif attr == "width":
                        return self._image.get_width()
                elif attr == "height":
                        return self._image.get_height()
                # FIXME: implement format, mode, filename

                return super(Image, self).__getattr__(attr)

        def get_capabilities(self):
                return get_capabilities()
        


def open(file):
        return Image(file)

def new(size, rawdata = None, from_format = "RGBA"):
        if from_format not in get_capabilities()["from-raw-formats"]:
                raise ValueError, "Unsupported raw format: %s" % from_format
        if rawdata:
                return Image( pygame.image.fromstring(rawdata, size, from_format) )
        return Image( pygame.Surface(size) )

def add_font_path(path):
        pass

def load_font(font, size):
        pass

--- NEW FILE: imlib2_backend.py ---
import Imlib2, types
import base


def get_capabilities():
        return {
                "to-raw-formats": [ "BGRA", "ABGR", "RGBA", "ARGB", "YV12A" ],
                "from-raw-formats": [ "BGRA", "ABGR", "RGBA", "ARGB" ],
                "preferred-format": "BGRA",  # Native format
                "shmem": True,
                "pickling": True,
                "unicode": True,
                "layer-alpha": True,
                "alpha-mask": True,
                "cache": True

        }


class Image(base.Image):

        def __init__(self, image_or_filename):
                if isinstance(image_or_filename, Image):
                        self._image = image_or_filename._image
                elif isinstance(image_or_filename, Imlib2.Image):
                        self._image = image_or_filename
                elif type(image_or_filename) in types.StringTypes:
                        self._image = Imlib2.Image(image_or_filename)
                else:
                        raise ValueError, "Unsupported image type: %s" % 
type(image_or_filename)

        def __getattr__(self, attr):
                if attr in ("width", "height", "size", "format", "mode", "filename"):
                        return getattr(self._image, attr)
                return super(Image, self).__getattr__(attr)

        def get_raw_data(self, format = "BGRA"):
                return self._image.get_bytes(format)

        def scale(self, size):
                return Image( self._image.scale(size) )

        def crop(self, pos, size):
                return Image( self._image.crop(pos, size) )

        def rotate(self, angle):
                return Image( self._image.rotate(angle) )

        def scale_preserve_aspect(self, size):
                return Image( self._image.scale_preserve_aspect(size) )

        def copy_rect(self, src_pos, size, dst_pos):
                return self._image.copy_rect( src_pos, size, dst_pos )

        def blend(self, srcimg, dst_pos = (0, 0), src_pos = (0, 0), 
                  src_size = (-1, -1), alpha = 255, merge_alpha = True):
                return self._image.blend(srcimg._image, dst_pos, src_pos, src_size, 
alpha, merge_alpha)

        def clear(self, pos = (0, 0), size = (-1, -1)):
                self._image.clear( pos, size )

        def draw_mask(self, maskimg, pos):
                return self._image.draw_mask(maskimg._image, pos)

        def copy(self):
                return Image( self._image.copy() )

        def set_font(self, font_or_fontname):
                if isinstance(font_or_fontname, Font):
                        font_or_fontname = font_or_fontname._font
                return self._image.set_font(font_or_fontname._font)

        def get_font(self):
                return Font(self._image.get_font())
                
        def draw_text(self, pos, text, color = None, font_or_fontname = None):
                if isinstance(font_or_fontname, Font):
                        font_or_fontname = font_or_fontname._font
                return self._image.draw_text(pos, text, color, font_or_fontname)       
 

        def draw_rectangle(self, pos, size, color, fill = True):
                return self._image.draw_rectangle(pos, size, color, fill)

        def draw_ellipse(self, center, size, amplitude, fill = True):
                return self._image.draw_ellipse(center, size, amplitude, fill)

        def move_to_shmem(self, format = "BGRA", id = None):
                return self._image.move_to_shmem(format, id)

        def save(self, filename, format = None):
                return self._image.save(filename)

        def get_capabilities(self):
                return get_capabilities()


class Font(base.Font):

        def __init__(self, fontdesc, color = (255, 255, 255, 255)):
                self._font = Imlib2.Font(fontdesc, color)

        def get_text_size(self, text):
                return self._font.get_text_size(text)

        def set_color(self, color):
                return self._font.set_color(color)

        def __getattr__(self, attr):
                if attr in ("ascent", "descent", "max_ascent", "max_descent"):
                        return getattr(self._font, attr)
                return super(Font, self).__getattr__(attr)


def open(file):
        return Image(file)

def new(size, rawdata = None, from_format = "BGRA"):
        if from_format not in get_capabilities()["from-raw-formats"]:
                raise ValueError, "Unsupported raw format: %s" % from_format
        return Image( Imlib2.new(size, rawdata) )

def add_font_path(path):
        return Imlib2.add_font_path(path)

def load_font(font, size):
        return Imlib2.load_font(font, size)



--- NEW FILE: base.py ---
def get_capabilities():
        """
        Returns a dictionary of capabilities for this backend.

        Required items are:
            to-raw-formats: list of supported pixel formats that get_raw_data()
                            supports.  e.g. BGRA, RGB, YV12A.
          from-raw-formats: list of supported pixel formats that new() supports.
          preferred-format: the native or preferred pixel format for the backend.
                            When format parameters are unspecified, this is the
                            default format.
                     shmem: If the backend supports writing raw data to shared
                            shared memory.
                  pickling: If Image and Font objects can be pickled.
                   unicode: If the backend correctly supports unicode strings.
               layer-alpha: If the alpha parameter in Image.blend() is supported.
                alpha-mask: If Image.draw_mask() is supported.
                     cache: If the the backend implements an image cache.
        """
        return {
        }


class Image(object):

        def __init__(self, image_or_filename):
                """
                Create a new Image object wrapper for the given backend.

                Arguments:
                  image_or_filename: Instantiate the image from another Image
                                     instance, an instance of the backend's image
                                     class or type, or a file name from which to load
                                     the image.             
                """
                pass


        def __getattr__(self, attr):
                """
                These attributes must be available:

                      size: tuple containing the width and height of the image
                     width: width of the image
                    height: height of the image
                    format: format of the image if loaded from file (e.g. PNG, JPEG)
                  filename: filename if loaded from file
                """

                if attr not in self.__dict__:
                        raise AttributeError, attr
                return self.__dict__[attr]


        def get_raw_data(self, format = "RGBA"):
                """
                Returns raw image data.

                Arguments:
                  format: pixel format of the raw data to be returned.  If 'format' is
                          not a supported format, raise ValueError.

                Returns: A string or buffer object containing the raw image data.
                """
                pass


        def scale(self, (w, h)):
                """
                Scale the image and return a new image.

                Arguments:
                  w, h: the width and height of the new image.  If either argument
                        is -1, that dimension is calculated from the other dimension
                        while retaining the original aspect ratio.

                Returns: a new Image instance representing the scaled image.
                """
                
                pass

        def crop(self, (x, y), (w, h)):
                """
                Crop the image and return a new image.

                Arguments:
                  x, y, w, h: represents the left, top, width, height region in
                              the image.

                Returns: a new Image instance representing the cropped image.
                """
                pass

        def rotate(self, angle):
                """
                Rotate the image and return a new image.

                Arguments:
                  angle: the angle in degrees by which to rotate the image.

                Return: a new Image instance representing the rotated image.
                """
                pass

        def scale_preserve_aspect(self, (w, h)):
                """
                Scales the image while retaining the original aspect ratio and return
                a new image.

                Arguments:
                  w, h: the maximum size of the new image.  The new image will be as
                        large as possible, using w, h as the upper limits, while
                        retaining the original aspect ratio.

                Return: a new Image insatnce represented the scaled image.
                """
                pass


        def copy_rect(self, src_pos, size, dst_pos):
                """
                Copies a region within the image.

                Arguments:
                  src_pos: a tuple holding the x, y coordinates marking the top left
                           of the region to be moved.
                     size: a tuple holding the width and height of the region to move.
                               If either dimension is -1, then that dimension extends 
to
                           the far edge of the image.
                  dst_pos: a tuple holding the x, y coordinates within the image 
                           where the region will be moved to.

                Returns: None
                """
                pass


        def blend(self, srcimg, dst_pos = (0, 0), src_pos = (0, 0),
                  src_size = (-1, -1), alpha = 255, merge_alpha = True):
                """
                Blends one image onto another.  

                Arguments:
                       srcimg: the image being blended onto 'self'
                      dst_pos: a tuple holding the x, y coordinates where the source
                               image will be blended onto the destination image.
                      src_pos: a tuple holding the x, y coordinates within the source
                               image where blending will start.
                     src_size: a tuple holding the width and height of the source
                               image to be blended.  A value of -1 for either one
                               indicates the full dimension of the source image.
                        alpha: the "layer" alpha that is applied to all pixels of the
                               image.  If an individual pixel has an alpha of 128 and
                               this value is 128, the resulting pixel will have an
                               alpha of 64 before it is blended to the destination
                               image.  0 is fully transparent and 255 is fully opaque,
                               and 256 is a special value that means alpha blending is
                               disabled.
                  merge_alpha: if True, the alpha channel is also blended.  If False,
                               the destination image's alpha channel is untouched and
                               the RGB values are compensated

                Returns: None.
                """
                pass

        def clear(self, (x, y) = (0, 0), (w, h) = (-1, -1)):
                """
                Clears the image at the specified rectangle, resetting all pixels in
                that rectangle to fully transparent.

                Arguments:
                  x, y: left and top coordinates of the rectangle to be cleared.
                        Default is the top left corner.
                  w, h: width and height of the rectangle to be cleared.  If either
                        value is -1 then the image is cleared to the far edge.

                Returns: None
                """
                pass

        def draw_mask(self, maskimg, (x, y)):
                """
                Applies the luma channel of maskimg to the alpha channel of the
                the current image.

                Arguments:
                  maskimg: the image from which to read the luma channel
                     x, y: the top left coordinates within the current image where the
                           alpha channel will be modified.  The mask is drawn to the
                           full width/height of maskimg.

                Returns: None
                """
                pass

        def copy(self):
                """
                Creates a copy of the current image.

                Returns: a new Image instance with a copy of the current image.
                """
                pass

        def set_font(self, font_or_fontname):
                """
                Sets the font context to font_or_font_name.  Subsequent calls to
                draw_text() will be rendered using this font.

                Arguments:
                  font_or_fontname: either a Font object, or a string containing the
                                    font's name and size.  This string is in the
                                    form "Fontname/Size" such as "Arial/16"


                Returns: a Font instance represent the specified font.  It
                         'font_or_fontname' is already a Font instance, it is simply
                         returned back to the caller.
                """
                pass


        def get_font(self):
                """
                Gets the current Font context.

                Returns: A Font instance as created by set_font() or None if no font
                         context is defined.
                """
                pass


        def draw_text(self, (x, y), text, color = None, font_or_fontname = None):
                """
                Draws text on the image.

                Arguments:
                              x, y: the left/top coordinates within the current image
                                    where the text will be rendered.
                              text: a string holding the text to be rendered.
                             color: a 3- or 4-tuple holding the red, green, blue, and
                                    alpha values of the color in which to render the
                                    font.  If color is a 3-tuple, the implied alpha
                                    is 255.  If color is None, the color of the font
                                    context, as specified by set_font(), is used.
                  font_or_fontname: either a Font object, or a string containing the
                                    font's name and size.  This string is in the
                                    form "Fontname/Size" such as "Arial/16".  If this
                                    parameter is none, the font context is used, as
                                    specified by set_font().

                Returns: a 4-tuple representing the width, height, horizontal advance,
                         and vertical advance of the rendered text.
                """
                pass

        def draw_rectangle(self, (x, y), (w, h), color, fill = True):
                """
                Draws a rectangle on the image.

                Arguments:
                   x, y: the top left corner of the rectangle.
                   w, h: the width and height of the rectangle.
                  color: a 3- or 4-tuple holding the red, green, blue, and alpha 
                         values of the color in which to draw the rectangle.  If 
                         color is a 3-tuple, the implied alpha is 255.
                   fill: whether the rectangle should be filled or not.  The default
                         is true.

                Returns: None
                """
                pass

        def draw_ellipse(self, (xc, yc), (a, b), color, fill = True):
                """
                Draws an ellipse on the image.

                Arguments:
                  xc, yc: the x, y coordinates of the center of the ellipse.
                    a, b: the horizontal and veritcal amplitude of the ellipse.
                   color: a 3- or 4-tuple holding the red, green, blue, and alpha 
                          values of the color in which to draw the ellipse.  If 
                          color is a 3-tuple, the implied alpha is 255.
                    fill: whether the ellipse should be filled or not.  The default
                          is true.

                Returns: None
                """
                pass


        def move_to_shmem(self, format = "RGBA", id = None):
                """
                Creates a POSIX shared memory object and copy the image's raw data.  

                Arguments:
                  format: the format of the raw data to copy to shared memory.  If
                          the specified format is not supported, raise ValueError.
                      id: the name of the shared memory object (as passed to 
                          shm_open(3)).  If id is None, a suitable unique id is
                          generated.

                Returns: the id of the shared memory object.
                """
                pass


        def save(self, filename, format = None):
                """
                Saves the image to a file.

                Arguments:
                  format: the format of the written file (jpg, png, etc.).  If format
                          is None, the format is gotten from the filename extension.

                Returns: None.
                """
                pass

        def get_capabilities(self):
                """
                Get the capabilities of the imagelib backend used to create this
                image.
                """
                return get_capabilities()


class Font:

        def __init__(self, fontdesc, color = (255, 255, 255, 255)):
                """
                Create a new Font object wrapper.

                Arguments:
                  fontdesc: the description of the font, in the form "Fontname/Size".
                            Only TrueType fonts are supported, and the .ttf file must
                            exist in a registered font path.  Font paths can be
                            registered by calling imagelib.add_font_path().
                     color: a 3- or 4-tuple holding the red, green, blue, and alpha 
                            values of the color in which to render text with this 
                            font context.  If color is a 3-tuple, the implied alpha 
                            is 255.  If color is not specified, the default is fully
                            opaque white.

                """
                pass

        def get_text_size(self, text):
                """
                Get the font metrics for the specified text as rendered by the 
                current font.

                Arguments:
                  text: the text for which to retrieve the metric.

                Returns: a 4-tuple containing the width, height, horizontal advance,
                         and vertical advance of the text when rendered.
                """
                pass

        def set_color(self, color):
                """
                Sets the default color for text rendered with this font.

                Arguments:
                  color: a 3- or 4-tuple holding the red, green, blue, and alpha 
                         values of the color in which to render text with this 
                         font context.  If color is a 3-tuple, the implied alpha 
                         is 255.
                """
                pass

        def __getattr__(self, attr):
                """
                These attributes must be available:

                       ascent: the current font's ascent value in pixels.
                      descent: the current font's descent value in pixels.
                  max_descent: the current font's maximum descent extent.
                   max_ascent: the current font's maximum ascent extent.
                """
                if attr not in self.__dict__:
                        raise AttributeError, attr
                return self.__dict__[attr]


# These methods must be provided by the backend and are accessed by
# imagelib.functionname when the backend is loaded.

def open(file):
        """
        Opens a file and returns an Image instance.

        Arguments:
          file: the filename of the image to load.

        Returns: a new Image instance representing the file.
        """
        pass

def new(size, rawdata = None, from_format = "RGBA"):
        """
        Creates a new image and returns an Image instance.

        Arguments:
                 size: a tuple containing the width and height of the new image.
              rawdata: the raw image data from which to create the new image.  If
                       rawdata is not None, it is expected to be in the same
                       format as specified by from_format, and be the required
                       size.  If the size is invalid, raise ValueError.  If
                       rawdata is not None, the all pixels are initialized to
                       fully transparent.
          from_format: the format the raw data is specified in.  If the format is
                       not a supported format by this backend, raise ValueError.

        Returns: a new Image instance, either blank if rawdata is not specified,
                 or initialized from rawdata.
        """
        pass

def add_font_path(path):
        """
        Adds a path to the list of paths scanned when loading fonts.

        Arguments:
          path: the path name to add.

        Returns: None.
        """
        pass
                                                                                       
                                            
def load_font(font, size):
        """
        Load the specified font and return a new Font instance.

        Arguments:
          font: the name of the font to load.
          size: the size of font.

        Returns: a new Font instance representing the specified font.
        """
        pass

--- NEW FILE: __init__.py ---
_backend = None

def set_backend(name):
        exec "from %s_backend import *"  % name in globals()
        global _backend
        _backend = name

def get_current_backend():
        return _backend

def get_backend(name):
        try:
                backend = __import__(name + "_backend", globals())
        except ImportError:
                return None
        return backend

# Set default backend
try:
        import Imlib2
        set_backend("imlib2")
except ImportError:
        pass

if _backend == None:
        raise "No supported image library could be found."




-------------------------------------------------------
SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media
100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33
Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift.
http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285
_______________________________________________
Freevo-cvslog mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to