Part of coursework of my Intro to Programming class at my university 🎓
The whole purpose of this, is to turn a pixmap(RGB) image (which is of netpbm format) to grayscale and a grayscale image to black and white.
- Open a terminal
- Compile the netpbm.c file using a compiler of your choice (I use gcc here)
$ gcc -o netpbm netpbm.c
- If no errors pop up 😂 run this command
$ ./netpbm < YOUR_INPUT_IMAGE.ppm > YOUR_OUTPUT_IMAGE.pgm
for example: $ ./netpbm < colorsasc.ppm > colorsasc.pgm
- Have a cookie 'cause you're done! 🍪
It reads the image given as input, character by character. Each file starts with a two-byte magic number (in ASCII) that identifies the type of file it is (PBM, PGM, and PPM) and its encoding (ASCII or binary). The magic number is a capital P followed by a single-digit number.
P# | Type, Encoding | Extension |
---|---|---|
P1 | Black & White, ASCII | .pbm |
P2 | Grayscale, ASCII | .pgm |
P3 | RBG, ASCII | .ppm |
P4 | Black & White, Binary | .pbm |
P5 | Grayscale, Binary | .pgm |
P6 | RBG, Binary | .ppm |
After the magic number, follows the width (in ASCII).
After the width, follows the height (in ASCII).
After the width, follows the max value (in ASCII) that a given character can have.
❗❗ CAUTION ❗❗ Between the values above there most likely will be white space... you know... tabs, spaces, enters and stuff like that... We ignore them. Just like that. They are not cool enough to deal with. 😎
To turn a Pixmap(RBG) image to grayscale, it reads the R, the G, and the B and then using this function it calculates how gray the pixel should be.
GRAYSCALE_PIXEL = floor(0.299R + 0.587G + 0.114*B)
To turn a Graymap(Grayscale) image to BitMap(Black & White), it reads a pixel and then using this function it calculates if it should be black or white.
BIT_PIXEL = floor((max+1)/2)
$ ./netpbm < colorsasc.ppm > colorsasc.pgm
$ ./netpbm < colorsasc.pgm > colorsasc.pbm
$ ./netpbm < SWbin.ppm > SWbin.pgm
$ ./netpbm < SWbin.pgm > SWbin.pbm