Skip to content

Commit

Permalink
Utils: Add method to write PPM file
Browse files Browse the repository at this point in the history
Write uncompressed image as PPM file
  • Loading branch information
rajat2004 committed Jan 1, 2021
1 parent b1ad3bf commit c7ff43a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
24 changes: 24 additions & 0 deletions AirLib/include/common/common_utils/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,30 @@ class Utils {
}
}
}

file.close();
}

static void writePPMfile(const uint8_t* const image_data, int width, int height, const std::string &path)
{
std::fstream file(path.c_str(), std::ios::out | std::ios::binary);

file << "P6\n"; // Magic type for PPM files
file << width << " " << height << "\n";
file << "255\n"; // Max color value

for (int i=0; i<height; i++) {
for (int j=0; j<width; j++) {
int id = (i*width + j)*3; // Pixel index

// Image is in BGR, write as RGB
file.write(reinterpret_cast<const char*>(&image_data[id+2]), sizeof(image_data[id+2])); // R
file.write(reinterpret_cast<const char*>(&image_data[id+1]), sizeof(image_data[id+1])); // G
file.write(reinterpret_cast<const char*>(&image_data[id]), sizeof(image_data[id])); // B
}
}

file.close();
}

template<typename T>
Expand Down
20 changes: 17 additions & 3 deletions Unreal/Plugins/AirSim/Source/Recording/RecordingFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ void RecordingFile::appendRecord(const std::vector<msr::airlib::ImageCaptureBase
<< vehicle_sim_api->getVehicleName() << "_"
<< response.camera_name << "_" <<
common_utils::Utils::toNumeric(response.image_type) << "_" <<
common_utils::Utils::getTimeSinceEpochNanos() <<
(response.pixels_as_float ? ".pfm" : ".png");
common_utils::Utils::getTimeSinceEpochNanos();

std::string extension;
if (response.pixels_as_float)
extension = ".pfm";
else if (response.compress)
extension = ".png";
else
extension = ".ppm";

image_file_name << extension;

if (i > 0)
image_file_names << ";";
Expand All @@ -31,11 +40,16 @@ void RecordingFile::appendRecord(const std::vector<msr::airlib::ImageCaptureBase

//write image file
try {
if (response.pixels_as_float) {
if (extension == ".pfm") {
common_utils::Utils::writePfmFile(response.image_data_float.data(), response.width, response.height,
image_full_file_path);
}
else if (extension == ".ppm") {
common_utils::Utils::writePPMfile(response.image_data_uint8.data(), response.width, response.height,
image_full_file_path);
}
else {
// Write PNG image, already compressed in binary
std::ofstream file(image_full_file_path, std::ios::binary);
file.write(reinterpret_cast<const char*>(response.image_data_uint8.data()), response.image_data_uint8.size());
file.close();
Expand Down

0 comments on commit c7ff43a

Please sign in to comment.