THotPDF.AddImageWithSMask Method

 

THotPDF.AddImageWithSMask

THotPDF

 

Top  Previous  Next

Adds a color image XObject together with a PDF 1.4 soft-mask image XObject (ISO 32000-1 8.9.5.4) so the resulting picture carries full per-pixel transparency instead of the binary on/off limit of the legacy /ImageMask path.

 

Delphi syntax:

function AddImageWithSMask(Width, Height: Integer; const RGB: array of Byte; const Alpha: array of Byte): Integer;

function AddImageWithTransparency(Width, Height: Integer; const RGB: array of Byte; const Alpha: array of Byte): Integer;

function AddImageWithSMask32(Width, Height: Integer; const Pixels: array of Byte): Integer;

function AddImageWithTransparency32(Width, Height: Integer; const Pixels: array of Byte): Integer;

 

C++ syntax:

int AddImageWithSMask(int Width, int Height, const TBytes& RGB, const TBytes& Alpha);

int AddImageWithTransparency(int Width, int Height, const TBytes& RGB, const TBytes& Alpha);

int AddImageWithSMask32(int Width, int Height, const TBytes& Pixels);

int AddImageWithTransparency32(int Width, int Height, const TBytes& Pixels);

 

Description

SMask images carry an independent 8-bit DeviceGray channel whose luminosity drives the alpha of the color image at paint time (0 = fully transparent, 255 = fully opaque). HotPDF emits both planes as Flate-compressed image XObjects, wires the color image dictionary's /SMask entry at the soft-mask XObject, and returns an XImages index that THPDFPage.ShowImage accepts directly - all downstream placement code works unchanged.

AddImageWithTransparency and AddImageWithTransparency32 are naming aliases for the same PDF 1.4 soft-mask workflow. They are provided for call sites that think in terms of transparent images rather than PDF dictionary terminology

 

AddImageWithSMask(Width, Height, RGB, Alpha)

- Width / Height: pixel dimensions, identical for both planes.

- RGB: Width * Height * 3 bytes, row-major top-down, R G B interleaved per pixel (no row padding).

- Alpha: Width * Height bytes, row-major top-down, 8-bit luminosity per pixel.

 

AddImageWithSMask32(Width, Height, Pixels)

Convenience overload for packed 32-bit RGBA input. Pixels must contain Width * Height * 4 bytes in R, G, B, A order. HotPDF splits the buffer into the RGB color plane and the 8-bit alpha plane before delegating to AddImageWithSMask

 

Return value: an index into XImages identical to the one AddImage returns; pass it to THPDFPage.ShowImage to place the picture on a page. Returns -1 when StrictVersionLock is on and the active Version is below PDF 1.4 (otherwise the version is auto-bumped to 1.4).

 

Code Example

// Raw-bytes path: build a 64x64 magenta-on-white plane + horizontal
// alpha gradient and place the result on the page
var
  RGB, Alpha: TBytes;
  W, H, X, Y, Idx, ImIdx: Integer;
begin
  W := 64;
  H := 64;
  SetLength(RGB,   W * H * 3);
  SetLength(Alpha, W * H);
  for Y := 0 to H - 1 do
    for X := 0 to W - 1 do
    begin
      Idx := Y * W + X;
      RGB[Idx * 3 + 0] := 255;    // R
      RGB[Idx * 3 + 1] := 0;      // G
      RGB[Idx * 3 + 2] := 255;    // B
      Alpha[Idx]       := Byte((X * 255) div (W - 1));
    end;

  HPDF.Version    := pdf14;       // SMask requires PDF 1.4
  HPDF.Compression := cmFlateDecode;
  HPDF.BeginDoc;
  ImIdx := HPDF.AddImageWithSMask(W, H, RGB, Alpha);
  HPDF.CurrentPage.ShowImage(ImIdx, 80, 80, 256, 256, 0);
  HPDF.EndDoc;
end;

 

See Also

AddImage, THPDFPage.ShowImage, Version, PDF Filter Support