Png signature reading fail - c++

I am trying to read the signature of png image file using std::ifstream opened it in binary mode but I got only few bytes correct. Surely using png image for input.
Just I want to validate my png image, whats going wrong here.
#include <fstream>
#include <iostream>
using namespace std;
const auto PNG_SIGN_SIZE = 8;
const uint8_t PNG_SIGN_BUFFER[] = {137, 80, 78, 71, 13, 10, 26, 10};
bool validate(const string& filename) {
ifstream ifs;
ifs.open(filename, ios::binary);
bool is_valid = true;
if (ifs.is_open()) {
uint8_t buffer[PNG_SIGN_SIZE]{};
for (int i = 0; i < PNG_SIGN_SIZE; ++i) {
ifs >> buffer[i];
if (ifs.fail()) {
printf("ERROR: Fail to read bytes.\n");
is_valid = false;
break;
}
}
for (int i = 0; i < PNG_SIGN_SIZE; ++i) {
// printf("%u %u\n", buffer[i], PNG_SIGN_BUFFER[i]);
if (buffer[i] != PNG_SIGN_BUFFER[i]) {
is_valid = false;
break;
}
}
} else {
printf("ERROR: Could not read <%s>.\n", filename.c_str());
is_valid = false;
}
ifs.close();
return is_valid;
}
int main() {
string filename = "baby.png";
auto res = validate(filename);
cout << boolalpha << res;
return 0;
}

Related

Segmentation Fault(core dumped) when invoking execute() of an ExecutionContext object in TensorRT-8.4

My colleague built and serialized a TenosrRT engine. She handed me the serialized engine and an input image in two binary files. The image file contains 640x480x3 float numbers. In the following code snippet, I load the engine and the image data for inference, but when execute() method is called, segmentation fault occurs. I geuss there might be something wrong with the way I create buffers that causes the error. Any idea on how I can solve this?
#define OUTPUT_SIZE 1000
#define BATCH_SIZE 1
#define IMAGE_WIDTH 640
#define IMAGE_HEIGHT 480
bool InitAndInfer(std::string engine_path,std::string image_path)
{
std::ifstream trt_stream(engine_path, std::ifstream::binary | std::ifstream::in);
if (trt_stream.is_open()) {
trt_stream.seekg(0, trt_stream.end);
int size = trt_stream.tellg();
trt_stream.seekg(0, trt_stream.beg);
char *trtModel = new char[size];
trt_stream.read(trtModel, size);
trt_stream.close();
TensorLogger logger;
nvinfer1::IRuntime *runtime = createInferRuntime(logger);
nvinfer1::ICudaEngine *engine = runtime->deserializeCudaEngine(trtModel, size);
if (engine == nullptr) {
std::cout<<"Engine init failed\n";
return false;
}
std::cout<<"Ready to create context\n";
nvinfer1::IExecutionContext *context = engine->createExecutionContext();
if (context = nullptr) {
std::cout<<"Engine created failed!\n";
return false;
}
std::cout<<"Context created\n";
int binding_indecis = engine->getNbBindings();
std::cout<<"Engine binding num = "<<binding_indecis<<std::endl;
std::string binding_names[binding_indecis];
for (int i = 0; i < binding_indecis; i++) {
binding_names[i] = engine->getBindingName(i);
std::cout<<"The binding name for "<<i<<" is "<<binding_names[i]<<std::endl;
}
std::cout<<"Binding indecis got\n";
float image_data[640*480*3];
int image_pos{0};
std::ifstream image_stream(image_path, std::ifstream::binary | std::ifstream::in);
while(true) {
std::string line;
if (std::getline(image_stream, line)) {
std::stringstream ss(line);
while(ss>>image_data[image_pos]) {
image_pos++;
}
} else {
break;
}
}
std::cout<<image_pos<<" float num has been read\n";
void *buffers[4];
if (cudaSuccess != cudaMalloc(&buffers[0], 3*BATCH_SIZE*IMAGE_HEIGHT*IMAGE_WIDTH*sizeof(float)))
std::cout<<"Error when cudaMalloc for buffer[0]\n";
if (cudaSuccess != cudaMalloc(&buffers[1], BATCH_SIZE*OUTPUT_SIZE*sizeof(float)))
std::cout<<"Error when cudaMalloc for buffer[1]\n";
if (cudaSuccess != cudaMalloc(&buffers[2], BATCH_SIZE*100000000*sizeof(float)))
std::cout<<"Error when cudaMalloc for buffer[2]\n";
if (cudaSuccess != cudaMalloc(&buffers[3], BATCH_SIZE*100000000*sizeof(float)))
std::cout<<"Error when cudaMalloc for buffer[3]\n";
if (cudaSuccess != cudaMemcpy(buffers[0], image_data, 3*BATCH_SIZE*IMAGE_HEIGHT*IMAGE_WIDTH*sizeof(float), cudaMemcpyHostToDevice))
std::cout<<"Error when cudaMemcpy\n";
std::cout<<"Engine max batch size="<<engine->getMaxBatchSize()<<std::endl;
std::cout<<"Ready to infer\n";
context->execute(BATCH_SIZE, buffers);
std::cout<<"Infer finished\n";
char *result = static_cast<char*>(buffers[1]);
std::cout<<result<<std::endl;
} else {
std::cout<<"Can't open trt engine file - "<<engine_path<<std::endl;
return false;
}
return true;
}
And the runtime log is put below:

Find a number in a binary file, rotate and overwrite

I'm trying to find an integer (6664) in a binary file (file.bin) and I must shift this integer to the right once and write this result to the same position the integer was found. However, I am not able to overwrite the original value with the shifted value. I developed the code below:
#include <cstdio>
#include <cstring>
int main(){
//opening file
FILE *pfile
pfile = fopen("file.bin","r+b");
//ffinding file size
fseek(pfile, 0L, SEEK_END);
long size= ftell(pfile);
rewind(pfile);
//reading data
int vec[size];
fread(vec, sizeof(int), size, pfile);
for(int i = 0; i<size; i++){
if(vec[i]==6664){
int aux = vec[i]>>1;
fseek(pfile, i, SEEK_SET);
fwrite(&aux, sizeof(int), 1, pfile);
}
}
return 0;
}
I generated the file with the following command:
echo "0000000: 6408 0623 77ef bfbd efbf bdef bfbd 2779 d..#w.........'y
0000010: efbf bdef 081a 0000 0000 efbf bdef bfbd ................
0000020: 4577 efbf bdef bfbd efbf bd00 Ew.........." | xxd -r > file.bin
Any thoughts?
IMHO, you should read in chunks. There is no guarantee that your platform or executable has enough memory to read in the file (files can be huge).
static const size_t QUANTITY_INTEGERS = 1024u * 1024u;
uint16_t buffer[QUANTITY_INTEGERS] = {0};
std::ifstream number_file("file.bin", ios::binary);
while (number_file.read((char *) &buffer[0], QUANTITY_INTEGERS * sizeof(uint16_t)))
{
unsigned int numbers_read = number_file.gcount();
uint16_t * p_number = std::find(&buffer[0], &buffer[numbers_read], 0x6664);
if (p_number != &buffer[numbers_read])
{
std::cout << "0x6664 found.\n";
break;
}
}
You can use whatever buffer capacity you want.
Aside from the complete lack of error handling, you are not adequately differentiating between bytes and integers.
Your size variable is expressed in bytes, but you are using it as a count when dealing with your vec array. As such, you are over-allocating your array, reading too many ints from the file into the array, and iterating through too many elements of the array.
Also, when writing back to the file, you are treating the loop counter i as a byte offset, which it is not, thus you are writing to the wrong offset in the file.
Try something more like this instead:
#include <cstdio>
#include <cstdint>
#include <vector>
typedef int32_t myint_t; // or int16_t, if needed...
int main()
{
//opening file
FILE *pfile = fopen("file.bin", "r+b");
if (!pFile) return -1
//finding file size
//TODO: use f/stat() or std::filesystem::file_size() instead
if (fseek(pfile, 0L, SEEK_END) < 0) {
fclose(pFile);
return -1;
}
long size = ftell(pfile);
if (size < 0) {
fclose(pFile);
return -1;
}
rewind(pfile);
//reading data
std::vector<myint_t> vec(size / sizeof(myint_t));
size_t numItems = fread(vec.data(), sizeof(myint_t), vec.size(), pfile);
for(size_t i = 0; i < numItems; ++i) {
if (vec[i] == 6664) {
vec[i] >>= 1;
// TODO: it is more efficient for the filesystem to make
// smaller relative seeks using SEEK_CUR than to make
// larger seeks from the beginning using SEEK_SET...
if (fseek(pfile, i * sizeof(myint_t), SEEK_SET) < 0) {
fclose(pFile);
return -1;
}
if (fwrite(&vec[i], sizeof(myint_t), 1, pfile) != 1) {
fclose(pFile);
return -1;
}
}
}
fclose(pfile);
return 0;
}
That being said, consider using C++-style file I/O instead of C-style file I/O, eg:
#include <iostream>
#include <vector>
#include <cstdint>
using myint_t = int32_t; // or int16_t, if needed...
int main()
{
std::fstream fs;
fs.exceptions(std::fstream::failbit | std::fstream::badbit);
try {
//opening file
fs.open("file.bin", std::ios::in | std::ios::out | std::ios::binary | std::ios::ate);
//finding file size
//TODO: use f/stat() or std::filesystem::file_size() instead
long size = fs.tellg();
fs.seekg(0);
//reading data
std::vector<myint_t> vec(size / sizeof(myint_t));
fs.read(reinterpret_cast<char*>(vec.data()), sizeof(myint_t) * vec.size());
size_t numItems = fs.gcount() / sizeof(myint_t);
for(size_t i = 0; i < numItems; ++i) {
if (vec[i] == 6664) {
vec[i] >>= 1;
// TODO: it is more efficient for the filesystem to make
// smaller relative seeks using std::ios::cur than to make
// larger seeks from the beginning...
fs.seekp(i * sizeof(myint_t));
fs.write(reinterpret_cast<char*>(&vec[i]), sizeof(myint_t));
}
}
}
catch(...) {
return -1;
}
return 0;
}
Though, the following would be a little bit simpler and safer:
#include <iostream>
#include <vector>
#include <filesystem>
#include <cstdint>
using myint_t = int32_t; // or int16_t, if needed...
int main()
{
std::ifstream ifs;
std::ofstream ofs;
ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
ofs.exceptions(std::ofstream::failbit | std::ofstream::badbit);
try {
//opening files
ifs.open("file.bin", std::ios::binary);
ofs.open("file.new", std::ios::binary);
//reading data
myint_t value;
bool found = false;
while (ifs.read(reinterpret_cast<char*>(&value), sizeof(value))) {
if (value == 6664) {
value >>= 1;
found = true;
}
ofs.write(reinterpret_cast<char*>(&value), sizeof(value));
}
ifs.close();
ofs.close();
if (found) {
std::filesystem::rename("file.bin", "file.bak");
std::filesystem::rename("file.new", "file.bin");
std::filesystem::remove("file.bak");
}
else {
std::filesystem::remove("file.new");
}
}
catch(...) {
return -1;
}
return 0;
}

ifstream read reading random char for the whole stream

I'm trying to implement the function from listing 5.1 here
but when copying into a buffer with read from a file I just get the same character (Í) for the whole array, where string.txt is a copy and paste from the previous link content.
Here is my code:
#include <iostream>
#include <fstream>
#include <string>
#include <cinttypes>
#include <cstdio>
#include <cstring>
const int block_size = 0x4000; //16KB
int search(char* buffer, int searchLength, char* stringToSearch, int stringToSearchLength) {
char * potentialMatch;
while (searchLength) {
potentialMatch = reinterpret_cast<char *>(memchr(buffer, *stringToSearch, searchLength));
if (potentialMatch == NULL)
break;
if (stringToSearchLength == 1) {
return 1;
} else {
if (!memcmp(potentialMatch + 1, stringToSearch + 1, stringToSearchLength - 1))
return 1;
}
searchLength -= potentialMatch - buffer + 1;
buffer = potentialMatch + 1;
}
return 0;
}
int main(int argc, char* argv[]) {
char *toSearch = "Interpreting Where";
int done = 0;
int found = 0;
char *buffer;
int64_t fileSizeLeft = 0;
std::ifstream myFile("string.txt");
if (!myFile.fail()) {
buffer = new char[block_size];
myFile.seekg(0, std::ios::end); //Get file's size
fileSizeLeft = myFile.tellg();
} else {
std::cout << "Cannot open file" << std::endl;
return 1;
}
int toSearchLength = strlen(toSearch);
int stringLeft = toSearchLength - 1;
int first_time = 1;
while (!done && fileSizeLeft > toSearchLength) {
if (first_time) {
myFile.read(buffer, block_size);
found = search(buffer, block_size, toSearch, toSearchLength);
} else {
memcpy(buffer, buffer + stringLeft, stringLeft);
myFile.read(buffer+stringLeft, fileSizeLeft-stringLeft);
found = search(buffer, block_size, toSearch, toSearchLength);
}
fileSizeLeft = fileSizeLeft - block_size;
first_time = 0;
}
if (found) {
std::cout << "String found" << std::endl;
} else {
std::cout << "String not found" << std::endl;
}
myFile.close();
delete[] buffer;
return 0;
}
I hope you can help me see what I'm doing wrong, thanks!
You are setting myFile's position to ios_base::end with seekg:
myFile.seekg(0, ios::end);
Then trying to read from it:
myFile.read(buffer, block_size);
Clearly no data will be read since myFile is already at ios_base::end. And you'll be reading whatever uninitialized data that was already in buffer
What you probably intended to do was to set your myFile position back to the beginning by doing this before reading:
myFile.seekg(0, ios::beg);

Adding values to char* var from txt file

Example file:
[16bpp] Ponete el cinturon *-*
arfield
Nothing (cumpleanios):
Alkon^
~~|Tampon)
[16bpp] Chico Tonto.
Budin
16bpp] Leooooooooo!!!!!
Ev
16bpp] fedee
etamod
:) mAnKeAno
I want each name on a different array position...
I tried this code:
int c;
FILE *file;
file = fopen("bots.txt", "r");
if (file){
char *buffer;
char *jugadores = new char[1000];
int p;
int pos;
while ((c = getc(file)) != EOF){
if (c == '\n'){
strcpy(jugadores[p], buffer);
p++;
buffer = "";
pos = 0;
} else {
buffer[pos] = c;
pos++;
}
}
fclose(file);
}
But it doesn't even compile...
In php the right code would be something like this:
$data = file_get_contents("file.txt");
$names = explode("\n", $data);
Your code has several flaws in it. You need something more like this instead:
FILE *file = fopen("bots.txt", "r");
if (file)
{
char** lines = new char*[1000];
int maxlines = 1000;
int numlines = 0;
char *buffer = new char[1024];
int maxbuf = 1024;
int buflen = 0;
char *line;
int c;
while ((c = getc(file)) != EOF)
{
if (c == '\n')
{
if (numlines == maxlines)
{
char** tmplines = new char*[maxlines+1000];
memcpy(tmplines, lines, sizeof(char*)*maxlines);
delete[] lines;
lines = tmplines;
maxlines += 1000;
}
line = new char[buflen+1];
memcpy(line, buffer, sizeof(char)*buflen);
line[buflen] = 0;
lines[numlines] = line
numlines++;
buflen = 0;
}
else
{
if (buflen == maxbuf)
{
char* tmpbuf = new char[maxbuf+1024];
memcpy(tmpbuf, buffer, sizeof(char)*maxbuf);
delete[] buffer;
buffer = tmpbuf;
maxbuf += 1024;
}
buffer[buflen] = c;
buflen++;
}
}
fclose(file);
if (buflen > 0)
{
if (numlines == maxlines)
{
char** tmplines = new char*[maxlines+1000];
memcpy(tmplines, lines, sizeof(char*)*maxlines);
delete[] lines;
lines = tmplines;
maxlines += 1000;
}
line = new char[buflen+1];
memcpy(line, buffer, sizeof(char)*buflen);
line[buflen] = 0;
lines[numlines] = line
numlines++;
}
delete[] buffer;
// use lines up to numlines elements as needed...
for (int i = 0; i < numlines; i++)
printf("%s\n", lines[i]);
for (int i = 0; i < numlines; ++i)
delete[] lines[i];
delete[] lines;
}
With that said, since you are using C++, you should use C++ classes that will help manage everything for you. Try something more like this instead:
#include <fstream>
#include <ostream>
#include <string>
#include <vector>
std::ifstream file("bots.txt");
if (file.is_open())
{
std::string line;
std::vector<std::string> lines;
while (std::getline(file, line))
lines.push_back(line);
file.close();
// use lines as needed...
for (int i = 0; i < lines.size(); i++)
std::cout << lines[i] << std::endl;
}

Saving Byte Array to a RAW file format

I have a simple program that reads data from a PNG into a 2D array. I would like to save that data to a .RAW file so that Raw Studio or Irfanview can view the raw image that my program outputs to my_out.raw. Currently if I just write the raw binary data to the my_out.raw file, neither application can actually read the file, that is view the image. What do I need to do to the program below so that I can see the image?
The code to read the PNG files is:
// MAIN.cpp
#include "pngfilereader.h"
#include <string>
#include <vector>
#include <fstream>
int main (int argc, char *argv[])
{
PNGFileReader pngfr;
if (!pngfr.decompress_png_to_raw(std::string("/home/matt6809/Downloads"
"/City.png"))) {
std::cout << "File decompression error: " << std::endl;
} else {
std::ofstream out;
out.open("./my_out.raw", std::ios_base::out);
std::vector<std::vector<unsigned char> > data;
pngfr.get_image_data(data);
typedef std::vector<std::vector<unsigned char> >::iterator row_it;
typedef std::vector<unsigned char>::iterator col_it;
for(row_it rit= data.begin(); rit != data.end(); ++rit) {
for(col_it cit = rit->begin(); cit != rit->end(); ++cit) {
out << (*cit);
}
}
out << std::endl;
}
return 0;
}
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <png.h>
#include <iostream>
#include <vector>
#include <string>
class PNGFileReader
{
public:
PNGFileReader();
~PNGFileReader();
// Public exposed API:
bool compress_raw_to_png(uint8_t data, int size);
bool decompress_png_to_raw(const std::string &path);
// Getters
long unsigned int get_image_width();
long unsigned int get_image_height();
void get_image_data(std::vector<std::vector<unsigned char> > &data);
private:
// Helper functions:
bool read_png(const std::string &path);
bool create_png_structs(FILE *fp);
bool free_data();
bool alloc_data();
// Member variables:
png_structp m_pPNG;
png_infop m_pPNGInfo;
png_infop m_pPNGEndInfo;
png_bytepp m_Data;
long unsigned int m_ImageWidth;
long unsigned int m_ImageHeight;
// Enums
enum PNGBOOL {NOT_PNG, PNG};
enum PNGERRORS {ERROR, SUCCESS};
};
#include "pngfilereader.h"
#include <stdexcept>
PNGFileReader::PNGFileReader() :
m_pPNG(NULL),
m_pPNGInfo(NULL),
m_pPNGEndInfo(NULL),
m_Data(NULL),
m_ImageWidth(0),
m_ImageHeight(0)
{
}
PNGFileReader::~PNGFileReader()
{
for (unsigned long int i = 0; i < m_ImageHeight; ++i) {
if (m_Data[i]) {
delete m_Data[i];
m_Data[i] = NULL;
}
}
if (m_Data) {
delete m_Data;
m_Data = NULL;
}
}
// Public Exposed API
bool PNGFileReader::compress_raw_to_png(uint8_t m_Data, int size)
{
return PNGFileReader::SUCCESS;
}
bool PNGFileReader::decompress_png_to_raw(const std::string &path)
{
return read_png(path);
}
// Getters
long unsigned int PNGFileReader::get_image_width()
{
return m_ImageWidth;
}
long unsigned int PNGFileReader::get_image_height()
{
return m_ImageHeight;
}
void PNGFileReader::get_image_data(
std::vector<std::vector<unsigned char> > &data)
{
for (unsigned long int i = 0; i < m_ImageHeight; ++i) {
std::vector<unsigned char> v;
data.push_back(v);
for (unsigned long int j = 0; j < m_ImageWidth; ++j) {
std::vector<unsigned char> *vp = &data[i];
vp->push_back(m_Data[i][j]);
}
}
}
// Private Methods
bool PNGFileReader::read_png(const std::string &path)
{
/*
* Open up the file to read (path) in binary mode
* first so that if anything goes wrong with libpng
* we won't have much to undo
*/
const char *c_path = path.c_str();
FILE *fp = fopen(c_path, "rb");
if (!fp)
return PNGFileReader::ERROR;
/*
* Read the first BYTES_TO_READ bytes from file
* then determine if it is a png file or
* not. If png_sig_cmp == 0 all is okay
*/
enum {BYTES_TO_READ = 8};
unsigned char sig[BYTES_TO_READ];
if (!fread(sig, 1, BYTES_TO_READ, fp)) {
fclose(fp);
return PNGFileReader::ERROR;
}
bool is_png = !png_sig_cmp(sig, 0, BYTES_TO_READ);
if (!is_png) {
fclose(fp);
return PNGFileReader::ERROR;
}
if (!this->create_png_structs(fp)) {
fclose(fp);
return PNGFileReader::ERROR;
}
/*
* For error handling purposes. Set a long pointer
* back to this function to handle all error related
* to file IO
*/
if (setjmp(png_jmpbuf(m_pPNG)))
{
png_destroy_read_struct(&m_pPNG, &m_pPNGInfo, &m_pPNGEndInfo);
fclose(fp);
return PNGFileReader::ERROR;
}
/*
* Set up the input code for FILE openend in binary mode,
* and tell libpng we have already read BYTES_TO_READ btyes from
* signature
*/
png_init_io(m_pPNG, fp);
png_set_sig_bytes(m_pPNG, BYTES_TO_READ);
/*
* Using the lowlevel interface to lib png ...
*/
png_read_info(m_pPNG, m_pPNGInfo);
m_ImageHeight = png_get_image_height(m_pPNG, m_pPNGInfo);
m_ImageWidth = png_get_rowbytes(m_pPNG, m_pPNGInfo);
this->alloc_data();
png_read_image(m_pPNG, m_Data);
png_read_end(m_pPNG, NULL);
png_destroy_read_struct(&m_pPNG, &m_pPNGInfo, &m_pPNGEndInfo);
fclose(fp);
return PNGFileReader::SUCCESS;
}
bool PNGFileReader::create_png_structs(FILE *fp)
{
/*
* Create the pointer to main libpng struct, as well as
* two info structs to maintain information after, and
* prior to all operations on png m_Data. Only necessary
* to release resource after function succeeds.
*/
m_pPNG = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
NULL, NULL);
if (!m_pPNG)
{
fclose(fp);
return PNGFileReader::ERROR;
}
m_pPNGInfo = png_create_info_struct(m_pPNG);
if (!m_pPNGInfo)
{
png_destroy_read_struct(&m_pPNG, (png_infopp)NULL,(png_infopp)NULL);
fclose(fp);
return PNGFileReader::ERROR;
}
m_pPNGEndInfo = png_create_info_struct(m_pPNG);
if (!m_pPNGEndInfo)
{
png_destroy_read_struct(&m_pPNG, &m_pPNGInfo, (png_infopp)NULL);
fclose(fp);
return PNGFileReader::ERROR;
}
return PNGFileReader::SUCCESS;
}
bool PNGFileReader::free_data()
{
if (m_ImageHeight == 0 || m_ImageWidth == 0)
return PNGFileReader::ERROR;
for (unsigned long int i = 0; i < m_ImageHeight; ++i) {
if (m_Data[i]) {
delete m_Data[i];
m_Data[i] = NULL;
}
}
if (m_Data) {
delete m_Data;
m_Data = NULL;
}
return PNGFileReader::SUCCESS;
}
bool PNGFileReader::alloc_data()
{
if (m_ImageHeight == 0 || m_ImageWidth == 0)
return PNGFileReader::ERROR;
if (m_Data != NULL)
this->free_data();
m_Data = new png_bytep[m_ImageHeight]();
for (unsigned long int i = 0; i < m_ImageHeight; ++i) {
m_Data[i] = NULL;
}
try {
for (unsigned long int i = 0; i < m_ImageHeight; ++i) {
m_Data[i] = new png_byte[m_ImageWidth];
}
}
catch (std::bad_alloc e) {
for (unsigned long int i = 0; i < m_ImageHeight; ++i) {
if (m_Data[i]) {
delete m_Data[i];
m_Data[i] = NULL;
}
}
if (m_Data) {
delete m_Data;
m_Data = NULL;
}
throw e;
}
return PNGFileReader::SUCCESS;
}
A "raw" file that is intended to be used with a camera-image processing program like Raw Studio and Irfraview is not a raw-binary dump of the image-data with no header. Instead the "raw" moniker refers to the fact that the image has a minimal amount of image-processing applied in-camera. For instance, the image-data may still be a single-channel monochrome image from the camera's bayer-pattern CFA, or no white-balance, color-matrix, etc. has been applied, etc. Either way, the image-data is still formatted in a standard binary image file format complete with a header, data-packing method, etc. Examples include formats such as Adobe's DNG file format (which is based on TIFF), or proprietary formats from camera manufacturer's themselves such as Canon's CR2, Nikon's NEF, etc.
So if you want these raw-file processing programs to read your "raw" file image data, you'll have to read-up on the binary data specifications the raw-file formats they support, and then re-format the original PNG image-data correctly.