34
34
MacOSGrabber::MacOSGrabber (QObject *parent, QList<QRgb> *grabResult, QList<GrabWidget *> *grabAreasGeometry):
35
35
TimeredGrabber(parent, grabResult, grabAreasGeometry)
36
36
{
37
+ _imageBuf = NULL ;
38
+ _imageBufSize = 0 ;
39
+ _colorSpace = CGColorSpaceCreateDeviceRGB ();
40
+ _context = NULL ;
41
+ _contextHeight = 0 ;
42
+ _contextWidth = 0 ;
37
43
}
38
44
39
45
MacOSGrabber::~MacOSGrabber ()
40
46
{
47
+ CGColorSpaceRelease (_colorSpace);
48
+ if (_imageBuf)
49
+ free (_imageBuf);
50
+ if (_context)
51
+ CGContextRelease (_context);
41
52
}
42
53
43
54
const char * MacOSGrabber::getName ()
@@ -50,28 +61,36 @@ void MacOSGrabber::updateGrabMonitor(QWidget *widget)
50
61
Q_UNUSED (widget);
51
62
}
52
63
53
- void imageCleanup (void *data) {
54
- free (data);
55
- }
56
-
57
64
QImage * MacOSGrabber::toImage (CGImageRef imageRef)
58
65
{
59
66
size_t width = CGImageGetWidth (imageRef);
60
67
size_t height = CGImageGetHeight (imageRef);
61
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
62
- unsigned char *rawData = (unsigned char *) calloc (height * width * 4 , sizeof (unsigned char ));
68
+ size_t new_buf_size = height * width * 4 ;
69
+ if (new_buf_size > _imageBufSize) {
70
+ DEBUG_LOW_LEVEL << Q_FUNC_INFO << " new width = " << width << " new height = " << height;
71
+ if (_imageBuf)
72
+ free (_imageBuf);
73
+ _imageBuf = (unsigned char *) calloc (height * width * 4 , sizeof (unsigned char ));
74
+ _imageBufSize = new_buf_size;
75
+ }
76
+
63
77
size_t bytesPerPixel = 4 ;
64
78
size_t bytesPerRow = bytesPerPixel * width;
65
79
size_t bitsPerComponent = 8 ;
66
- CGContextRef context = CGBitmapContextCreate (rawData, width, height,
67
- bitsPerComponent, bytesPerRow, colorSpace,
68
- kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
69
- CGColorSpaceRelease (colorSpace);
80
+ if (width != _contextWidth || height != _contextHeight) {
81
+ if (_context)
82
+ CGContextRelease (_context);
83
+
84
+ _context = CGBitmapContextCreate (_imageBuf, width, height,
85
+ bitsPerComponent, bytesPerRow, _colorSpace,
86
+ kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little );
87
+ _contextWidth = width;
88
+ _contextHeight = height;
89
+ }
70
90
71
- CGContextDrawImage (context, CGRectMake (0 , 0 , width, height), imageRef);
72
- CGContextRelease (context);
91
+ CGContextDrawImage (_context, CGRectMake (0 , 0 , _contextWidth, _contextHeight), imageRef);
73
92
74
- QImage * result = new QImage (rawData, width, height , QImage::Format_ARGB32, imageCleanup );
93
+ QImage * result = new QImage (_imageBuf, _contextWidth, _contextHeight , QImage::Format_RGB32 );
75
94
76
95
return result;
77
96
}
0 commit comments