Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#
Article

PixelMap Class and PNM Image Viewer

Rate me:
Please Sign up or sign in to vote.
4.52/5 (14 votes)
28 May 2007CPOL2 min read 76.1K   3.8K   15   16
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)


Written By
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 Pin
jazper11-Dec-13 13:28
jazper11-Dec-13 13:28 
QuestionSuggestion for Avoiding Stride Issues Pin
lewax0018-Sep-13 7:09
lewax0018-Sep-13 7:09 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 21:27
professionalManoj Kumar Choubey26-Feb-12 21:27 
GeneralThank you! Pin
kcarrera6-Feb-11 9:48
kcarrera6-Feb-11 9:48 
Generalmemory leak patch Pin
leongurman6-Sep-10 20:12
leongurman6-Sep-10 20:12 
public class PixelMap
{
private IntPtr pImageData;
...

private Bitmap CreateBitMap()
{
pImageData = Marshal.AllocHGlobal(this.imageData.Length);
Marshal.Copy(this.imageData, 0, pImageData, this.imageData.Length);
Bitmap bitmap = new Bitmap(this.header.Width, this.header.Height, this.stride, this.pixelFormat, pImageData);
return bitmap;
}

private Bitmap CreateGreyMap()
{
...

pImageData = Marshal.AllocHGlobal(greyData.Length);
Marshal.Copy(greyData, 0, pImageData, greyData.Length);
Bitmap bitmap = new Bitmap(this.header.Width, this.header.Height, stride, PixelFormat.Format24bppRgb, pImageData);
bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
return bitmap;
}

public void Dispose()
{
imageData = null;
bitmap.Dispose();
Marshal.FreeHGlobal(pImageData);
}
}
GeneralThanks for solving my problem Pin
jimmygyuma18-Mar-10 8:22
jimmygyuma18-Mar-10 8:22 
GeneralIt doesn't work for some *.pgm images. Pin
farid_colombia2-Oct-08 18:17
farid_colombia2-Oct-08 18:17 
GeneralRe: It doesn't work for some *.pgm images. Pin
Stephen Bogner21-Oct-08 4:38
Stephen Bogner21-Oct-08 4:38 
GeneralRe: It doesn't work for some *.pgm images. Pin
farid_colombia26-Oct-08 6:46
farid_colombia26-Oct-08 6:46 
GeneralRe: It doesn't work for some *.pgm images. Pin
Dan Mos12-Jan-10 8:30
Dan Mos12-Jan-10 8:30 
GeneralPLEASE HEPL ME !! Pin
xuanvuongspkt24-Sep-08 3:06
xuanvuongspkt24-Sep-08 3:06 
GeneralThanks! Pin
daothanhtuan25-Mar-08 10:29
daothanhtuan25-Mar-08 10:29 
GeneralRe: Thanks! Pin
Stephen Bogner25-Mar-08 12:25
Stephen Bogner25-Mar-08 12:25 
GeneralRe: Thanks! Pin
daothanhtuan3-Apr-08 9:18
daothanhtuan3-Apr-08 9:18 
GeneralMuch appreciated Pin
SteveAbbott29-May-07 7:08
SteveAbbott29-May-07 7:08 
GeneralRe: Much appreciated Pin
Stephen Bogner1-Jun-07 7:48
Stephen Bogner1-Jun-07 7:48 

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.