예제 #1
0
 def extract_value(vs_frame: vs.VideoFrame, i: int, idx: int) \
         -> Union[int, float]:
     fmt = vs_frame.format
     if fmt.sample_type == vs.FLOAT and fmt.bytes_per_sample == 2:
         ptr = ctypes.cast(
             vs_frame.get_read_ptr(i),
             ctypes.POINTER(ctypes.c_char *
                            (2 * vs_frame.width * vs_frame.height)))
         val = unpack('e',
                      ptr.contents[(idx * 2):(idx * 2 +
                                              2)])[0]  # type: ignore
         return cast(float, val)
     else:
         ptr = ctypes.cast(
             vs_frame.get_read_ptr(i),
             ctypes.POINTER(
                 self.data_types[fmt.sample_type][fmt.bytes_per_sample]
                 * (  # type:ignore
                     vs_frame.width * vs_frame.height)))
         return ptr.contents[idx]  # type: ignore
예제 #2
0
 def extract_value(vs_frame: vs.VideoFrame, plane: int,
                   pos: Qt.QPoint) -> Union[int, float]:
     fmt = vs_frame.format
     stride = vs_frame.get_stride(plane)
     if fmt.sample_type == vs.FLOAT and fmt.bytes_per_sample == 2:
         ptr = ctypes.cast(
             vs_frame.get_read_ptr(plane),
             ctypes.POINTER(ctypes.c_char * (stride * vs_frame.height)))
         offset = pos.y() * stride + pos.x() * 2
         val = unpack('e', ptr.contents[offset:(offset +
                                                2)])[0]  # type: ignore
         return cast(float, val)
     else:
         ptr = ctypes.cast(
             vs_frame.get_read_ptr(plane),
             ctypes.POINTER(
                 self.data_types[fmt.sample_type][fmt.bytes_per_sample]
                 * (  # type:ignore
                     stride * vs_frame.height)))
         logical_stride = stride // fmt.bytes_per_sample
         idx = pos.y() * logical_stride + pos.x()
         return ptr.contents[idx]  # type: ignore
예제 #3
0
    def render_raw_videoframe(self, vs_frame: vs.VideoFrame) -> Qt.QPixmap:
        import ctypes

        frame_data = vs_frame.get_read_ptr(0)
        frame_stride = vs_frame.get_stride(0)
        # frame_itemsize = vs_frame.get_read_array(0).itemsize
        frame_itemsize = vs_frame.format.bytes_per_sample

        # powerful spell. do not touch
        frame_data = ctypes.cast(
            frame_data,
            ctypes.POINTER(ctypes.c_char *
                           (frame_itemsize * vs_frame.width *
                            vs_frame.height)))[0]  # type: ignore
        frame_image = Qt.QImage(frame_data, vs_frame.width, vs_frame.height,
                                frame_stride, Qt.QImage.Format_RGB32)
        frame_pixmap = Qt.QPixmap.fromImage(frame_image)

        return frame_pixmap
예제 #4
0
    def render_raw_videoframe(
            self,
            vs_frame: vs.VideoFrame,
            vs_frame_alpha: Optional[vs.VideoFrame] = None) -> Qt.QPixmap:
        # powerful spell. do not touch
        frame_data_pointer = ctypes.cast(
            vs_frame.get_read_ptr(0),
            ctypes.POINTER(ctypes.c_char *
                           (vs_frame.format.bytes_per_sample * vs_frame.width *
                            vs_frame.height)))[0]
        frame_image = Qt.QImage(frame_data_pointer,
                                vs_frame.width, vs_frame.height,
                                vs_frame.get_stride(0), Qt.QImage.Format_RGB32)

        if vs_frame_alpha is None:
            result_pixmap = Qt.QPixmap.fromImage(frame_image)
        else:
            alpha_data_pointer = ctypes.cast(
                vs_frame_alpha.get_read_ptr(0),
                ctypes.POINTER(
                    ctypes.c_char *
                    (vs_frame_alpha.format.bytes_per_sample *
                     vs_frame_alpha.width * vs_frame_alpha.height)))[0]
            alpha_image = Qt.QImage(alpha_data_pointer, vs_frame.width,
                                    vs_frame.height,
                                    vs_frame_alpha.get_stride(0),
                                    Qt.QImage.Format_Alpha8)

            result_image = Qt.QImage(vs_frame.width, vs_frame.height,
                                     Qt.QImage.Format_ARGB32_Premultiplied)
            painter = Qt.QPainter(result_image)
            painter.setCompositionMode(Qt.QPainter.CompositionMode_Source)
            painter.drawImage(0, 0, frame_image)
            painter.setCompositionMode(
                Qt.QPainter.CompositionMode_DestinationIn)
            painter.drawImage(0, 0, alpha_image)
            painter.end()

            result_pixmap = Qt.QPixmap.fromImage(result_image)

        return result_pixmap
예제 #5
0
    def render_raw_videoframe(
            self,
            vs_frame: vs.VideoFrame,
            vs_frame_alpha: Optional[vs.VideoFrame] = None) -> Qt.QImage:
        # powerful spell. do not touch
        frame_data_pointer = ctypes.cast(
            vs_frame.get_read_ptr(0),
            ctypes.POINTER(ctypes.c_char * (vs_frame.format.bytes_per_sample *
                                            vs_frame.width * vs_frame.height)))
        frame_image = Qt.QImage(frame_data_pointer.contents,
                                vs_frame.width, vs_frame.height,
                                vs_frame.get_stride(0), Qt.QImage.Format_RGB32)

        if vs_frame_alpha is None:
            return frame_image

        alpha_data_pointer = ctypes.cast(
            vs_frame_alpha.get_read_ptr(0),
            ctypes.POINTER(ctypes.c_char *
                           (vs_frame_alpha.format.bytes_per_sample *
                            vs_frame_alpha.width * vs_frame_alpha.height)))
        alpha_image = Qt.QImage(alpha_data_pointer.contents, vs_frame.width,
                                vs_frame.height, vs_frame_alpha.get_stride(0),
                                Qt.QImage.Format_Alpha8)

        result_image = Qt.QImage(vs_frame.width, vs_frame.height,
                                 Qt.QImage.Format_ARGB32_Premultiplied)
        painter = Qt.QPainter(result_image)
        painter.setCompositionMode(Qt.QPainter.CompositionMode_Source)
        painter.drawImage(0, 0, frame_image)
        painter.setCompositionMode(Qt.QPainter.CompositionMode_DestinationIn)
        painter.drawImage(0, 0, alpha_image)
        if self.main.CHECKERBOARD_ENABLED:
            painter.setCompositionMode(
                Qt.QPainter.CompositionMode_DestinationOver)
            painter.drawImage(0, 0, self.checkerboard)
        painter.end()

        return result_image