Click here to Skip to main content
15,867,977 members
Articles / Mobile Apps
Article

Managed PNM Image, Reader and Writer

Rate me:
Please Sign up or sign in to vote.
4.80/5 (10 votes)
7 Jul 2008CPOL3 min read 83.4K   2.1K   17   13
A class to read and write PNM files from System.Drawing.Image class
Screenshot - sample.jpg

Introduction

This class supports reading the PNM image format into System.Drawing.Image and writing it back to PNM format. It makes it possible to inter convert different image file formats including PNM.

Background

PNM is a portable bitmap format used in legacy software. The use of PNM file format is more common in LINUX / UNIX Platform. PNM file format consists of three different file formats namely PBM, PGM and PPM. (Corrections suggested by Andrew Kirillov, thank you.)

  • PBM [Portable Bit Map - Binary]
  • PGM [Portable Grey Map - GreyScale]
  • PPM [Portable Pixel Map - Color]

Each of these file formats, except PBM can be written in ASCII or Binary Encoding. The encoding is determined at reading time against the Identifier each PNM file contains called "Magic Number". This is always the first token of the PNM file.

  • "P1" [PBM - ASCII Encoding]
  • "P2" [PGM - ASCII Encoding]
  • "P3" [PPM - ASCII Encoding]
  • "P4" [PBM - Binary Encoding] (not yet implemented)
  • "P5" [PGM - Binary Encoding]
  • "P6" [PPM - Binary Encoding]

The next two tokens in the PNM header define width and height of the image. The fourth token is the maximum value of the pixel which is present only in the case of PGM and PPM. PBM does not contain this token. Moreover, the header can contain comments beginning with '#' character.

Read more about these image formats from the links provided in the Reference section.

16-bit Extensions

The original definitions of PGM and PNM do not describe 16-bit pixel formats. It has, however been used in many practical situations where accuracy/depth of color information matters. This library does not currently support the 16-bit extensions, perhaps an avenue for you to contribute. Read more about it here.

Using the Code

The parameterless constructor of System.Drawing.Image is marked internal and System.Drawing.Bitmap is marked as a sealed class, therefore I could not inherit them into my class and hence could not follow true object oriented design.

The code is primarily a class library, a class named PNM contains two overloaded functions to provide its functionality. Since the functions are static, you do not have to create an object of the said class.

Here is a sample use of the class:

C#
//
// Read a PNM File into System.Drawing.Image
//
System.Drawing.Image im = ShaniSoft.Drawing.PNM.ReadPNM(FileName);

//
// Writing System.Drawing.Image into a PNM File
//
ShaniSoft.Drawing.PNM.WritePNM(im, FileName);

Both functions may throw IOException like a normal class would.

Points of Interest

PNM format is very plain in terms of complexity. It contains image subtype, width, height and max value of a pixel in ASCII format. The pixel data is then appended at the end in either binary format or ASCII format (nothing a BinaryReader/BinaryWriter couldn't handle).

History

  • Version 1, conversion from C to C# [PGM Only]
  • Version 2, complete PNM Support [PBM, PGM, PPM]
  • Corrections made to article regarding PNM format description

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Pakistan Pakistan
BCSE - Software Engineering (2000 - 2004)
Foundation University Institute of Management and Computer Sciences.
Pakistan.

MS - Computer Sciences (2004 - 2005)
Lahore Univeristy of Management Sciences
Pakistan.

Comments and Discussions

 
GeneralSpeed increasing Pin
savamura2-Nov-08 11:35
savamura2-Nov-08 11:35 
GeneralRe: Speed increasing Pin
Zeeshan Ejaz Bhatti4-Nov-08 4:54
Zeeshan Ejaz Bhatti4-Nov-08 4:54 
Generalpgm write in c++ Pin
william cplus18-Jul-08 5:07
william cplus18-Jul-08 5:07 
AnswerRe: pgm write in c++ Pin
Zeeshan Ejaz Bhatti18-Jul-08 8:18
Zeeshan Ejaz Bhatti18-Jul-08 8:18 
GeneralIncorrect interepretation of formats Pin
Andrew Kirillov7-Jul-08 7:21
Andrew Kirillov7-Jul-08 7:21 
GeneralRe: Incorrect interepretation of formats Pin
Zeeshan Ejaz Bhatti7-Jul-08 18:49
Zeeshan Ejaz Bhatti7-Jul-08 18:49 
Generalwell done Pin
Ahmad hassanat30-Jun-07 13:24
Ahmad hassanat30-Jun-07 13:24 
GeneralRe: well done Pin
cowlinator19-Jan-10 18:16
cowlinator19-Jan-10 18:16 
GeneralWhy not the whole PNM range? Pin
Willi Deutschmann22-Mar-07 8:43
Willi Deutschmann22-Mar-07 8:43 
GeneralRe: Why not the whole PNM range? Pin
Zeeshan Ejaz Bhatti24-Mar-07 4:42
Zeeshan Ejaz Bhatti24-Mar-07 4:42 
GeneralConversion Pin
PeaceTiger19-Mar-07 5:59
PeaceTiger19-Mar-07 5:59 
GeneralRe: Conversion Pin
Zeeshan Ejaz Bhatti19-Mar-07 19:01
Zeeshan Ejaz Bhatti19-Mar-07 19:01 
GeneralRe: Conversion Pin
PeaceTiger20-Mar-07 5:26
PeaceTiger20-Mar-07 5:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.