Skip to content

constantinosgeorgiou/netpbm-image-processor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Netpbm image processor

Part of coursework of my Intro to Programming class at my university 🎓

Getting Started

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.

How to use it

  1. Open a terminal
  2. Compile the netpbm.c file using a compiler of your choice (I use gcc here)
$ gcc -o netpbm netpbm.c
  1. If no errors pop up 😂 run this command
$ ./netpbm < YOUR_INPUT_IMAGE.ppm > YOUR_OUTPUT_IMAGE.pgm

for example: $ ./netpbm < colorsasc.ppm > colorsasc.pgm

  1. Have a cookie 'cause you're done! 🍪

How it works

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)

Examples with images

ASCII

colorsasc.ppm to colorsasc.pgm

$ ./netpbm < colorsasc.ppm > colorsasc.pgm

colorsasc.pgm to colorsasc.pbm

$ ./netpbm < colorsasc.pgm > colorsasc.pbm

Binary

SWbin.ppm to SWbin.pgm

$ ./netpbm < SWbin.ppm > SWbin.pgm

SWbin.pgm to SWbin.pbm

$ ./netpbm < SWbin.pgm > SWbin.pbm

About

Netpbm image format processor

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages