THotPDF.RegisterSampledFunction Method

 

THotPDF.RegisterSampledFunction

THotPDF

 

傳回頂部

宣告一個 PDF 1.2+ 的 Function Type 0(Sampled)間接串流物件(ISO 32000-1 7.10.2)。取樣函式把任意的輸入到輸出對應表示成 M 維輸出取樣的規則 N 維網格,轉譯器在網格點之間插值,以算出中間的輸入值。當 ICC profile 機制太笨重、而手調的查表更合用時就用它:/TransferFunction(ExtGState /TR)的色調曲線、/Separation 或 /DeviceN 的取樣 tint 轉換(替代線性 Type 2 / 算術 Type 4 路徑)、半色調臨界曲線,或任何其他接受 Function 字典的 PDF 結構

 

Delphi 語法:

function RegisterSampledFunction(const Domain: array of Single; const Range: array of Single; const Size: array of Integer; BitsPerSample: Integer; const Samples: array of byte; Order: Integer = 1): THPDFStreamObject;

 

C++ 語法:

THPDFStreamObject* RegisterSampledFunction(const float* Domain, int DomainCount, const float* Range, int RangeCount, const int* Size, int SizeCount, int BitsPerSample, const unsigned char* Samples, int SamplesCount, int Order = 1);

 

說明

此函式會以間接串流輸出,帶上 /FunctionType 0、傳入的 /Domain、/Range、/Size、/BitsPerSample 與 /Order 項目,再加上原始按位元打包的取樣串流。/Encode 與 /Decode 一律省略,讓規格預設值生效:/Encode 把每個 Domain 端點對應到網格範圍 [0..Size[i]-1],/Decode 則與 /Range 相同(取樣先解包成整數,再線性映射回 Range 端點)。多輸入函式的取樣位元組順序依 PDF 1.7 的規定「輸入座標沿第一個輸入維度增加最快」,也就是網格點 (i_0, i_1, ..., i_{N-1}) 搭配 M 個輸出的位元組位移是 ((((i_{N-1} * Size[N-2] + i_{N-2}) * ...) * Size[0] + i_0) * M * BitsPerSample / 8。

Order - 選用,預設為 1。依 PDF 1.7 表 38 接受 1(線性插值,PDF 1.7 預設)或 3(三次樣條插值)。三次在取樣網格稀疏時過渡更平滑(例如 17 點的 ICC 色調曲線),但某些較舊的行動閱讀器遇到未實作的 Order 值會默默退回線性。自 HotPDF v2.54.0 起可用。

對 ASCII 輸入這類典型編寫路徑,單輸入 / M 輸出的函式取 BitsPerSample = 8 與 Size = [256],就得到一個 256 取樣的 LUT,其串流恰好是 256 * M 位元組,排成 (o0_0, o1_0, ..., oM-1_0, o0_1, ..., oM-1_255)。

 

Domain - 2N 個 Single 值,順序為 [in0Min in0Max in1Min in1Max ...]。函式會在插值之前把傳入值限制到此範圍。N 由 Length(Domain) div 2 決定。

Range - 2M 個 Single 值,形狀與 M 個輸出維度相同;輸出取樣在解碼後會限制到此範圍。

Size - N 個整數,給出每個輸入軸上的網格點數。Length(Size) 必須等於 N。網格點總數是所有 Size 項目的乘積。

BitsPerSample - 依 PDF 1.7 表 38,取 1、2、4、8、12、16、24 或 32 之一。8 是最省事的選擇(原始位元組串流);16 到 32 是位元組對齊的大端寬度;1 / 2 / 4 / 12 要求 MSB 優先的位元打包,最後一個位元組補零到位元組邊界。其他任何值都會擲回例外。

Samples - 原始按位元打包的取樣串流。長度必須等於 ceil(GridPoints * M * BitsPerSample / 8) 位元組,只在需要時包含末位元組填充。HotPDF 會檢查每種支援的打包深度,並把提供的位元組原樣複製進間接串流物件;位元打包的正確性由呼叫端負責。

已載入頁面上的求值支援 1 到 8 個輸入與 1 到 32 個輸出,顯式正向或反向的 /Encode、獨立的 /Decode、多線性 /Order 1、輸入超過四個時退回多線性的多維三次 /Order 3,以及一維自然三次樣條。驗證過的取樣位元組與插值中繼資料會保留在一個 8 項目、64 MiB 的 LRU 中,供重複的 tint、shading、軟遮罩與轉換函式呼叫使用。自 HotPDF v2.544.0 起可用。

 

傳回值: 間接的 THPDFStreamObject,呼叫端可把它附加到任何期望 Function 字典的位置(通常是透過 AddValue 把它間接參照傳給另一個字典,或交給 shading / separation 建構器)。物件由文件的 IndirectObjects 清單持有,在文件結束時釋放。傳回 nil 的條件是 StrictVersionLock 開啟且目前的 Version 低於 PDF 1.2(否則文件版本會自動升到 1.2)

 

程式碼範例

// 256-sample 1-input / 3-output (RGB) heatmap LUT. The LUT maps a
// scalar t in [0..1] to a blue -> green -> red gradient through the
// midpoint
uses
  HPDFObjs;

var
  Func: THPDFStreamObject;
  Domain: array[0..1] of Single;
  Range: array[0..5] of Single;
  Size: array[0..0] of Integer;
  Samples: array[0..767] of byte;   // 256 grid points * 3 outputs
  I: Integer;
  T: Single;
begin
  HPDF.BeginDoc;
  // 1 input dim: t in [0..1]
  Domain[0] := 0; Domain[1] := 1;
  // 3 output dims: each in [0..1]
  Range[0] := 0; Range[1] := 1;
  Range[2] := 0; Range[3] := 1;
  Range[4] := 0; Range[5] := 1;
  // 256 sample points along the single input axis
  Size[0] := 256;

  for I := 0 to 255 do
  begin
    T := I / 255.0;
    if T < 0.5 then
    begin
      Samples[I*3 + 0] := 0;
      Samples[I*3 + 1] := byte(Round(2.0 * T * 255.0));
      Samples[I*3 + 2] := byte(Round((1.0 - 2.0*T) * 255.0));
    end
    else
    begin
      Samples[I*3 + 0] := byte(Round(2.0 * (T - 0.5) * 255.0));
      Samples[I*3 + 1] := byte(Round((1.0 - 2.0*(T - 0.5)) * 255.0));
      Samples[I*3 + 2] := 0;
    end;
  end;
  Func := HPDF.RegisterSampledFunction(Domain, Range, Size, 8, Samples);
  // Func is now an indirect THPDFStreamObject the caller can attach
  // to any PDF dict slot that accepts a Function reference
  HPDF.EndDoc;
end;

 

請參閱

RegisterAxialGradient, RegisterDeviceN, RegisterSeparation, Version, PDF 篩選器支援