SharpAstro.Pnm 3.32.1561

This package has a SemVer 2.0.0 package version: 3.32.1561+25f80f8a848ba90b62c5922e98c826c7181bce96.
dotnet add package SharpAstro.Pnm --version 3.32.1561
                    
NuGet\Install-Package SharpAstro.Pnm -Version 3.32.1561
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SharpAstro.Pnm" Version="3.32.1561" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SharpAstro.Pnm" Version="3.32.1561" />
                    
Directory.Packages.props
<PackageReference Include="SharpAstro.Pnm" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SharpAstro.Pnm --version 3.32.1561
                    
#r "nuget: SharpAstro.Pnm, 3.32.1561"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SharpAstro.Pnm@3.32.1561
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SharpAstro.Pnm&version=3.32.1561
                    
Install as a Cake Addin
#tool nuget:?package=SharpAstro.Pnm&version=3.32.1561
                    
Install as a Cake Tool

SharpAstro.Pnm

Pure-managed Netpbm reading and writing for .NET 10, written from the Netpbm format documentation (pbm, pgm, ppm, pam). The facade (SharpAstro.Codecs) sniffs P1..P7 and decodes through it. Writing is PnmWriter.

Reads every member of the family:

  • P1/P4 PBM (bitmap, 1 = black), P2/P5 PGM, P3/P6 PPM, in plain (ASCII) and raw form; plain P1 may pack its digits with no separators.
  • P7 PAM with TUPLTYPE BLACKANDWHITE, GRAYSCALE, RGB and their _ALPHA forms (or by DEPTH when absent). CMYK and other tuple types are refused.
  • maxval 1..65535; above 255 raw samples are two bytes, big-endian.
  • # comments wherever the format allows them.

A sample above maxval, a truncated raster or an out-of-range header throws InvalidDataException. A declared size over 2^28 samples (a caller-chosen ceiling) is refused before anything is allocated, and a raw raster must fit in the input.

using SharpAstro.Pnm;

PnmImage image = PnmReader.Read(File.ReadAllBytes("image.pgm"));
Console.WriteLine($"{image.Width} x {image.Height}, {image.Channels} channel(s), maxval {image.MaxValue}");
ushort sample = image.Samples[0]; // native value, 0..MaxValue

Read / write matrix

Format Magic Read Write
PBM bitmap P1 plain, P4 raw yes no
PGM grey P2 plain, P5 raw yes P5
PPM RGB P3 plain, P6 raw yes P6
PAM BLACKANDWHITE[_ALPHA] P7 yes no
PAM GRAYSCALE, RGB P7 yes yes
PAM GRAYSCALE_ALPHA, RGB_ALPHA P7 yes yes

maxval 1..65535 both ways; above 255 a raw sample is two bytes, big-endian.

Writing

using SharpAstro.Pnm;

byte[] file = PnmWriter.Encode(rgb8, width, height, PnmTupleType.Rgb);               // P6, maxval 255
byte[] wide = PnmWriter.Encode(gray16, width, height, PnmTupleType.Grayscale);       // P5, maxval 65535
byte[] pam  = PnmWriter.Encode(rgba8, width, height, PnmTupleType.RgbAlpha);         // P7 RGB_ALPHA
PnmWriter.Write(stream, gray16, width, height, PnmTupleType.Grayscale, maxValue: 1023, pam: true);

Grey and RGB go out as P5 / P6 unless pam: true asks for P7; anything with alpha is always P7, since the PGM/PPM headers cannot carry it. Output is the raw form with Netpbm's own header layout and no comment. A short buffer, a tuple type that cannot be written (BlackAndWhite*), a maxval outside 1..65535 or a sample above it throws ArgumentException.

The writer never scales. Samples are the file's native values: with maxValue: 1023 they are 0..1023 and are written as they are. Scaling is a decision for the caller, and the reader's facade rule is the one to mirror (below): a maxval below the type's full range is stretched to fill it, round(v * full / maxval), which is what ImageMagick does too. Only maxval 255 and 65535 are written without any such question, and those are the two the tests compare against Magick.NET pixel for pixel; every other maxval is verified by our own reader round trip.

ImageMagick agrees sample for sample, except where it departs from the Netpbm documents: a binary raster with maxval 128..254 or 32768..65534 is read as full-scale samples, without dividing by maxval, and a 1-bit grey+alpha PAM comes back with its alpha inverted. This reader follows the documents there.

PnmReader returns the file's native samples. Through the facade (PnmImageDecoder), they are scaled to fill UInt8 (maxval ⇐ 255) or UInt16, with the rounding ImageMagick uses, so a maxval 15 file decodes to the pixels ImageMagick gives.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SharpAstro.Pnm:

Package Downloads
SharpAstro.Codecs

Image-codec facade over the SharpAstro codec family: sniff a byte stream's format by its magic-byte signature and dispatch to the matching decoder (PNG, JPEG, TIFF, JPEG XR, OpenEXR, JPEG XL, JBIG2, JPEG 2000, WebP, GIF, Radiance RGBE, Netpbm PBM/PGM/PPM/PAM, BMP, and TGA by its TGA 2.0 footer, since a TGA has no leading magic number), returning a codec-neutral IDecodedImage (fidelity tier: native bit depth + ColorEncoding colour signalling, with a ToFloats() RGBA float32 view) or decoding zero-copy into a caller-provided 8-bit RGBA buffer (display tier, integer-sample formats). AOT-compatible.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.32.1561 33 11/3/2026
3.32.1541 26 11/3/2026
3.31.1511 24 11/3/2026
3.30.1461 29 11/3/2026
3.29.1431 30 11/3/2026
3.29.1401 29 11/3/2026