Example #1
0
    def _get_pyglet_ImageData_from_source_at_index(self, sourceRef, index):
        imageRef = c_void_p(quartz.CGImageSourceCreateImageAtIndex(sourceRef, index, None))

        # Regardless of the internal format of the image (L, LA, RGB, RGBA, etc)
        # we just automatically convert everything to an RGBA format.
        format = 'RGBA'
        rgbColorSpace = c_void_p(quartz.CGColorSpaceCreateDeviceRGB())
        bitsPerComponent = 8
        width = quartz.CGImageGetWidth(imageRef)
        height = quartz.CGImageGetHeight(imageRef)
        bytesPerRow = 4 * width

        # Create a buffer to store the RGBA formatted data.
        bufferSize = height * bytesPerRow
        buffer = (c_ubyte * bufferSize)()

        # Create a bitmap context for the RGBA formatted data.
        # Note that premultiplied alpha is required:
        # http://developer.apple.com/library/mac/#qa/qa1037/_index.html
        bitmap = c_void_p(quartz.CGBitmapContextCreate(buffer,
                                                       width, height,
                                                       bitsPerComponent,
                                                       bytesPerRow,
                                                       rgbColorSpace,
                                                       kCGImageAlphaPremultipliedLast))

        # Write the image data into the bitmap.
        quartz.CGContextDrawImage(bitmap, NSMakeRect(0,0,width,height), imageRef)

        quartz.CGImageRelease(imageRef)
        quartz.CGContextRelease(bitmap)
        quartz.CGColorSpaceRelease(rgbColorSpace)

        pitch = bytesPerRow
        return ImageData(width, height, format, buffer, -pitch)
Example #2
0
def get_scaling_factor(window):
    from pyglet import compat_platform
    if compat_platform == 'darwin':
        from pyglet.libs.darwin.cocoapy import NSMakeRect
        view = window.context._nscontext.view()
        content_rect = NSMakeRect(0, 0, window._width,
                                  window._height)  # Get size, possibly scaled
        bounds = view.convertRectFromBacking_(
            content_rect)  # Convert to actual pixel sizes
        return int(content_rect.size.width / bounds.size.width)
    else:
        return 1
Example #3
0
def get_scaling_factor(window):
    """
    Tries to get the scaling factor of the given Window. Currently works
    on MacOS only. Useful in figuring out what's going on with Retina and
    high-res displays.

    :param Window window: Handle to window we want to get scaling factor of.

    :return: Scaling factor. E.g., 2 would indicate scaled up twice.
    :rtype: int

    """
    from pyglet import compat_platform
    if compat_platform == 'darwin':
        from pyglet.libs.darwin.cocoapy import NSMakeRect
        view = window.context._nscontext.view()
        content_rect = NSMakeRect(0, 0, window._width, window._height)  # Get size, possibly scaled
        bounds = view.convertRectFromBacking_(content_rect)  # Convert to actual pixel sizes
        return int(content_rect.size.width / bounds.size.width)
    else:
        return 1