44import argparse
55import os
66import sys
7- from PySide6 .QtCore import Qt , QSize , QTimer , QByteArray
8- from PySide6 .QtGui import QImage , QPainter , QColor
9- from PySide6 .QtWidgets import QApplication , QMainWindow , QWidget , QVBoxLayout , QLabel
7+ from PySide6 .QtCore import Qt , QTimer
8+ from PySide6 .QtGui import QImage , QPainter , QColor , QPixmap
9+ from PySide6 .QtWidgets import QApplication , QMainWindow , QWidget
1010import numpy as np
1111
1212from moving_average import MovingAverage
@@ -48,29 +48,55 @@ def __init__(self, width, height, options: Options):
4848 super ().__init__ ()
4949 self .setMinimumSize (width , height )
5050 self .options = options
51- self .image = QImage (self .options .resolution [0 ], self .options .resolution [1 ], QImage .Format_RGB888 )
52- self .image .fill (QColor (255 , 255 , 0 ))
51+ width , height = options .resolution
52+ self .pixmap = QPixmap (self .width (), self .height ())
53+ self .pixmap .fill (QColor (255 , 0 , 0 ))
54+ self .painter = QPainter (self )
5355
5456 def paintEvent (self , event ):
5557 print ("paintEvent" )
56- painter = QPainter (self )
57- painter .drawImage (self .rect (), self .image )
58+
59+ self .painter .begin (self )
60+
61+ new_pixmap = self .scale_image (self .pixmap )
62+
63+ # Calculate the starting x and y coordinates to center the image
64+ start_x = (self .width () - new_pixmap .width ()) // 2
65+ start_y = (self .height () - new_pixmap .height ()) // 2
66+
67+ self .painter .drawPixmap (start_x , start_y , new_pixmap )
68+
69+ self .painter .end ()
70+
71+ def scale_image (self , image : QImage ) -> QImage :
72+ scale_width = self .width () / image .width ()
73+ scale_height = self .height () / image .height ()
74+ scale = min (scale_width , scale_height )
75+
76+ scaled_width = int (image .width () * scale )
77+ scaled_height = int (image .height () * scale )
78+
79+ scaled_width = min (scaled_width , self .width ())
80+ scaled_height = min (scaled_height , self .height ())
81+
82+ return image .scaled (scaled_width , scaled_height , Qt .KeepAspectRatio )
5883
5984 def update_image (self , buffer ):
60- now = time .time ()
6185 print ("update_image: buffer id:" , buffer .id )
62-
63- x_size , y_size = buffer .width , buffer .height
64- #for y in range(y_size):
65- # for x in range(x_size):
66- # color = buffer_get_color(buffer, x, y)
67- # rgb: int = buffer.bytes[y, x]
68- # bytes = [rgb >> 24, (rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF]
69- # color = QColor(bytes[1], bytes[2], bytes[3])
70- # self.image.setPixelColor(x, y, color)
86+ now = time .time ()
87+ # x_size, y_size = buffer.width, buffer.height
88+ # for y in range(y_size):
89+ # for x in range(x_size):
90+ # rgb: int = buffer.bytes[y, x]
91+ # bytes = [rgb >> 24, (rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF]
92+ # color = QColor(bytes[1], bytes[2], bytes[3])
93+ # self.image.setPixelColor(x, y, color)
7194
7295 # np_array = np.array(buffer, copy = False) # This is possible thanks to buffer protocol
73- self .image = QImage (buffer .bytes , buffer .height , buffer .width , QImage .Format_RGB32 )
96+ # self.image = QImage(np_array, buffer.width, buffer.height, QImage.Format_RGB32)
97+
98+ image = QImage (buffer .bytes , buffer .width , buffer .height , QImage .Format_RGB32 )
99+ self .pixmap = self .pixmap .fromImage (image )
74100
75101 took = time .time () - now
76102 print ("update_image: took" , took , "seconds" )
@@ -80,7 +106,7 @@ class MainWindow(QMainWindow):
80106 def __init__ (self , options : Options ):
81107 super ().__init__ ()
82108 self .setWindowTitle ("EVDI virtual monitor" )
83- self .image_buffer_widget = ImageBufferWidget (400 , 300 , options )
109+ self .image_buffer_widget = ImageBufferWidget (128 , 128 , options )
84110 self .setCentralWidget (self .image_buffer_widget )
85111
86112 def resizeEvent (self , event ):
0 commit comments