We implement a C program to
- read PGM file from disk;
- convert PGM data structure to our format;
- binarize it (0-127 -> 0 and 128-255 -> 1);
- convert back to PGM format;
- write to disk.
Note: our image format is
typedef struct {
int width;
int height;
tPixelType pixelType;
tpPixel pPixel;
} tImage;
typedef enum {
GRAY8 = 1,
FLOAT1 = 2,
RGB = 3
} tPixelType;
This is a nice tutorial that explains how to read and write to disk the PGM binary format. This reference is also good if you want to use Libnetpbm.
We can use Netpbm to convert a PNG image to PGM image. Just run the following command.
$ pngtopnm cat.png | ppmtopgm > cat.pgm
To use Libnetpbm, we need to install a package libnetpbm10-dev
(version 10 is the newest at this moment). We can then use #include <pgm.h>
in the code. When compile, we need to link the library. Simply add -lnetpbm
.
...