Is there a way to do the following better? Current I write a set of pixels to a file called "PngFile.bmp". Then I read that file back in as a PNG encoded buffer.. Then I save that buffer back to the harddisk and delete "PngFile.bmp"
/*std::fstream PngFile(TempPath, std::fstream::out | std::fstream::app | std::ofstream::binary);
if (!PngFile.is_open()) return false;
PngFile.write(reinterpret_cast<char*>(&bFileHeader), sizeof(BITMAPFILEHEADER));
PngFile.write(reinterpret_cast<char*>(&Info.bmiHeader), sizeof(BITMAPINFOHEADER));
PngFile.write(reinterpret_cast<char*>(&TEMP[0]), Size());
PngFile.close();
*/
//I do not want to write it to a physical file.. Maybe if I can write it to a memory location.. Maybe write it to a vector of unsigned chars.
//There has to be a better way to do the following?..
unsigned w = 0, h = 0;
std::vector<unsigned char> bmp(sizeof(BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + Size());
unsigned char* Ppos = &bmp[0];
memcpy(Ppos, &bFileHeader, sizeof(BITMAPFILEHEADER)); //copy the file header into the vector.
Ppos += sizeof(BITMAPFILEHEADER);
memcpy(Ppos, &Info.bmiHeader, sizeof(BITMAPINFOHEADER)); //copy the info header into the vector.
Ppos += sizeof(BITMAPINFOHEADER);
memcpy(Ppos, &TEMP[0], Size()); //copy/append the vector of pixels into the vector.
std::vector<unsigned char> image, png;
if (DecodeBMP(image, w, h, bmp) || lodepng::encode(png, image, w, h)) return false;
lodepng::save_file(png, FilePath);
remove(TempPath);
So my question is.. Is there a better way to do the above? Currently I dislike having to do memcpy on a vector tbh. But then again, I'm not sure of any other way to do it. I tried using an iterator but it errors out since I actually created the iterator and it points to nothing + sizeof(...). I cannot enter values instead for std::copy.. It requires a valid iterator.
Please help me?
I tried:
std::vector<unsigned char> BmpBuffer(sizeof(BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + Size());
BmpBuffer.insert(BmpBuffer.end(), &bFileHeader, sizeof(BITMAPFILEHEADER));
BmpBuffer.insert(BmpBuffer.end(), &Info.bmiHeader, sizeof(BITMAPINFOHEADER));
BmpBuffer.insert(BmpBuffer.end(), TEMP.begin(), TEMP.end());
but I'm not sure if that'll work. Inother words, should I even construct the vector with a size first? Or just insert?