Save an image to a PPM image file. Returns true if file is successfully written, false otherwise.
| 1205 | // Save an image to a PPM image file. |
| 1206 | // Returns true if file is successfully written, false otherwise. |
| 1207 | static bool writeScreenshot(ScreenshotQueueData &data) { |
| 1208 | PROFILE("screenshot.write"); |
| 1209 | // Map the final image so that the CPU can read it. |
| 1210 | const VkImageSubresource sr = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0}; |
| 1211 | VkSubresourceLayout srLayout; |
| 1212 | const char *pixels; |
| 1213 | if (data.image3 == VK_NULL_HANDLE) { |
| 1214 | data.pTableDevice->GetImageSubresourceLayout(data.device, data.image2, &sr, &srLayout); |
| 1215 | VkResult err = data.pTableDevice->MapMemory(data.device, data.mem2, 0, VK_WHOLE_SIZE, 0, (void **)&pixels); |
| 1216 | if (VK_SUCCESS != err) return false; |
| 1217 | } else { |
| 1218 | data.pTableDevice->GetImageSubresourceLayout(data.device, data.image3, &sr, &srLayout); |
| 1219 | VkResult err = data.pTableDevice->MapMemory(data.device, data.mem3, 0, VK_WHOLE_SIZE, 0, (void **)&pixels); |
| 1220 | if (VK_SUCCESS != err) return false; |
| 1221 | } |
| 1222 | |
| 1223 | pixels += srLayout.offset; |
| 1224 | |
| 1225 | string fileName; |
| 1226 | if (settings.targetFolder.empty()) { |
| 1227 | fileName = to_string(data.frameNumber); |
| 1228 | } else { |
| 1229 | fileName = settings.targetFolder; |
| 1230 | fileName += "/" + to_string(data.frameNumber); |
| 1231 | } |
| 1232 | |
| 1233 | bool writeResult; |
| 1234 | switch (settings.screenshotExtension) { |
| 1235 | case Settings::ScreenshotExtension::PPM: |
| 1236 | fileName += ".ppm"; |
| 1237 | writeResult = writePPM(fileName.c_str(), pixels, data.dstWidth, data.dstHeight, data.dstNumChannels, srLayout.rowPitch); |
| 1238 | break; |
| 1239 | case Settings::ScreenshotExtension::PAM: |
| 1240 | fileName += ".pam"; |
| 1241 | writeResult = writePAM(fileName.c_str(), pixels, data.dstWidth, data.dstHeight, data.dstNumChannels, srLayout.rowPitch); |
| 1242 | break; |
| 1243 | } |
| 1244 | |
| 1245 | if (data.image3 == VK_NULL_HANDLE) { |
| 1246 | data.pTableDevice->UnmapMemory(data.device, data.mem2); |
| 1247 | } else { |
| 1248 | data.pTableDevice->UnmapMemory(data.device, data.mem3); |
| 1249 | } |
| 1250 | |
| 1251 | if (!writeResult) { |
| 1252 | #ifdef ANDROID |
| 1253 | __android_log_print(ANDROID_LOG_ERROR, "screenshot", "Failed to write image: %s", fileName.c_str()); |
| 1254 | #else |
| 1255 | fprintf(stderr, "screenshot: Failed to write image: %s\n", fileName.c_str()); |
| 1256 | #endif |
| 1257 | return false; |
| 1258 | } |
| 1259 | #ifdef ANDROID |
| 1260 | __android_log_print(ANDROID_LOG_INFO, "screenshot", "Saved image: %s", fileName.c_str()); |
| 1261 | #else |
| 1262 | printf("screenshot: Saved image: %s \n", fileName.c_str()); |
| 1263 | fflush(stdout); |
| 1264 | #endif |
no test coverage detected