Click here to Skip to main content
Click here to Skip to main content
Go to top

PixelMap Class and PNM Image Viewer

, 28 May 2007
Rate this:
Please Sign up or sign in to vote.
A managed C# class and viewer for working with and converting Portable Bit Map (.pbm), Portable Grey Map (.pgm) and Portable Pixel Map (.ppm) images
Screenshot - PixelMapViewer.jpg

Introduction

Although the class of Portable Any Map (PNM) format images -- which includes ASCII and Binary versions of Portable Bit Map (PBM), Portable Grey Map (PGM) and Portable Pixel Map (PPM) images -- represents the "lowest common denominator" of image formats, they are relatively uncommon in modern Windows environments. They also tend to be poorly supported on the free graphics programs for Windows, such as Paint and Paint.NET. Furthermore, I was not (in an admittedly short search) able to find a suitable C# class library for working with these formats programmatically.

Fortunately, the formats are quite simple, so it was relatively easy to build a PixelMap class to encapsulate these images and expose a couple of System.Drawing.Bitmap properties (BitMap and GreyMap) that can be used easily by other classes. Also, the simple Windows application that I created to test the PixelMap class rapidly evolved into a fairly competent viewer and converter that can save the current image as a BMP, JPG, TIFF, GIF, or PNG file.

Using the code

To use the PixelMap class you should:

  1. Copy the PixelMap.cs file into your own project.
  2. Change the namespace in the PixelMap.cs file to the name of your project namespace.
  3. Compile. The PixelMap class should now be available in your project.
The PixelMapViewer application demonstrates the usage of the PixelMap class. It also provides a handy viewing and converting application in its own right.

Points of interest

  • There are ASCII and Binary versions of each of the PBM, PGM, and PPM formats. The binary versions are much, much smaller and faster than the ASCII versions. They ought to be used unless there is a compelling reason to make the image file "human readable." Personally, I prefer to "read" my images by "looking at the pictures." This PixelMap class does not support the binary PBM format (Magic Number P4) at this time.
  • Since PBM, PGM, and PPM are UNIX-centric formats, their pixel order is BGR, rather than the RGB order that Windows programmers would tend to expect.
  • The "stride" of the image (normally calculated as stride = Image.Width * BytesPerPixel) should be a multiple of 4. If this is not the case, then in many programs the output images will become corrupted as they are processed. However, this is not a problem with the PixelMap class since it has been designed to work correctly with "off-size" images. However, it does have to use much less efficient (i.e. slow) algorithms relative to the normal direct-memory algorithms that it uses for well-sized images.

History

25 May, 2007 - Version 1.0.0.0 uploaded to CodeProject

License

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

Share

About the Author

Stephen Bogner
Engineer Defence R&D Canada
Canada Canada
Stephen Bogner is a Senior Research Engineer with Defence R&D Canada. As the Head Autonomous Applications Group, Autonomous Intelligent Systems Section, he only programs when it can't be avoided, and then only in C#.

Comments and Discussions

 
QuestionBinary pbm P4 Format Pinmemberjazper11-Dec-13 13:28 
QuestionSuggestion for Avoiding Stride Issues Pinmemberlewax0018-Sep-13 7:09 
GeneralMy vote of 5 Pinmembermanoj kumar choubey26-Feb-12 21:27 
GeneralThank you! Pinmemberkcarrera6-Feb-11 9:48 
Generalmemory leak patch Pinmemberleongurman6-Sep-10 20:12 
GeneralThanks for solving my problem Pinmemberjimmygyuma18-Mar-10 8:22 
GeneralIt doesn't work for some *.pgm images. Pinmemberfarid_colombia2-Oct-08 18:17 
GeneralRe: It doesn't work for some *.pgm images. PinmemberStephen Bogner21-Oct-08 4:38 
GeneralRe: It doesn't work for some *.pgm images. Pinmemberfarid_colombia26-Oct-08 6:46 
GeneralRe: It doesn't work for some *.pgm images. PinmemberMos Dan - Lucian12-Jan-10 8:30 
GeneralPLEASE HEPL ME !! Pinmemberxuanvuongspkt24-Sep-08 3:06 
GeneralThanks! Pinmemberdaothanhtuan25-Mar-08 10:29 
GeneralRe: Thanks! PinmemberStephen Bogner25-Mar-08 12:25 
GeneralRe: Thanks! Pinmemberdaothanhtuan3-Apr-08 9:18 
GeneralMuch appreciated PinmemberSteveAbbott29-May-07 7:08 
GeneralRe: Much appreciated PinmemberStephen Bogner1-Jun-07 7:48 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 28 May 2007
Article Copyright 2007 by Stephen Bogner
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid