Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.
hichaeretaqua edited this page Mar 27, 2015 · 9 revisions

Getting Started

Read a PortableMap

In order to read a PortableMap you first have to know which file format is used. Normal you can use the file extension to distingush between the three formats.

  • *.pbm - PortbaleBitmap (Magic Number P1 or P4)
  • *.pgm - PortbaleGraymap (Magic Number P2 or P5)
  • *.ppm - PortbalePixmap (Magic Number P3 or P6)

P1, P2 and P3 are pure ASCII files that you can easily read yourself. The other one only contains a ASCII header. Unfortunately you cannot rely on on file extension, because for example Photoshop does not care about the correct file extension. So you may have to look at the magic number of the file.

There is a reader Factory that creates all readers needed:

using PortableGrayMap;
using PortableGrayMap.Reader;

var p1Reader = ReaderFactory.AsciiPortableBitmapReader;
var pbmFromFile = p1Reader.ReadFromFile("pathToFile.pbm");

You can also read from streams:

using PortableGrayMap;
using PortableGrayMap.Reader;

var p1Reader = ReaderFactory.AsciiPortableBitmapReader;
IPortbaleBitmap pbmFromStream;
using(var fileStream = new FileStream("pathToFile.pbm", FileMode.Open){
   pbmFromStream = p1Reader.ReadFromStream(fileStream);
}

The ReadFromStream methods in the different Reader interfaces are more flexible. On the one hand they allow to read from every stream, on the other hand you can load multiple images from one file. The PortbaleMap format allows to concatenate multiple images into one big file. This possible, but not recommended. Most of the programs that can read PortableMaps only read the first image (as ReadFromFile() does).

using PortableGrayMap;
using PortableGrayMap.Reader;

var p1Reader = ReaderFactory.AsciiPortableBitmapReader;
IList<IPortbaleBitmap> images = new List<IPortbaleBitmap>();
using(var fileStream = new FileStream("pathToFile.pbm", FileMode.Open){
   if(fileStream.Position < fileStream.Length){
       images.Add(p1Reader.ReadFromStream(fileStream))
   }
}

Write a PortableMap

Writing a bitmap is handled by one of the different writes provided by WriterFactory. Here you can choose a writer based on your image format.

  • IPortableBitmap => IPortableBitmapWriter
  • IPortableGraymap => IPortableGraymapWriter
  • IPortablePixmap => IPortablePixmap

With to following code you read a P1 image and save it as P4 image:

using PortableGrayMap;
using PortableGrayMap.Writer;
using PortableGrayMap.Reader;

var bitmap = ReaderFactory.AsciiPortableBitmapReader.ReadFromFile("filePath");
WriterFactory.BinaryPortableBitmapWriter.WriteToFile("newFilePath", bitmap);

Convert Image to more common formats

In order to display PortableMaps in WinForms or WPF, they had to be converter into a System.Drawing.Bitmap or System.Windows.Media.Imaging.ImageSource. This is done by converters. Convert to PortbaleBitmap to Bitmap:

using PortableGrayMap;
using PortableGrayMap.Conveter;
using PortableGrayMap.Reader;

var pbmBitmap = ReaderFactory.AsciiPortableBitmapReader.ReadFromFile("filePath");
var winformsBitmap = ConverterFactory.ToBitmapConverter.ConvertFrom(pbmBitmap);

Convert PortbaleGraymap To BitmapSource:

using PortableGrayMap;
using PortableGrayMap.Conveter;
using PortableGrayMap.Reader;

var graymap = ReaderFactory.AsciiPortableGraymapReader.ReadFromFile("filePath");
var wpfBitmap = ConverterFactory.ToBitmapSourceConverter.ConvertFrom(graymap);

#Functions & ToDo Functions that already exists:

  • Read P1, P2, P3, P4, P5, P6 images.
  • Write P1, P2, P3, P4, P5, P6 images.
  • Convert P1, P2, P3, P4, P5, P6 to Bitmap or BitmapSource.

Planned features:

  • Convert IPortbaleBitmap, IPortableGraymap and IPortablePixmap to Bitmap and BitmapSource and back.
  • Convert automatically between IPortbaleBitmap, IPortableGraymap and IPortablePixmap to/from Portable Network Graphics

About

All Factories are using the Lazy<> classed in order to create and maintain instances. So you can call the Factories as often as you want without creating unnecessary instances.

Clone this wiki locally