Question: C++ program running visual studio. Part 3: Reading and writing binary PGM images Abinary PGM is very similar to the text-based PGMs we've been working
C++ program running visual studio.


Part 3: Reading and writing binary PGM images Abinary PGM is very similar to the text-based PGMs we've been working with, except the pixel values are stored as binary data in 8-bit bytes instead of of as ascii characters. The header is the same as for text based, except the image type is P5 instead of P2. Specifically 5 white space- ncols white space nrows w hite space maxval enter (note: the der is in regular ascii characters followed by ncols nrows binary pixel values. Note that there are an arbitrary er of white-space (space tab, newline, or return characters between the P5, ncols. nrows and maxval, but there is exactly I white space after the maxval and before the binary pixel data begins. This ix of ascii ation in the header and binary data is actually not a problem. You can open the file in binary mode, read the header with the usual and then read the pixel data w hifs read ere's what u need to do to read a binary image 1. Open the file in binary mode 2. Read Image Type, ncols, nrows, maxwal with something like Typ 3. Read 1 byte and throw it away (with something like ifs.read(&junk, l): This is the single white-space char following the maxva 4. Allocate an array of unsigued chars to use as a teruuporary builer for the pixel data 5. Read the pixels into the unsigned-char array using is read Part 3: Reading and writing binary PGM images Abinary PGM is very similar to the text-based PGMs we've been working with, except the pixel values are stored as binary data in 8-bit bytes instead of of as ascii characters. The header is the same as for text based, except the image type is P5 instead of P2. Specifically 5 white space- ncols white space nrows w hite space maxval enter (note: the der is in regular ascii characters followed by ncols nrows binary pixel values. Note that there are an arbitrary er of white-space (space tab, newline, or return characters between the P5, ncols. nrows and maxval, but there is exactly I white space after the maxval and before the binary pixel data begins. This ix of ascii ation in the header and binary data is actually not a problem. You can open the file in binary mode, read the header with the usual and then read the pixel data w hifs read ere's what u need to do to read a binary image 1. Open the file in binary mode 2. Read Image Type, ncols, nrows, maxwal with something like Typ 3. Read 1 byte and throw it away (with something like ifs.read(&junk, l): This is the single white-space char following the maxva 4. Allocate an array of unsigued chars to use as a teruuporary builer for the pixel data 5. Read the pixels into the unsigned-char array using is read
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
