2015-01-24 23:42:22 +01:00
|
|
|
// STL includes
|
|
|
|
#include <cassert>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
// Local includes
|
2016-03-09 11:03:20 +01:00
|
|
|
#include <grabber/OsxFrameGrabber.h>
|
2015-01-24 23:42:22 +01:00
|
|
|
|
2017-08-04 23:08:15 +02:00
|
|
|
OsxFrameGrabber::OsxFrameGrabber(const unsigned display, const unsigned width, const unsigned height)
|
|
|
|
: Grabber("OSXGRABBER", width, height)
|
|
|
|
, _screenIndex(display)
|
2015-01-24 23:42:22 +01:00
|
|
|
{
|
|
|
|
CGImageRef image;
|
|
|
|
CGDisplayCount displayCount;
|
|
|
|
CGDirectDisplayID displays[8];
|
|
|
|
|
|
|
|
// get list of displays
|
|
|
|
CGGetActiveDisplayList(8, displays, &displayCount);
|
|
|
|
if (_screenIndex + 1 > displayCount)
|
|
|
|
{
|
2016-07-12 00:53:48 +02:00
|
|
|
Error(_log, "Display with index %d is not available. Using main display", _screenIndex);
|
2015-01-24 23:42:22 +01:00
|
|
|
_display = kCGDirectMainDisplay;
|
|
|
|
} else {
|
|
|
|
_display = displays[_screenIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
image = CGDisplayCreateImage(_display);
|
|
|
|
assert(image != NULL);
|
|
|
|
|
2016-07-12 00:53:48 +02:00
|
|
|
Info(_log, "Display opened with resolution: %dx%d@%dbit", CGImageGetWidth(image), CGImageGetHeight(image), CGImageGetBitsPerPixel(image));
|
2015-01-24 23:42:22 +01:00
|
|
|
|
|
|
|
CGImageRelease(image);
|
|
|
|
}
|
|
|
|
|
|
|
|
OsxFrameGrabber::~OsxFrameGrabber()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void OsxFrameGrabber::grabFrame(Image<ColorRgb> & image)
|
|
|
|
{
|
|
|
|
CGImageRef dispImage;
|
|
|
|
CFDataRef imgData;
|
|
|
|
unsigned char * pImgData;
|
|
|
|
unsigned dspWidth, dspHeight;
|
|
|
|
|
|
|
|
dispImage = CGDisplayCreateImage(_display);
|
|
|
|
|
2017-08-04 23:08:15 +02:00
|
|
|
// display lost, use main
|
2015-01-24 23:42:22 +01:00
|
|
|
if (dispImage == NULL && _display)
|
|
|
|
{
|
|
|
|
dispImage = CGDisplayCreateImage(kCGDirectMainDisplay);
|
|
|
|
// no displays connected, return
|
|
|
|
if (dispImage == NULL)
|
|
|
|
{
|
2016-07-12 00:53:48 +02:00
|
|
|
Error(_log, "No display connected...");
|
2015-01-24 23:42:22 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
imgData = CGDataProviderCopyData(CGImageGetDataProvider(dispImage));
|
|
|
|
pImgData = (unsigned char*) CFDataGetBytePtr(imgData);
|
|
|
|
dspWidth = CGImageGetWidth(dispImage);
|
|
|
|
dspHeight = CGImageGetHeight(dispImage);
|
|
|
|
|
2017-08-04 23:08:15 +02:00
|
|
|
_imageResampler.setHorizontalPixelDecimation(dspWidth/_width);
|
|
|
|
_imageResampler.setVerticalPixelDecimation(dspHeight/_height);
|
|
|
|
_imageResampler.processImage( pImgData,
|
2015-01-24 23:42:22 +01:00
|
|
|
dspWidth,
|
|
|
|
dspHeight,
|
|
|
|
CGImageGetBytesPerRow(dispImage),
|
|
|
|
PIXELFORMAT_BGR32,
|
|
|
|
image);
|
|
|
|
|
|
|
|
CFRelease(imgData);
|
|
|
|
CGImageRelease(dispImage);
|
|
|
|
}
|