示例#1
0
    def _get_data_ref(self, file, filename):
        self._data_hold = data = create_string_buffer(file.read())

        dataref = carbon.NewHandle(sizeof(PointerDataRefRecord))
        datarec = cast(dataref, POINTER(
            POINTER(PointerDataRefRecord))).contents.contents
        datarec.data = addressof(data)
        datarec.dataLength = len(data)

        self._data_handler_holder = data_handler = ComponentInstance()
        r = quicktime.OpenADataHandler(dataref, PointerDataHandlerSubType,
                                       None, 0, None, kDataHCanRead,
                                       byref(data_handler))
        _oscheck(r)

        extension_handle = Handle()

        self._filename_hold = filename = Str255(filename)
        r = carbon.PtrToHand(filename, byref(extension_handle), len(filename))
        r = quicktime.DataHSetDataRefExtension(data_handler, extension_handle,
                                               kDataRefExtensionFileName)
        _oscheck(r)
        quicktime.DisposeHandle(extension_handle)

        quicktime.DisposeHandle(dataref)

        dataref = c_void_p()
        r = quicktime.DataHGetDataRef(data_handler, byref(dataref))
        _oscheck(r)

        quicktime.CloseComponent(data_handler)

        return dataref
    def decode(self, file, filename):
        dataref = self._get_data_ref(file, filename)
        importer = ComponentInstance()
        quicktime.GetGraphicsImporterForDataRef(dataref,
                                                PointerDataHandlerSubType,
                                                byref(importer))

        if not importer:
            raise ImageDecodeException(filename or file)

        rect = Rect()
        quicktime.GraphicsImportGetNaturalBounds(importer, byref(rect))
        width = rect.right
        height = rect.bottom

        format, qtformat = self._get_formats()

        buffer = (c_byte * (width * height * len(format)))()
        world = GWorldPtr()
        quicktime.QTNewGWorldFromPtr(byref(world), qtformat, byref(rect),
                                     c_void_p(), c_void_p(), 0, buffer,
                                     len(format) * width)

        quicktime.GraphicsImportSetGWorld(importer, world, c_void_p())
        result = quicktime.GraphicsImportDraw(importer)
        quicktime.DisposeGWorld(world)
        quicktime.CloseComponent(importer)

        if result != 0:
            raise ImageDecodeException(filename or file)

        pitch = len(format) * width

        return ImageData(width, height, format, buffer, -pitch)