In order to ease the data access, the image.txt file provided at the beginning of the session and holding the images, was an ASCII file. In practice, this kind of file format is very seldomly used because it isn’t very efficient, both in terms of memory/disk space and in terms of access/reading speed. Instead, one usually resorts to binary format(s) which consists in writing out data values byte by byte without any particular formatting.
Note
In their simplistic incarnations, binary formats aren’t portable, that is: e.g. you can not write a binary file on a MacOS machine and expect to read it correctly on a PC machine. There are of course ways to write portable binary formats.
For the file accesses, one will either use the STL iostream or the QFile class from Qt. More informations about Qt can be found here.
We’ll save:
m_nX as a short (2 bytes)
m_nY as a short (2 bytes)
the m_nX * m_nY values as int (4*m_nX*m_nY bytes)
Writing. Add the item Save image (in WFrame::InitMenus()) in the File menu. Implement the associated method Image::SaveImage(). The file extension shall be .bin. To handle the files’ extensions, one could leverage the QFileInfo class and its suffix() method.
One could either save the image value-by-value or percolate through the QVector class whose method QVector::data() returns the address of the beginning of the vector and thus allows to save line-by-line (it is more efficient because there are fewer seeks so fewer disk accesses.) The conversions std::vector <-> QVector can be done by means of QVector::toStdVector() and QVector::fromStdVector().
Reading back. Modify the WFrame::ReadImage() so as to be able to seamlessly load images from ASCII files (.dat) and binary image files (.bin). To achieve such a goal, one will have to pass as a third argument to QFileDialog::getOpenFileName(...) the string "Images (*.dat *.bin)". The files’ extensions will be discovered thanks to the QFileInfo class and its QFileInfo::suffix() method. Implement the corresponding Image::ReadBinaryFile().
The program will be then tested with the bulbe-olfactif-hexana.bin and bulbe-olfactif-pentylacetate.bin files which contain images from the rat’s olfactory bulb acquired by intrinsic optical imaging while under olfactory stimulii.