libcudaisp  0.1.4
framereader.cpp

Functions to read and write the images

/*
* Copyright (C) 2023 RidgeRun, LLC (http://www.ridgerun.com)
* All Rights Reserved.
*
* The contents of this software are proprietary and confidential to RidgeRun,
* LLC. No part of this program may be photocopied, reproduced or translated
* into another programming language without prior written consent of
* RidgeRun, LLC. The user is free to modify the source code after obtaining
* a software license from RidgeRun. All source code changes must be provided
* back to RidgeRun without any encumbrance.
*/
#include "framereader.hpp" /* NOLINT */
#include <experimental/filesystem>
#include <fstream>
#include <iostream>
bool ReadFrame(const std::string &filename, uint8_t *memblock,
size_t target_size) {
std::streampos size;
std::ifstream file(filename, std::ios::in | std::ios::binary | std::ios::ate);
if (!memblock) {
std::cerr << "Invalid pointer" << std::endl;
return false;
}
if (!file.is_open()) {
std::cerr << "Cannot open file: " << filename << std::endl;
return false;
}
size = file.tellg();
int converted_target_size = static_cast<int>(target_size);
if (size != converted_target_size) {
std::cerr << "Sizes mismatch. Expected: " << target_size
<< " Received: " << size << std::endl;
return false;
}
file.seekg(0, std::ios::beg);
char *readblock = reinterpret_cast<char *>(memblock);
file.read(readblock, size);
file.close();
return true;
}
bool WriteFrame(const std::string &filename, uint8_t *memblock,
size_t target_size) {
std::streampos size;
std::ofstream file(filename,
std::ios::out | std::ios::binary | std::ios::ate);
if (!memblock) {
std::cerr << "Invalid pointer" << std::endl;
return false;
}
if (!file.is_open()) {
std::cerr << "Cannot open file: " << filename << std::endl;
return false;
}
char *writeblock = reinterpret_cast<char *>(memblock);
file.write(writeblock, target_size);
file.close();
return true;
}