From 4ded810ba97c0d721590148e0c55cd5e5de18951 Mon Sep 17 00:00:00 2001 From: tt2468 Date: Thu, 30 Apr 2020 20:09:13 -0700 Subject: [PATCH 1/3] Add optional `fileFormat` and `fileCompressionRatio` parameters to `TakeSourceScreenshot` --- src/WSRequestHandler_Sources.cpp | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/WSRequestHandler_Sources.cpp b/src/WSRequestHandler_Sources.cpp index 579f76b4..29ee2882 100644 --- a/src/WSRequestHandler_Sources.cpp +++ b/src/WSRequestHandler_Sources.cpp @@ -1437,6 +1437,8 @@ RpcResponse WSRequestHandler::SetSourceFilterVisibility(const RpcRequest& reques * @param {String} `sourceName` Source name. Note that, since scenes are also sources, you can also provide a scene name. * @param {String (optional)} `embedPictureFormat` Format of the Data URI encoded picture. Can be "png", "jpg", "jpeg" or "bmp" (or any other value supported by Qt's Image module) * @param {String (optional)} `saveToFilePath` Full file path (file extension included) where the captured image is to be saved. Can be in a format different from `pictureFormat`. Can be a relative path. +* @param {String (optional)} `fileFormat` Format to save the image file as. If not specified, tries to guess based on file extension. +* @param {int (optional)} `fileCompressionQuality` Compression ratio between -1 and 100 to save the file with. -1 is automatic, 1 is smallest file/most compression, 100 is largest file/least compression. Varies with image type. * @param {int (optional)} `width` Screenshot width. Defaults to the source's base width. * @param {int (optional)} `height` Screenshot height. Defaults to the source's base height. * @@ -1544,7 +1546,7 @@ RpcResponse WSRequestHandler::TakeSourceScreenshot(const RpcRequest& request) { QByteArrayList supportedFormats = QImageWriter::supportedImageFormats(); if (!supportedFormats.contains(pictureFormat)) { - QString errorMessage = QString("Unsupported picture format: %1").arg(pictureFormat); + QString errorMessage = QString("unsupported picture format: %1").arg(pictureFormat); return request.failed(errorMessage.toUtf8()); } @@ -1552,7 +1554,7 @@ RpcResponse WSRequestHandler::TakeSourceScreenshot(const RpcRequest& request) { QBuffer buffer(&encodedImgBytes); buffer.open(QBuffer::WriteOnly); if (!sourceImage.save(&buffer, pictureFormat)) { - return request.failed("Embed image encoding failed"); + return request.failed("embed image encoding failed"); } buffer.close(); @@ -1569,7 +1571,31 @@ RpcResponse WSRequestHandler::TakeSourceScreenshot(const RpcRequest& request) { QFileInfo filePathInfo(filePathStr); QString absoluteFilePath = filePathInfo.absoluteFilePath(); - if (!sourceImage.save(absoluteFilePath)) { + const char* fileFormat; + if (request.hasField("fileFormat")) { + fileFormat = obs_data_get_string(request.parameters(), "fileFormat"); + QByteArrayList supportedFormats = QImageWriter::supportedImageFormats(); + + if (!supportedFormats.contains(fileFormat)) { + QString errorMessage = QString("unsupported file format: %1").arg(fileFormat); + return request.failed(errorMessage.toUtf8()); + } + } + else { + fileFormat = nullptr; + } + + int fileCompressionQuality {-1}; + if (request.hasField("fileCompressionQuality")) { + fileCompressionQuality = obs_data_get_int(request.parameters(), "fileCompressionQuality"); + + if (fileCompressionQuality < -1 || fileCompressionQuality > 100) { + QString errorMessage = QString("compression quality out of range: %1").arg(fileCompressionQuality); + return request.failed(errorMessage.toUtf8()); + } + } + + if (!sourceImage.save(absoluteFilePath, fileFormat, fileCompressionQuality)) { return request.failed("Image save failed"); } obs_data_set_string(response, "imageFile", absoluteFilePath.toUtf8()); From 333ffa0e895fb16300b84b9b0c4acf7125534334 Mon Sep 17 00:00:00 2001 From: tt2468 Date: Thu, 30 Apr 2020 20:17:11 -0700 Subject: [PATCH 2/3] `compressionQuality` should be for embed and file saving --- src/WSRequestHandler_Sources.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/WSRequestHandler_Sources.cpp b/src/WSRequestHandler_Sources.cpp index 29ee2882..06f33380 100644 --- a/src/WSRequestHandler_Sources.cpp +++ b/src/WSRequestHandler_Sources.cpp @@ -1438,7 +1438,7 @@ RpcResponse WSRequestHandler::SetSourceFilterVisibility(const RpcRequest& reques * @param {String (optional)} `embedPictureFormat` Format of the Data URI encoded picture. Can be "png", "jpg", "jpeg" or "bmp" (or any other value supported by Qt's Image module) * @param {String (optional)} `saveToFilePath` Full file path (file extension included) where the captured image is to be saved. Can be in a format different from `pictureFormat`. Can be a relative path. * @param {String (optional)} `fileFormat` Format to save the image file as. If not specified, tries to guess based on file extension. -* @param {int (optional)} `fileCompressionQuality` Compression ratio between -1 and 100 to save the file with. -1 is automatic, 1 is smallest file/most compression, 100 is largest file/least compression. Varies with image type. +* @param {int (optional)} `compressionQuality` Compression ratio between -1 and 100 to write the image with. -1 is automatic, 1 is smallest file/most compression, 100 is largest file/least compression. Varies with image type. * @param {int (optional)} `width` Screenshot width. Defaults to the source's base width. * @param {int (optional)} `height` Screenshot height. Defaults to the source's base height. * @@ -1541,6 +1541,16 @@ RpcResponse WSRequestHandler::TakeSourceScreenshot(const RpcRequest& request) { OBSDataAutoRelease response = obs_data_create(); + int compressionQuality {-1}; + if (request.hasField("compressionQuality")) { + compressionQuality = obs_data_get_int(request.parameters(), "compressionQuality"); + + if (compressionQuality < -1 || compressionQuality > 100) { + QString errorMessage = QString("compression quality out of range: %1").arg(compressionQuality); + return request.failed(errorMessage.toUtf8()); + } + } + if (request.hasField("embedPictureFormat")) { const char* pictureFormat = obs_data_get_string(request.parameters(), "embedPictureFormat"); @@ -1553,7 +1563,7 @@ RpcResponse WSRequestHandler::TakeSourceScreenshot(const RpcRequest& request) { QByteArray encodedImgBytes; QBuffer buffer(&encodedImgBytes); buffer.open(QBuffer::WriteOnly); - if (!sourceImage.save(&buffer, pictureFormat)) { + if (!sourceImage.save(&buffer, pictureFormat, compressionQuality)) { return request.failed("embed image encoding failed"); } buffer.close(); @@ -1585,17 +1595,7 @@ RpcResponse WSRequestHandler::TakeSourceScreenshot(const RpcRequest& request) { fileFormat = nullptr; } - int fileCompressionQuality {-1}; - if (request.hasField("fileCompressionQuality")) { - fileCompressionQuality = obs_data_get_int(request.parameters(), "fileCompressionQuality"); - - if (fileCompressionQuality < -1 || fileCompressionQuality > 100) { - QString errorMessage = QString("compression quality out of range: %1").arg(fileCompressionQuality); - return request.failed(errorMessage.toUtf8()); - } - } - - if (!sourceImage.save(absoluteFilePath, fileFormat, fileCompressionQuality)) { + if (!sourceImage.save(absoluteFilePath, fileFormat, compressionQuality)) { return request.failed("Image save failed"); } obs_data_set_string(response, "imageFile", absoluteFilePath.toUtf8()); From 0eaa9187ead361d78079d874a181401b3f1400f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Lepin?= Date: Fri, 15 May 2020 02:35:53 +0200 Subject: [PATCH 3/3] requests(TakeSourceScreenshot): improved docs --- src/WSRequestHandler_Sources.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/WSRequestHandler_Sources.cpp b/src/WSRequestHandler_Sources.cpp index 06f33380..5e90e158 100644 --- a/src/WSRequestHandler_Sources.cpp +++ b/src/WSRequestHandler_Sources.cpp @@ -1437,7 +1437,7 @@ RpcResponse WSRequestHandler::SetSourceFilterVisibility(const RpcRequest& reques * @param {String} `sourceName` Source name. Note that, since scenes are also sources, you can also provide a scene name. * @param {String (optional)} `embedPictureFormat` Format of the Data URI encoded picture. Can be "png", "jpg", "jpeg" or "bmp" (or any other value supported by Qt's Image module) * @param {String (optional)} `saveToFilePath` Full file path (file extension included) where the captured image is to be saved. Can be in a format different from `pictureFormat`. Can be a relative path. -* @param {String (optional)} `fileFormat` Format to save the image file as. If not specified, tries to guess based on file extension. +* @param {String (optional)} `fileFormat` Format to save the image file as (one of the values provided in the `supported-image-export-formats` response field of `GetVersion`). If not specified, tries to guess based on file extension. * @param {int (optional)} `compressionQuality` Compression ratio between -1 and 100 to write the image with. -1 is automatic, 1 is smallest file/most compression, 100 is largest file/least compression. Varies with image type. * @param {int (optional)} `width` Screenshot width. Defaults to the source's base width. * @param {int (optional)} `height` Screenshot height. Defaults to the source's base height. @@ -1581,7 +1581,7 @@ RpcResponse WSRequestHandler::TakeSourceScreenshot(const RpcRequest& request) { QFileInfo filePathInfo(filePathStr); QString absoluteFilePath = filePathInfo.absoluteFilePath(); - const char* fileFormat; + const char* fileFormat = nullptr; if (request.hasField("fileFormat")) { fileFormat = obs_data_get_string(request.parameters(), "fileFormat"); QByteArrayList supportedFormats = QImageWriter::supportedImageFormats(); @@ -1591,9 +1591,6 @@ RpcResponse WSRequestHandler::TakeSourceScreenshot(const RpcRequest& request) { return request.failed(errorMessage.toUtf8()); } } - else { - fileFormat = nullptr; - } if (!sourceImage.save(absoluteFilePath, fileFormat, compressionQuality)) { return request.failed("Image save failed");