Skip to content

About

Library for decoding obscure graphics formats, such as Targa (.TGA), Sun raster (.RAS, .SUN), ZSoft (.PCX), Netpbm (.PPM, .PGM, .PBM, .PNM), Amiga (LBM, PIC), SGI, MacPaint, AOL .ART, PSP, and DICOM.

Topics

Resources

Stars

48 stars

Watchers

5 watching

Forks

Latest commit

 

History

182 Commits

Folders and files

Repository files navigation

imageformats

Build NuGet Downloads

.NET modules for decoding obscure graphics formats:

  • .PPM, .PGM, .PBM (Netpbm images)
  • .TGA (Targa)
  • .PCX (ZSoft Paintbrush)
  • .IFF (Amiga ILBM images)
  • .SGI, .RGB, .BW (SGI images)
  • .RAS (Sun raster images)
  • .MAC (MacPaint)
  • .CUT (Dr. Halo)
  • .XPM (X Window PixMap)
  • .DEEP (TVPaint IFF DEEP images)
  • .FITS, .FIT, .FTS, .FZ (astronomical images, including image cubes, multi-extension and tile-compressed files)
  • .DCM (DICOM medical images, including JPEG, JPEG-LS, JPEG 2000 and RLE compressed ones)
  • .JP2, .J2K, .J2C, .JPF (JPEG 2000)
  • .ART (AOL Johnson-Grace images)
  • .PSP, .PSPIMAGE (Paint Shop Pro images)
  • .AWD (Microsoft Fax documents)
  • .PSD, .PSB, .PDD (Adobe Photoshop and PhotoDeluxe images)
  • .FLI, .FLC (Autodesk Animator animations)
  • .FPX (FlashPix images)
  • .PPP (Serif PagePlus publications; reads the preview image stored in the file)
  • .TIF, .TIFF (TIFF images, including BigTIFF, multi-page, tiled, CCITT, LZW, Deflate and JPEG compressed ones)
  • .SER (Lucam Recorder videos, as used for planetary imaging, including raw Bayer ones)
  • .CIN, .CINE (Phantom high-speed camera videos, including raw color and packed 10-bit ones)
  • .DDS (DirectDraw Surface textures, including BC1 to BC7 compressed ones, cube maps, texture arrays and volume textures)
  • .LFP, .LFR (Lytro light field camera pictures, both raw ones and refocus stacks)
  • .MNG, .JNG (Multiple-image Network Graphics animations, including ones with JNG and Delta-PNG images, and JPEG Network Graphics images)
  • .CR2, .CR3, .CRW, .NEF, .NRW, .ARW, .SR2, .SRF, .PEF, .ORF, .RW2, .RWL, .RAF, .DNG, .SRW, .KDC, .MRW, .MOS, .X3F, and other camera raw images (reads the preview image embedded in the file)

Usage

Every decoder returns an ImageData object: a plain buffer of raw pixels, with no dependency on any particular imaging library. Hand the buffer to whichever library you prefer.

using DmitryBrant.ImageFormats;

// Detects the format automatically, and returns null if it isn't one we know.
ImageData image = ImageData.Load("picture.tga");

The pixels in image.Data are stored top-down, one row after another, four bytes per pixel in blue, green, red, alpha order. (Read as a 32-bit little-endian integer, that's the usual packed ARGB layout.) This is the same layout as GDI+ Format32bppArgb, WIC/Direct2D B8G8R8A8, Avalonia's Bgra8888, and ImageSharp's Bgra32, so it can go straight into any of them:

// Avalonia
var bitmap = new WriteableBitmap(new PixelSize(image.Width, image.Height),
    new Vector(96, 96), PixelFormat.Bgra8888, AlphaFormat.Unpremul);
using (var buffer = bitmap.Lock())
    for (int y = 0; y < image.Height; y++)
        Marshal.Copy(image.Data, y * image.Stride, buffer.Address + (y * buffer.RowBytes), image.Stride);

// ImageSharp
var bitmap = Image.LoadPixelData<Bgra32>(image.Data, image.Width, image.Height);

// System.Drawing
var bitmap = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);
var bits = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, bitmap.PixelFormat);
for (int y = 0; y < image.Height; y++)
    Marshal.Copy(image.Data, y * image.Stride, bits.Scan0 + (y * bits.Stride), image.Stride);
bitmap.UnlockBits(bits);

DICOM, FITS, FLI/FLC, TIFF, SER, Cine, DDS, Lytro and MNG files can contain more than one frame (for example, an ultrasound cine loop, the planes of an astronomical image cube, an animation, the pages of a fax, a planetary or high-speed video, the faces of a cube map, or the focal planes of a refocusable picture). ImageData.Load returns the first frame (or, for a refocusable Lytro picture, the one focused at its default depth); to get at the others, use DicomReader, FitsReader, FliReader, TiffReader, SerReader, CineReader, DdsReader, LfpReader or MngReader directly:

int count = DicomReader.GetFrameCount("series.dcm");
ImageData frame = DicomReader.Load("series.dcm", count - 1);

// Or decode them all, one at a time, parsing the file only once:
foreach (ImageData frame in DicomReader.LoadFrames("series.dcm"))
    ...

Photoshop files normally contain a flattened copy of the picture, which is what ImageData.Load returns. When that copy is missing or only a placeholder (as in PhotoDeluxe files, or Photoshop files saved without "Maximize Compatibility"), the layers are composited instead. To make that choice yourself, use PsdReader directly:

ImageData image = PsdReader.Load("picture.psd", PsdImageSource.Layers);

For camera raw files, ImageData.Load returns the largest of the JPEG previews that the camera embedded in the file (often full size), rotated the way the picture was taken. A raw file that's also a TIFF file, but has no JPEG preview, is read as a TIFF file instead, which usually gives a small uncompressed thumbnail. RawReader.Load reads the preview of any raw file directly.

Occasionally a source format stores its pixel data as an embedded image in some other well-known encoding that the library can't decode itself (for example, a DICOM file that wraps an 8-bit JPEG using arithmetic coding). Those bytes are passed through untouched: image.IsEncoded is true, image.Data is null, and the encoded bytes are in image.EncodedData (with image.EncodedFormat naming the encoding) for you to decode yourself.

The ImageViewer project in this repository is a small Avalonia app that does all of the above; see its Interop.cs for a complete example.

Copyright 2013+ Dmitry Brant

https://dmitrybrant.com

License: MIT

About

Library for decoding obscure graphics formats, such as Targa (.TGA), Sun raster (.RAS, .SUN), ZSoft (.PCX), Netpbm (.PPM, .PGM, .PBM, .PNM), Amiga (LBM, PIC), SGI, MacPaint, AOL .ART, PSP, and DICOM.

Topics

Resources

Stars

48 stars

Watchers

5 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages