libpng clib Multimedia

libpng PNG codec. Encode, Decode, alpha channel, compression, text metadata, color types.

ppm install libpng

Overview

libpng is the reference PNG implementation. PNG offers lossless compression with full alpha channel support. It is the preferred format for graphics, icons, screenshots, and any image requiring exact pixel fidelity. Supports 8-bit and 16-bit depth, multiple color types, and embedded text metadata (tEXt/iTXt/zTXt chunks).

CLib package

Ubuntu/Debian: sudo apt install libpng-dev

Decode & Encode

uses libpng;

{ decode PNG file to raw RGBA pixels }
var img := DecodeFile('/path/to/image.png');
WriteLn('Size: ', img.Width, 'x', img.Height, ' channels: ', img.Channels);

{ encode to PNG bytes — compression 0 (none) .. 9 (max) }
var data := Encode(img, 6);   { level 6 = good balance }
WriteLn('PNG size: ', Length(data), ' bytes');

{ save to file }
EncodeFile(img, '/tmp/output.png', 6);

Alpha Channel

uses libpng;

{ decode as RGBA (4 channels) }
var img := Decode(ReadFile('sprite.png'));

{ flatten alpha onto white background }
var flat := RemoveAlpha(img, 255, 255, 255);

{ add opaque alpha to an RGB image }
var rgba := AddAlpha(flat);

{ make a specific color fully transparent }
var masked := MakeColorTransparent(img, 0, 255, 0);   { green screen }

{ premultiply alpha for compositing }
var premul := PremultiplyAlpha(img);

Compositing

uses libpng;

{ create a white 800x600 canvas }
var canvas := NewImage(800, 600, 255, 255, 255, 255);

{ decode an overlay image }
var overlay := DecodeFile('watermark.png');

{ composite overlay at position (20, 20) }
var result := Composite(canvas, overlay, 20, 20);

EncodeFile(result, 'output.png', 6);

Color Types & Encode Options

uses libpng;

var img := DecodeFile('photo.png');

{ encode as explicit color type }
var rgb   := EncodeEx(img, pngRGB,  6);   { strip alpha, 3 ch }
var gray  := EncodeEx(img, pngGrayscale, 6);

{ encode with embedded text metadata }
var meta := 'Author=PascalAI' + #10 + 'Description=Test image' + #10;
var withText := EncodeWithText(img, 6, meta);

{ decode as grayscale }
var grayImg := DecodeGray(ReadFile('photo.png'));

Text Metadata Chunks

uses libpng;

var data := ReadFile('image.png');

{ read tEXt/iTXt metadata }
var chunks := ReadTextChunks(data);
WriteLn(chunks);   { Author=PascalAI\nComment=...\n }

{ add or replace a text chunk }
var updated := WriteTextChunk(data, 'Author', 'PascalAI Team');

{ strip all metadata (tEXt, tIME, zTXt, etc.) for smaller files }
var stripped := StripMetadata(data);

PNG Info & Pixel Access

uses libpng;

{ read metadata without decoding all pixels }
var info := ReadInfoFile('image.png');
WriteLn('Depth: ',     info.BitDepth);
WriteLn('ColorType: ', Ord(info.ColorType));
WriteLn('HasAlpha: ',  info.HasAlpha);
WriteLn('Interlaced: ',info.Interlaced);

{ pixel-level access }
var img := DecodeFile('icon.png');
var px  := GetPixel(img, 10, 10);   { returns [R, G, B, A] }
SetPixel(img, 10, 10, [255, 0, 0, 255]);   { set to opaque red }

Package Info

Version1.0.0
Typeclib
CategoryMultimedia
Authorgustavo
Native liblibpai_libpng.so

API

  • Decode(data)
  • DecodeFile(path)
  • DecodeRGB(data)
  • DecodeGray(data)
  • ReadInfo(data)
  • ReadInfoFile(path)
  • Encode(img,level)
  • EncodeFile(img,path,lvl)
  • EncodeEx(img,type,lvl)
  • EncodeWithText(img,lvl,meta)
  • AddAlpha(img)
  • RemoveAlpha(img,r,g,b)
  • MakeColorTransparent(img,r,g,b)
  • PremultiplyAlpha(img)
  • Composite(dst,src,x,y)
  • NewImage(w,h,r,g,b,a)
  • ReadTextChunks(data)
  • WriteTextChunk(data,k,v)
  • StripMetadata(data)
  • GetPixel(img,x,y)
  • SetPixel(img,x,y,vals)
  • PNGVersion()
  • IsPNG(data)
Compression levels

0 = no compression (fast, large) • 1 = fastest • 6 = default (good balance) • 9 = maximum compression (slow). PNG compression is always lossless.