feat: hncs (1)
This commit is contained in:
1 parent
37025a76c7
commit
f82d87d733
65 files changed
+5057
-63
No files matched your search
@@ -11,9 +11,9 @@ Phocus 的 color/tone 不是一个统一的 3D LUT,而是把不同性质的问
|
||||
1. **相机表色层**:把 RGB 转为亮度与两条色度轴,以 `Cb/Y`、`Cr/Y` 查询相机和光源相关的
|
||||
二维色度 LUT;亮度 `Y` 原样保留。近中性色和低照度像素会降低 LUT 权重,避免噪声与白平衡
|
||||
误差被放大。
|
||||
2. **基础调性层**:使用 65,536 点、按 256×256 纹理铺放的一维 Film Curve,随后按 Film
|
||||
Curve 类型选择分段高光塑形,并用 Hasselblad power gamma 或逐通道 CIE L* 形式压缩内部
|
||||
线性动态范围。
|
||||
2. **基础调性层**:使用 65,536 点的一维 Film Curve,随后按 Film Curve 类型选择分段高光
|
||||
塑形;只有新版 correction 状态与 RAW capability 同时满足条件时,才追加 Hasselblad
|
||||
power gamma 或逐通道 CIE L* companding。
|
||||
3. **用户创作层**:RGB/对比度/亮度曲线使用独立的 65,536 点 LUT;选择性色彩则把色相映射到
|
||||
三张 65,536 点的一维表,分别控制 Hue、Saturation、Lightness。两者都显式做色相保护。
|
||||
4. **交付表色层**:最后才执行工作空间到目标空间的矩阵、白点适配和 Gamma/PQ 编码;预览路径
|
||||
@@ -28,19 +28,21 @@ flowchart TD
|
||||
C --> D["FilmCurve:65,536 点基础响应曲线"]
|
||||
D --> E{"特定 FilmCurve 类型?"}
|
||||
E -- 是 --> F["HighlightStrength:分段多项式 + HSL 色相保持"]
|
||||
E -- 否 --> G["Gamma:Hasselblad 2.2 或逐通道 L*"]
|
||||
E -- 否 --> G{"新版 correction 且 RAW 支持 Gamma?"}
|
||||
F --> G
|
||||
G --> H["Gradation:RGB / Contrast / Luma 曲线组合"]
|
||||
H --> I["MarkHue:仅选色交互预览,可选"]
|
||||
I --> J["SelectiveColor:Hue / Saturation / Lightness 表"]
|
||||
J --> K["Sharpness / Film Grain"]
|
||||
K --> L["Grayscale:可选"]
|
||||
L --> M["HDR / 工作空间到输出空间 / 传递函数"]
|
||||
G -- 是 --> H["Gamma:Hasselblad 2.2 或逐通道 L*"]
|
||||
G -- 否 --> I["Gradation:RGB / Contrast / Luma 曲线组合"]
|
||||
H --> I
|
||||
I --> J["MarkHue:仅选色交互预览,可选"]
|
||||
J --> K["SelectiveColor:Hue / Saturation / Lightness 表"]
|
||||
K --> L["Sharpness / Film Grain"]
|
||||
L --> M["Grayscale:可选"]
|
||||
M --> N["HDR / 工作空间到输出空间 / 传递函数"]
|
||||
```
|
||||
|
||||
这条顺序非常关键:**Film Curve 与高光塑形工作在 Gamma 之前,用户 Gradation 和
|
||||
Selective Color 工作在 Gamma 之后**。因此不能把全部 LUT 合并后任意换序;曲线所在的数值域、
|
||||
高光余量以及色相保护方式都不同。
|
||||
这条顺序非常关键:Gamma 是条件节点;它存在时 Film Curve/高光塑形在它之前,
|
||||
Gradation/Selective Color 在它之后;它不存在时 Gradation 直接接 Film Curve。不能把全部
|
||||
LUT 合并后任意换序。
|
||||
|
||||
## 证据范围与可信度
|
||||
|
||||
@@ -63,13 +65,13 @@ Selective Color 工作在 Gamma 之后**。因此不能把全部 LUT 合并后
|
||||
|
||||
| 路径 | 宿主入口 | 色彩校正 | Tone 组织 | 典型用途 |
|
||||
|---|---|---|---|---|
|
||||
| HNCS 主路径 | `CCameraImage::AddHNCSFilters` | `CColorCorrectAllFilter` + [`colorCorrectAll_0x3027dea.frag`](./colorCorrectAll_0x3027dea.frag) | 独立 `CFilmCurveFilter` →可选 Highlight→ Gamma→ Gradation | 完整 RAW 处理 |
|
||||
| HNCS 主路径 | `CCameraImage::AddHNCSFilters` | `CColorCorrectAllFilter` + [`colorCorrectAll_0x3027dea.frag`](./colorCorrectAll_0x3027dea.frag) | 独立 `CFilmCurveFilter` →可选 Highlight→可选 Gamma→ Gradation | 完整 RAW 处理 |
|
||||
| PostViewer RGB 路径 | `CCameraImage::AddPostViewerRGBFilters`,地址 `0x323f5a4` | 旧式 `CColorCorrectFilter` + [`color_correct_0x303b6ab.comp`](./color_correct_0x303b6ab.comp) | Highlight→ Gamma→ Gradation;Film/线性化的一部分由 ColorCorrection 资源承担 | 后置预览/已有 RGB 输入 |
|
||||
| 轻量 OpenGL 包装层 | `ColorCorrectAllFilter`、`FilmCurveFilter`、`HighlightStrengthFilter` 等无 `C` 前缀类 | [`ColorCorrectAll_0x301a79e.frag`](./ColorCorrectAll_0x301a79e.frag) | 对应的大写 shader | 另一套直接纹理渲染接口;包含未闭合功能 |
|
||||
|
||||
主路径反汇编中的关键插入点依次为:`CColorCorrectAllFilter` `0x3242524`、
|
||||
`CFilmCurveFilter` `0x32425e8`、条件性 `CHighlightStrengthFilter` `0x324285c`、
|
||||
`CGammaFilter` `0x3242680`、`CGradationFilter` `0x324271c`、
|
||||
条件性 `CGammaFilter` `0x3242680`、`CGradationFilter` `0x324271c`、
|
||||
`CSelectiveColorFilter` `0x3242934/0x32429e0`。两个 SelectiveColor 构造点属于互斥分支,
|
||||
不是连续应用两次。
|
||||
|
||||
@@ -95,6 +97,24 @@ neutral vector。导出符号 `CXMLLut::CalculateLUT(int, bool)`、`CColorCorrec
|
||||
|
||||
`Std` 与 `Repro` 的差别不仅是色彩风格。旧 compute shader 的 `uIsRepo` 会让 Reproduction
|
||||
模式绕过低亮度去饱和逻辑,因此它更接近“保持测量色度”,Standard 则更主动抑制暗部色噪。
|
||||
当前 HNCS 主 fragment 没有 `uIsRepo` uniform;主路径中的模式差异首先来自宿主上传的不同
|
||||
实测 LUT。不能把旧 compute 路径的分支直接移植成主 fragment 的额外开关。
|
||||
|
||||
XML 的 `mlt/mt/mf/mh` 解密后行和为 D50 XYZ 白点,语义是白平衡后的 camera RGB→XYZ(D50),
|
||||
不是 camera RGB→HNCS。使用这些矩阵驱动 HNCS 工作空间时必须再左乘
|
||||
`inverse(HNCS_RGB_TO_XYZ_D50)`。`vlt/vt/vf/vh` 也是加密数组:
|
||||
`CXMLLut::DecodeFrom` 对它们执行与矩阵相同的 `value × 0.5 + 1.0`,得到 RAW 相机通道
|
||||
neutral gain。`CRawColorParams::GetXYZ2RawRGBNeutralizedMatrix` 会把矩阵三列分别乘这三个
|
||||
gain 后再求逆。
|
||||
|
||||
Phocus 的 `ColorCorrectAll` 输入已经位于 neutralized camera domain;PhotonCamera 的 RCD/VGN
|
||||
对外输出则恢复为未白平衡 camera RGB。因此当前集成将 gain 折入输入矩阵:
|
||||
|
||||
```text
|
||||
M_raw_to_hncs = inverse(M_hncs_to_xyz_d50) · M_profile · diag(v_profile)
|
||||
```
|
||||
|
||||
这不是额外白平衡补偿,而是两个 demosaic 输出契约之间必须存在的等价变换。
|
||||
|
||||
### 1.2 主算法
|
||||
|
||||
@@ -130,7 +150,7 @@ neutral vector。导出符号 `CXMLLut::CalculateLUT(int, bool)`、`CColorCorrec
|
||||
`d/m` 越小,像素越接近中性轴,二维色度 LUT 的作用越弱。这样能避免灰阶被 LUT 的离散误差
|
||||
染色,也会抑制暗部白平衡/噪声偏差。
|
||||
|
||||
4. Standard 模式再应用亮度抑制抛物线:
|
||||
4. 宿主提供的低亮度去饱和参数再调制权重:
|
||||
|
||||
```text
|
||||
w_low(Y) = aY² + bY + c, Y < Y0
|
||||
@@ -138,6 +158,11 @@ neutral vector。导出符号 `CXMLLut::CalculateLUT(int, bool)`、`CColorCorrec
|
||||
w = max(0, min(w_gray, w_low))
|
||||
```
|
||||
|
||||
生产 `CColorCorrectAllFilter::UpdateParameters` 恢复出的完整支持相机默认值为
|
||||
`grayLow/grayHigh=0/0` 与 `(Y0,a,b,c)=(2,0,0,1)`。后者恒为 1;前者的零宽区间必须显式处理,
|
||||
不能靠 GPU 对除零/NaN 的偶然行为。通用/旧相机路径的灰阈值为 `0.005/0.07`,但低照度多项式
|
||||
仍依赖 source-specific 构造参数,不能把这组数值移植到所有相机。
|
||||
|
||||
5. LUT 输出乘回亮度,再由输出矩阵重建 RGB:
|
||||
|
||||
```text
|
||||
@@ -202,6 +227,11 @@ out[channel] = curve[channel][i]
|
||||
|
||||
无 `uGain` 的 [`filmCurve_0x30a3daf.frag`](./filmCurve_0x30a3daf.frag) 属于轻量包装层。
|
||||
|
||||
缺少 maker FilmCurve tag 时原始 reader 返回 `filmCurveType=6`;HNCS 工作空间选择
|
||||
`companding=2`。当前集成表由原始 `CGradationManager(6,2)` 直接导出,float table
|
||||
FNV-1a64 为 `a7fda12f9d03aa3f`。正常非 HDR 路径 `uGain=1`。HDR 动态 gain 仍来自
|
||||
source/image correction 状态,不能从 Colormap XML 推导。
|
||||
|
||||
### 2.2 Highlight Strength 是带色相保护的高光分段函数
|
||||
|
||||
生产版本是 [`highlightstrength_0x3035fd3.frag`](./highlightstrength_0x3035fd3.frag)。对每个
|
||||
@@ -237,7 +267,7 @@ T(x) = P2(x), X1 ≤ x < X2
|
||||
大写 [`HighlightStrength_0x3019721.frag`](./HighlightStrength_0x3019721.frag) 的分段 `mix`
|
||||
方向与生产版本不同,低/高区间行为并不等价;它只应视为另一包装层的实现证据。
|
||||
|
||||
### 2.3 Gamma 是内部动态范围 companding,不是输出 ICC 的替代品
|
||||
### 2.3 Gamma 是条件性内部 companding,不是输出 ICC 的替代品
|
||||
|
||||
[`gamma_hasselblad_0x30776ff.frag`](./gamma_hasselblad_0x30776ff.frag) 执行:
|
||||
|
||||
@@ -245,7 +275,19 @@ T(x) = P2(x), X1 ≤ x < X2
|
||||
out = (in · HDRMaxGain)^(1 / gamma) / HasselbladHdrRgbLimit
|
||||
```
|
||||
|
||||
`gamma` 的宿主缺省语义由 `CGammaFilter::HasselbladGamma` 与 shader 常量线索指向 2.2。
|
||||
宿主实际上传常量已经恢复:
|
||||
|
||||
```text
|
||||
gamma = 2.19921875
|
||||
HDRMaxGain = 49.261085510253906
|
||||
HasselbladHdrRgbLimit = 5.882924556732178
|
||||
```
|
||||
|
||||
`CCameraImage::AddHNCSFilters` 只有在 `CImageCorrection` stored version 至少为 4、其
|
||||
gamma-stage flag 开启且 `sRawDescription` 支持时才构造该 filter。默认
|
||||
`CImageCorrection::SetDefaultValues` 明确写入 version 2 并清除 flag,所以默认/第三方 RAW
|
||||
不会经过这个 shader。把该 shader 无条件接在 type 6 / companding 2 FilmCurve 后会形成重复
|
||||
companding,并显著抬高中间调。
|
||||
|
||||
[`gamma_lstar_0x307356f.frag`](./gamma_lstar_0x307356f.frag) 则逐通道应用归一化 L* 曲线:
|
||||
|
||||
@@ -258,9 +300,9 @@ out = f(in · HDRMaxGain) / HasselbladHdrRgbLimit
|
||||
典型 `ε = 216/24389`、`κ = 24389/27` 时两段在约 `0.08` 连续。这里对 R/G/B 分别编码,
|
||||
**不是**先求 XYZ/Y 再生成 CIELAB 的 `L*`;它仍是一条 RGB 传递曲线。
|
||||
|
||||
两个缩放参数让内部纹理可以用 `[0,1]` 附近的 FP16 保存超过 SDR white 的亮度。后续输出空间
|
||||
pass 仍需矩阵与最终 transfer function,不能因为已经经过 Hasselblad Gamma 就直接标记为
|
||||
sRGB、Display P3 或 PQ。
|
||||
两个缩放参数让内部纹理可以用 `[0,1]` 附近的 FP16 保存超过 SDR white 的亮度。该条件
|
||||
filter 实际执行时,后续输出空间 pass 仍需矩阵与最终 transfer function,不能把其输出直接
|
||||
标记为 sRGB、Display P3 或 PQ。
|
||||
|
||||
## 3. 用户 Gradation:三组高精度曲线的可组合设计
|
||||
|
||||
@@ -459,7 +501,7 @@ white/black point 操作。它不具备生产色彩管理的逻辑闭合性,
|
||||
1. `CameraColorProfile`:自有标定数据、光源锚点、render intent、二维色度表和矩阵;
|
||||
2. `CameraColorTransform`:线性 RGB ↔ luminance/chroma、近中性色/暗部保护、二维 LUT;
|
||||
3. `BaseToneTransform`:内部 headroom、Film Curve、分段 highlight roll-off;
|
||||
4. `PerceptualAdjustments`:Gamma 后的 Contrast/Luma/HSL selective 调整;
|
||||
4. `PerceptualAdjustments`:FilmCurve/可选 Gamma 后的 Contrast/Luma/HSL selective 调整;
|
||||
5. `OutputColorTransform`:工作空间→XYZ→目标空间、白点适配、目标 OETF/PQ;
|
||||
6. `DebugOverlays`:MarkHue、gamut/NaN 可视化,与最终交付图完全分离。
|
||||
|
||||
@@ -483,14 +525,36 @@ HDR headroom 和用户曲线的独立可编辑性。若为实时预览做缓存
|
||||
## 10. 仍需动态验证的问题
|
||||
|
||||
- `EffectiveFilmCurve` 数值 2、4 的公开名称及其 HighlightStrength 参数表;
|
||||
- `uGain`、`hrTrunc`、`hrMax`、`HDRMaxGain`、`HasselbladHdrRgbLimit` 在不同机型和输出模式下
|
||||
的实际数值闭环;
|
||||
- XML 六张色度 LUT 在 Flash/Tungsten/Low-Tungsten 之间的精确插值规则;
|
||||
- 动态 Film Curve `uGain` 与 `hrTrunc/hrMax/inputEV` 在不同机型、HDR 和输出模式下的实际
|
||||
数值闭环;
|
||||
- `desatHighLight` 是否只服务某个未导出的 HDR/缩略图分支;
|
||||
- `colorspaceconvert` 的 source/destination enum 到具体 profile 的完整映射,以及 sRGB transfer
|
||||
枚举未实现时的宿主替代路径;
|
||||
- exact-black、负 RGB、LUT 极值在目标 Adreno/Mali/PowerVR 驱动上的实际结果。
|
||||
|
||||
## 11. PhotonCamera 集成审计结论
|
||||
|
||||
| 原始模块 | 是否需要 | 当前状态 |
|
||||
|---|---|---|
|
||||
| `colorCorrectAll` 小写生产版 | 需要 | 已接入 105×89 手工双线性、整数 Kelvin 插值、真实矩阵/解密 neutral gain/LUT、gray/low-light 参数;相机域 headroom 保留为待真实宿主值 |
|
||||
| `filmcurve` 小写生产版 | 需要 | 已接入原库直接导出的 type=6 / companding=2 真实 65,536 点表与 nearest texel 读取 |
|
||||
| `highlightstrength` | 条件需要 | type=6 不需要;type=2/4 缺真实枚举名和宿主参数,不伪造 |
|
||||
| `gamma_hasselblad` | 条件需要 | 真实常量和 shader 已接入;默认 correction version=2/flag=false 时严格跳过 |
|
||||
| `gamma_lstar` | 条件需要 | 原始选择路径存在,但当前默认状态没有真实选择字段,不启用 |
|
||||
| `gradation` | 图结构需要 | 原始图始终构造;非恒等内容依赖真实用户曲线,当前不上传 identity 冒充 |
|
||||
| `selectivecolor` | 条件需要 | shader 已审计;缺真实用户 H/S/L 表时不启用 |
|
||||
| `mark_hue` | 最终输出不需要 | 仅 Phocus 选色交互 overlay |
|
||||
| `desatHighLight` | 未证实需要 | 未发现进入 `AddHNCSFilters` |
|
||||
| `colorspaceconvert/customCmm` | 需要等价职责 | 由 PhotonCamera 工作空间矩阵、D50 适配和 sRGB 输出 pass 承担 |
|
||||
| `sdr_icc` | HNCS 导出不直接需要 | 属于 PostViewer 固定预览路径 |
|
||||
| 大写 `ColorCorrectAll/FilmCurve/HighlightStrength` | 不需要 | 另一套接口且存在未闭合逻辑,不混入主图 |
|
||||
| `customColorSpace` | 不需要 | 含调试分屏逻辑,不是生产 CMM |
|
||||
| `rgb2Gray*` | HNCS 色彩不需要 | 灰度/打包辅助路径 |
|
||||
|
||||
原始 Colormap 与 native 调用图只证明 `Standard/Reproduction` 两个相机色彩意图。未发现
|
||||
`Natural/Portrait` 的二维表或 HNCS 分支:“Natural”属于 HNCS 名称,“Portrait”在 Phocus
|
||||
资源中是方向/题材语义。它们不应作为虚构 render intent 加入集成。
|
||||
|
||||
动态验证应使用逐 pass GPU capture 或在同一 RAW 上导出中间纹理;只比较最终 JPEG 外观无法区分
|
||||
Film Curve、Gamma、Gradation 和输出 ICC 的贡献。
|
||||
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Runtime research helper for the locally supplied Phocus libcrosssdk.so.
|
||||
*
|
||||
* CameraBackModelString() contains Phocus' name composer for internal
|
||||
* camera-back and CCD enums. WebDeviceType() filters combinations for the
|
||||
* detailed legacy model enums, although generic family enums still accept
|
||||
* more than the combinations shipped as products. The LUT lookup is queried
|
||||
* separately with the profile IDs recovered from
|
||||
* CRawColorParams::GetCPID()/CXMLLut::GetFileName().
|
||||
*/
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace {
|
||||
|
||||
using CameraBackModelString = std::string (*)(int, int);
|
||||
using WebDeviceType = int (*)(int, int);
|
||||
using GetLutFileName = std::string (*)(unsigned int);
|
||||
|
||||
template <typename T>
|
||||
T RequireSymbol(void *library, const char *name) {
|
||||
dlerror();
|
||||
void *symbol = dlsym(library, name);
|
||||
const char *error = dlerror();
|
||||
if (error != nullptr || symbol == nullptr) {
|
||||
fprintf(stderr, "dlsym(%s) failed: %s\n", name, error ? error : "null");
|
||||
exit(3);
|
||||
}
|
||||
return reinterpret_cast<T>(symbol);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "usage: %s LIBCROSSSDK_SO\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void *library = dlopen(argv[1], RTLD_NOW | RTLD_LOCAL);
|
||||
if (library == nullptr) {
|
||||
fprintf(stderr, "dlopen(%s) failed: %s\n", argv[1], dlerror());
|
||||
return 2;
|
||||
}
|
||||
|
||||
const auto camera_back_model_string =
|
||||
RequireSymbol<CameraBackModelString>(
|
||||
library,
|
||||
"_ZN12CBodyAndBack21CameraBackModelStringE16eCameraBackModel9eCCDTypes"
|
||||
);
|
||||
const auto web_device_type =
|
||||
RequireSymbol<WebDeviceType>(
|
||||
library,
|
||||
"_ZN12CBodyAndBack13WebDeviceTypeE16eCameraBackModel9eCCDTypes"
|
||||
);
|
||||
const auto get_lut_file_name =
|
||||
RequireSymbol<GetLutFileName>(
|
||||
library,
|
||||
"_ZN7CXMLLut11GetFileNameE13uColorProfile"
|
||||
);
|
||||
|
||||
constexpr int kCcdTypes[] = {
|
||||
0x04, 0x07, 0x08, 0x09, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
|
||||
};
|
||||
puts("[camera-models]");
|
||||
for (int model = 0; model <= 100; ++model) {
|
||||
for (const int ccd : kCcdTypes) {
|
||||
const int device_type = web_device_type(model, ccd);
|
||||
if (device_type != 0) {
|
||||
const std::string name = camera_back_model_string(model, ccd);
|
||||
printf(
|
||||
"cameraBackModel=%d ccdType=0x%02x "
|
||||
"webDeviceType=%d name=%s\n",
|
||||
model,
|
||||
ccd,
|
||||
device_type,
|
||||
name.c_str()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
constexpr unsigned int kColorProfiles[] = {
|
||||
0x004, 0x007, 0x008, 0x009, 0x012, 0x013,
|
||||
0x404, 0x408, 0x409, 0x40b, 0x40c, 0x40d, 0x40e,
|
||||
0x50b, 0x50c, 0x50d, 0x50e, 0x50f, 0x510, 0x511, 0x514,
|
||||
0x614, 0x714, 0x615, 0x616, 0x690f,
|
||||
};
|
||||
puts("[lut-files]");
|
||||
for (const unsigned int profile : kColorProfiles) {
|
||||
const std::string file_name = get_lut_file_name(profile);
|
||||
printf("colorProfile=0x%04x file=%s\n", profile, file_name.c_str());
|
||||
}
|
||||
|
||||
dlclose(library);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Runtime research helper for the locally supplied Phocus libcrosssdk.so.
|
||||
*
|
||||
* The static gradation tables live in BSS and are initialized by
|
||||
* CGradationManager::InitGradationCurves(), so they cannot be copied from the
|
||||
* ELF file directly. This helper asks the original constructor for each
|
||||
* film-curve/companding combination and writes the returned 65,536 float
|
||||
* samples without reimplementing any vendor math.
|
||||
*/
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr size_t kManagerBytes = 0xd0;
|
||||
constexpr size_t kExpectedSamples = 65536;
|
||||
|
||||
struct FloatSpan {
|
||||
const float *data;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
using ManagerConstructor = void (*)(void *, int, int);
|
||||
using GetGradationCurve = FloatSpan (*)(void *);
|
||||
|
||||
template <typename T>
|
||||
T RequireSymbol(void *library, const char *name) {
|
||||
dlerror();
|
||||
void *symbol = dlsym(library, name);
|
||||
const char *error = dlerror();
|
||||
if (error != nullptr || symbol == nullptr) {
|
||||
fprintf(stderr, "dlsym(%s) failed: %s\n", name, error ? error : "null");
|
||||
exit(3);
|
||||
}
|
||||
return reinterpret_cast<T>(symbol);
|
||||
}
|
||||
|
||||
uint64_t Fnv1a64(const float *values, size_t count) {
|
||||
uint64_t hash = UINT64_C(14695981039346656037);
|
||||
const uint8_t *bytes = reinterpret_cast<const uint8_t *>(values);
|
||||
const size_t byte_count = count * sizeof(float);
|
||||
for (size_t index = 0; index < byte_count; ++index) {
|
||||
hash ^= bytes[index];
|
||||
hash *= UINT64_C(1099511628211);
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
bool WriteCurve(
|
||||
const char *directory,
|
||||
int film_curve_type,
|
||||
int companding,
|
||||
const FloatSpan &curve
|
||||
) {
|
||||
char path[512];
|
||||
snprintf(
|
||||
path,
|
||||
sizeof(path),
|
||||
"%s/filmcurve-type%d-companding%d-f32le.bin",
|
||||
directory,
|
||||
film_curve_type,
|
||||
companding
|
||||
);
|
||||
FILE *output = fopen(path, "wb");
|
||||
if (output == nullptr) {
|
||||
fprintf(stderr, "fopen(%s) failed: %s\n", path, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
const size_t written = fwrite(curve.data, sizeof(float), curve.size, output);
|
||||
const int close_result = fclose(output);
|
||||
if (written != curve.size || close_result != 0) {
|
||||
fprintf(stderr, "write(%s) failed: %s\n", path, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
printf(
|
||||
"type=%d companding=%d samples=%zu first=%.9g p25=%.9g "
|
||||
"p50=%.9g p75=%.9g last=%.9g fnv1a64=%016llx file=%s\n",
|
||||
film_curve_type,
|
||||
companding,
|
||||
curve.size,
|
||||
curve.data[0],
|
||||
curve.data[curve.size / 4],
|
||||
curve.data[curve.size / 2],
|
||||
curve.data[curve.size * 3 / 4],
|
||||
curve.data[curve.size - 1],
|
||||
static_cast<unsigned long long>(Fnv1a64(curve.data, curve.size)),
|
||||
path
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 3) {
|
||||
fprintf(stderr, "usage: %s LIBCROSSSDK_SO OUTPUT_DIRECTORY\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void *library = dlopen(argv[1], RTLD_NOW | RTLD_LOCAL);
|
||||
if (library == nullptr) {
|
||||
fprintf(stderr, "dlopen(%s) failed: %s\n", argv[1], dlerror());
|
||||
return 2;
|
||||
}
|
||||
|
||||
const auto construct = RequireSymbol<ManagerConstructor>(
|
||||
library,
|
||||
"_ZN17CGradationManagerC1E14eFilmCurveType11eCompanding"
|
||||
);
|
||||
const auto get_curve = RequireSymbol<GetGradationCurve>(
|
||||
library,
|
||||
"_ZN17CGradationManager17GetGradationCurveEv"
|
||||
);
|
||||
|
||||
bool success = true;
|
||||
for (int companding = 1; companding <= 2; ++companding) {
|
||||
for (int film_curve_type = 0; film_curve_type <= 7; ++film_curve_type) {
|
||||
alignas(16) uint8_t manager[kManagerBytes] = {};
|
||||
construct(manager, film_curve_type, companding);
|
||||
const FloatSpan curve = get_curve(manager);
|
||||
if (curve.data == nullptr || curve.size != kExpectedSamples) {
|
||||
fprintf(
|
||||
stderr,
|
||||
"invalid curve type=%d companding=%d data=%p samples=%zu\n",
|
||||
film_curve_type,
|
||||
companding,
|
||||
static_cast<const void *>(curve.data),
|
||||
curve.size
|
||||
);
|
||||
success = false;
|
||||
} else {
|
||||
success &= WriteCurve(
|
||||
argv[2],
|
||||
film_curve_type,
|
||||
companding,
|
||||
curve
|
||||
);
|
||||
}
|
||||
// The destructor is local/hidden in this build. Each manager owns
|
||||
// one 256 KiB vector; allowing the short-lived extractor process
|
||||
// to reclaim these allocations at exit avoids guessing a hidden
|
||||
// ABI while keeping the source library completely unmodified.
|
||||
}
|
||||
}
|
||||
|
||||
dlclose(library);
|
||||
return success ? 0 : 4;
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
"""Package a runtime-exported Phocus gradation curve for PhotonCamera.
|
||||
|
||||
Input is the exact little-endian float table returned by the original
|
||||
CGradationManager. Phocus stores integral 16-bit code values in float
|
||||
containers; this tool verifies that invariant and stores lossless uint16
|
||||
samples plus provenance in a compact application asset.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import pathlib
|
||||
import struct
|
||||
|
||||
|
||||
MAGIC = b"HNCURV1\0"
|
||||
SCHEMA_VERSION = 1
|
||||
SAMPLE_COUNT = 65_536
|
||||
CODE_VALUE_MAX = 65_535
|
||||
FNV1A64_OFFSET_BASIS = 14_695_981_039_346_656_037
|
||||
FNV1A64_PRIME = 1_099_511_628_211
|
||||
|
||||
|
||||
def fnv1a64(data: bytes) -> int:
|
||||
value = FNV1A64_OFFSET_BASIS
|
||||
for byte in data:
|
||||
value ^= byte
|
||||
value = (value * FNV1A64_PRIME) & 0xFFFF_FFFF_FFFF_FFFF
|
||||
return value
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("input", type=pathlib.Path)
|
||||
parser.add_argument("output", type=pathlib.Path)
|
||||
parser.add_argument("--film-curve-type", type=int, required=True)
|
||||
parser.add_argument("--companding", type=int, required=True)
|
||||
parser.add_argument("--source-library-sha256", required=True)
|
||||
args = parser.parse_args()
|
||||
|
||||
source_library_sha256 = bytes.fromhex(args.source_library_sha256)
|
||||
if len(source_library_sha256) != 32:
|
||||
raise ValueError("source library SHA-256 must contain exactly 32 bytes")
|
||||
|
||||
source = args.input.read_bytes()
|
||||
if len(source) != SAMPLE_COUNT * 4:
|
||||
raise ValueError(
|
||||
f"expected {SAMPLE_COUNT * 4} input bytes, got {len(source)}"
|
||||
)
|
||||
samples_f32 = struct.unpack(f"<{SAMPLE_COUNT}f", source)
|
||||
samples_u16: list[int] = []
|
||||
for index, value in enumerate(samples_f32):
|
||||
rounded = round(value)
|
||||
if (
|
||||
not 0 <= rounded <= CODE_VALUE_MAX
|
||||
or abs(value - rounded) > 0.0
|
||||
):
|
||||
raise ValueError(f"sample {index} is not an exact uint16 code: {value}")
|
||||
samples_u16.append(rounded)
|
||||
|
||||
source_fnv1a64 = fnv1a64(source)
|
||||
header = struct.pack(
|
||||
"<8sIiiIIQ32s",
|
||||
MAGIC,
|
||||
SCHEMA_VERSION,
|
||||
args.film_curve_type,
|
||||
args.companding,
|
||||
SAMPLE_COUNT,
|
||||
CODE_VALUE_MAX,
|
||||
source_fnv1a64,
|
||||
source_library_sha256,
|
||||
)
|
||||
payload = struct.pack(f"<{SAMPLE_COUNT}H", *samples_u16)
|
||||
packaged = header + payload
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
args.output.write_bytes(packaged)
|
||||
|
||||
print(f"source_sha256={hashlib.sha256(source).hexdigest()}")
|
||||
print(f"source_fnv1a64={source_fnv1a64:016x}")
|
||||
print(f"asset_sha256={hashlib.sha256(packaged).hexdigest()}")
|
||||
print(f"asset_bytes={len(packaged)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user