0

Suppose we want to blur an image, and the first step is to read the image, what is the purpose of using file.read(reinterpret_cast(&variableName), sizeof(variablename)) ?

    struct BitmapInfo {
  short signature;
  int fileSize;
  int reserved;
  int offsetToBits;
  int headerSize;
  int width;
  int height;
} __attribute__((packed));



int main() {


  fstream file("BLUE SHARD.bmp");

  auto bmpInfo = BitmapInfo();
  file.read(reinterpret_cast<char*>(&bmpInfo), sizeof(bmpInfo));

  auto additionalData  = vector<char>(bmpInfo.offsetToBits - sizeof(bmpInfo));
  file.read(reinterpret_cast<char*>(&additionalData[0]), bmpInfo.offsetToBits - sizeof(bmpInfo));

  auto pixels = vector<vector<rgba>>(bmpInfo.height);
  for (int i=0; i<bmpInfo.height; i=i+1) {
    pixels[i] = std::vector<rgba>(bmpInfo.width);
    file.read(reinterpret_cast<char*>(&pixels[i][0]), bmpInfo.width * sizeof(rgba));
EthanPy
  • 27
  • 3
  • http://www.cplusplus.com/reference/istream/istream/read/ You call the read function to read the values? What do you don't understand? – RoQuOTriX Mar 20 '20 at 07:09
  • i dont get the purpose of this line, if this wasnt there, how would it effect the image processing? – EthanPy Mar 20 '20 at 07:10
  • The line is reading the image (or part of it) from the file `file` into the variable `variablename`. What exactly do you need explained? – walnut Mar 20 '20 at 07:11
  • You don't show where the image processing is happening, but the data after this line of file is stored in variableName. You can#t do the image processing on `file` directly. Myber if you show all the code, we can explain in detail what is happpening – RoQuOTriX Mar 20 '20 at 07:12
  • what is reinterpret_cast and sizeof, – EthanPy Mar 20 '20 at 07:12
  • 7
    @studentferret read about [reinterpret_cast](https://en.cppreference.com/w/cpp/language/reinterpret_cast) and [sizeof](https://en.cppreference.com/w/cpp/language/sizeof). And then get yourself a [good C++ book](https://stackoverflow.com/questions/388242/). – Remy Lebeau Mar 20 '20 at 07:16
  • Does this answer your question? [Using reinterpret\_cast to read file into structure](https://stackoverflow.com/questions/49565933/using-reinterpret-cast-to-read-file-into-structure) – Daniel Langr Mar 20 '20 at 07:56

1 Answers1

1

What does file.read(reinterpret_cast<char*>(&variableName), sizeof(variablename)) do in c++ for reading images?

It reads sizeof(variablename) bytes from a file handled by file stream to the storage of variableName object (memory, where it resides).

In simpler words, it loads some value from a file into a variable.

Daniel Langr
  • 22,196
  • 3
  • 50
  • 93