@freshcoat-js/for-print 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +4 -0
- package/README.md +114 -0
- package/package.json +36 -0
- package/src/analyze.d.ts +18 -0
- package/src/analyze.js +268 -0
- package/src/balance.d.ts +7 -0
- package/src/balance.js +104 -0
- package/src/calibration.d.ts +13 -0
- package/src/calibration.js +42 -0
- package/src/chart.d.ts +37 -0
- package/src/chart.js +255 -0
- package/src/geometry.d.ts +12 -0
- package/src/geometry.js +45 -0
- package/src/index.d.ts +10 -0
- package/src/index.js +13 -0
- package/src/measure.d.ts +25 -0
- package/src/measure.js +274 -0
- package/src/plan.d.ts +17 -0
- package/src/plan.js +216 -0
- package/src/presets.d.ts +5 -0
- package/src/presets.js +26 -0
- package/src/profile.d.ts +37 -0
- package/src/profile.js +213 -0
- package/src/types.d.ts +41 -0
- package/src/types.js +6 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# @freshcoat-js/for-print
|
|
2
|
+
|
|
3
|
+
Image analysis, correction planning and calibration for CR80 cards printed
|
|
4
|
+
on YMCKO dye-sublimation ribbon printers. This is the print policy used by
|
|
5
|
+
[Freshcoat](../../README.md) Studio's "Optimize for card printer" option.
|
|
6
|
+
|
|
7
|
+
Use this package to recommend corrections from decoded pixels, apply a
|
|
8
|
+
print policy to a scene, or create profiles from measured print charts.
|
|
9
|
+
It returns analysis and [`@freshcoat-js/engine`](../engine) adjustment data; the engine
|
|
10
|
+
applies those adjustments and produces the image. It does not encode files
|
|
11
|
+
or communicate with a printer.
|
|
12
|
+
|
|
13
|
+
## What it does
|
|
14
|
+
|
|
15
|
+
- **Analyze** a decoded pixel buffer → a `PrintOptimizeOptions` recommendation
|
|
16
|
+
tuned to brightness / saturation / contrast (`analyzePixels`,
|
|
17
|
+
`buildRecommendation`).
|
|
18
|
+
- **Plan** a coat engine scene: classify each layer by print intent and attach a
|
|
19
|
+
generic coat engine `Adjust` per layer, so correction happens **per-layer at
|
|
20
|
+
paint time** instead of on the flattened card (`planScene`, `analyzeScene`,
|
|
21
|
+
`classifyIntent`, `printAdjust`).
|
|
22
|
+
- **Recommend finishing**: the whole-frame output ops that aren't per-layer
|
|
23
|
+
(`YMCKO_FINISH` → the coat engine's `FrameFinish`).
|
|
24
|
+
- **CR80 geometry**: format spec + crop fitting (`cr80Dimensions`,
|
|
25
|
+
`fitCr80CropToImage`, …).
|
|
26
|
+
|
|
27
|
+
Its only runtime package dependency is the engine. Printer-specific decisions
|
|
28
|
+
stay here; the engine supplies the color matrices, lookup tables, sharpening
|
|
29
|
+
and frame-finishing operations that implement them.
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
### Plan a scene (per-layer correction)
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { planScene, analyzeScene, YMCKO_FINISH, type ImageSampler } from "@freshcoat-js/for-print";
|
|
37
|
+
import { sampleImageNode, compileScene } from "@freshcoat-js/engine";
|
|
38
|
+
|
|
39
|
+
// Sync, policy-driven: photos get the YMCKO preset; text/QR/graphics stay pristine.
|
|
40
|
+
const presetScene = planScene(nodeTree);
|
|
41
|
+
|
|
42
|
+
// Or analyze photos as displayed, including their fit and crop.
|
|
43
|
+
const sample: ImageSampler = async (image) => {
|
|
44
|
+
const pixels = await sampleImageNode(ck, image, loadBytes);
|
|
45
|
+
if (!pixels) throw new Error(`Couldn't decode ${image.src}`);
|
|
46
|
+
return pixels;
|
|
47
|
+
};
|
|
48
|
+
const analyzedScene = await analyzeScene(sample, nodeTree);
|
|
49
|
+
|
|
50
|
+
// freshcoat does the output: per-layer adjust + the whole-frame finish.
|
|
51
|
+
const commands = compileScene(analyzedScene, {
|
|
52
|
+
width, height, textEngine, finish: YMCKO_FINISH,
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
These examples assume an initialized CanvasKit instance (`ck`), a scene and
|
|
57
|
+
text engine, and a `loadBytes(src)` function supplied by your application.
|
|
58
|
+
Choose `presetScene` for fixed corrections or `analyzedScene` for corrections
|
|
59
|
+
based on each photo. Paint the resulting commands through an engine runtime.
|
|
60
|
+
|
|
61
|
+
### Analyze a single image
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { analyzePixels } from "@freshcoat-js/for-print";
|
|
65
|
+
import { decodePixels } from "@freshcoat-js/engine";
|
|
66
|
+
|
|
67
|
+
const pixels = decodePixels(ck, bytes);
|
|
68
|
+
if (!pixels) throw new Error("Couldn't decode image");
|
|
69
|
+
const analysis = analyzePixels(pixels); // recommendation + notes
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Layer intent
|
|
73
|
+
|
|
74
|
+
`classifyIntent`: `image → photo`, `text → text`, `bitmap → code` (QR/pixel art),
|
|
75
|
+
`rect|ellipse|path → graphic`, `group|mask → container`. By default, the planner
|
|
76
|
+
attaches photo corrections only to photos. It walks containers and applies
|
|
77
|
+
corrections to leaves. An optional profile's channel balance applies to all
|
|
78
|
+
leaves, including text, vectors and codes. Whole-frame finishing acts on the
|
|
79
|
+
composited image.
|
|
80
|
+
|
|
81
|
+
For exceptions, pass `PlanPolicy.intentFor`. For example, classify a rasterized
|
|
82
|
+
logo as `graphic` so it remains pristine, or apply a specific graphic policy to
|
|
83
|
+
it. The resolver is also honored by `analyzeScene`, so an overridden image is not
|
|
84
|
+
decoded and analyzed as a photo.
|
|
85
|
+
|
|
86
|
+
## Mapping
|
|
87
|
+
|
|
88
|
+
`printAdjust(PrintOptimizeOptions)` → a coat engine `Adjust`:
|
|
89
|
+
|
|
90
|
+
- saturation + contrast → color **matrix**
|
|
91
|
+
- gamma + darkness → per-channel **LUT**
|
|
92
|
+
- sharpness → **sharpen**
|
|
93
|
+
|
|
94
|
+
The conjunctive white-clamp / black-extraction and gradient dither aren't per-layer
|
|
95
|
+
expressible; they live in the coat engine's whole-frame `FrameFinish`, which for-print
|
|
96
|
+
requests via `YMCKO_FINISH`.
|
|
97
|
+
|
|
98
|
+
## Calibration profiles
|
|
99
|
+
|
|
100
|
+
Use `createPrintProfile(reading, details)` to turn a Gray balance chart reading
|
|
101
|
+
into reusable profile data. It only emits a profile when the capture is usable,
|
|
102
|
+
and records the quality assessment plus printer, ribbon, and stock details beside
|
|
103
|
+
the fitted balance. `parsePrintProfile` remains compatible with older profiles and
|
|
104
|
+
normalizes them to the current `version: 1` schema.
|
|
105
|
+
|
|
106
|
+
Profiles describe measured channel balance for a printer, ribbon and stock
|
|
107
|
+
combination. They are not ICC profiles. Pass a profile's `balance` through
|
|
108
|
+
`PlanPolicy` to use it in scene planning, or use `withProfile()` to combine
|
|
109
|
+
it with a single-image recommendation.
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
Apache-2.0, see [`LICENSE`](./LICENSE) and [`NOTICE`](./NOTICE). Part of
|
|
114
|
+
[Freshcoat](../../README.md).
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@freshcoat-js/for-print",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Image analysis, correction planning and calibration for dye-sublimation card printers",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/dotside-studios/freshcoat.git",
|
|
9
|
+
"directory": "packages/for-print"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@freshcoat-js/engine": "0.1.0"
|
|
14
|
+
},
|
|
15
|
+
"private": false,
|
|
16
|
+
"main": "./src/index.js",
|
|
17
|
+
"types": "./src/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"src",
|
|
20
|
+
"fixtures",
|
|
21
|
+
"schema",
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"NOTICE",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./src/index.d.ts",
|
|
32
|
+
"import": "./src/index.js",
|
|
33
|
+
"default": "./src/index.js"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/analyze.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type ColorMatrix } from "@freshcoat-js/engine";
|
|
2
|
+
import type { GamutReport, ImageAnalysis, PixelData, PrintOptimizeOptions } from "./types.js";
|
|
3
|
+
export declare function correctionMatrix(o: PrintOptimizeOptions): ColorMatrix | null;
|
|
4
|
+
export declare function measureGamut(pixels: PixelData, opts: PrintOptimizeOptions): GamutReport;
|
|
5
|
+
export type ImageStats = {
|
|
6
|
+
meanBrightness: number;
|
|
7
|
+
medianBrightness: number;
|
|
8
|
+
shadow: number;
|
|
9
|
+
highlight: number;
|
|
10
|
+
avgSaturation: number;
|
|
11
|
+
contrast: number;
|
|
12
|
+
};
|
|
13
|
+
export declare function analyzeStats(pixels: PixelData): ImageStats;
|
|
14
|
+
export declare function analyzePixels(pixels: PixelData): ImageAnalysis;
|
|
15
|
+
export declare function buildRecommendation(stats: ImageStats): {
|
|
16
|
+
recommendation: PrintOptimizeOptions;
|
|
17
|
+
notes: Array<string>;
|
|
18
|
+
};
|
package/src/analyze.js
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { concatColorMatrix, contrastMatrix, identityColorMatrix, saturationMatrix, } from "@freshcoat-js/engine";
|
|
2
|
+
import { detectOrientation } from "./geometry.js";
|
|
3
|
+
// The cross-channel half of a correction — saturation then contrast — as one
|
|
4
|
+
// matrix, or null when both are identity. Shared with the planner so a measurement
|
|
5
|
+
// and the render it describes can never diverge.
|
|
6
|
+
export function correctionMatrix(o) {
|
|
7
|
+
let m = identityColorMatrix();
|
|
8
|
+
let touched = false;
|
|
9
|
+
if (o.saturation !== 1) {
|
|
10
|
+
m = concatColorMatrix(saturationMatrix(o.saturation), m);
|
|
11
|
+
touched = true;
|
|
12
|
+
}
|
|
13
|
+
if (o.contrast !== 1) {
|
|
14
|
+
m = concatColorMatrix(contrastMatrix(o.contrast), m);
|
|
15
|
+
touched = true;
|
|
16
|
+
}
|
|
17
|
+
return touched ? m : null;
|
|
18
|
+
}
|
|
19
|
+
// Rec.709 luma, the axis chroma is scaled toward.
|
|
20
|
+
const LUMA = [0.2126, 0.7152, 0.0722];
|
|
21
|
+
// The largest factor in [0,1] that brings one channel back inside [0,1] when the
|
|
22
|
+
// color is scaled toward `l`. Mirrors the shader's fitScale (see freshcoat's
|
|
23
|
+
// adjust pass) so the number reported is the pullback actually rendered.
|
|
24
|
+
function fitScale(v, l) {
|
|
25
|
+
const d = v - l;
|
|
26
|
+
if (v > 1 && d > 0)
|
|
27
|
+
return (1 - l) / d;
|
|
28
|
+
if (v < 0 && d < 0)
|
|
29
|
+
return -l / d;
|
|
30
|
+
return 1;
|
|
31
|
+
}
|
|
32
|
+
// Measure what a correction costs at the top of the range: how much of the image
|
|
33
|
+
// it drives past full scale, and how much chroma comes back to fit. Reports the
|
|
34
|
+
// pressure itself, so the answer is the same whether the render clips or
|
|
35
|
+
// preserves hue.
|
|
36
|
+
export function measureGamut(pixels, opts) {
|
|
37
|
+
const m = correctionMatrix(opts);
|
|
38
|
+
const { data } = pixels;
|
|
39
|
+
const total = data.length / 4;
|
|
40
|
+
if (!m || total === 0)
|
|
41
|
+
return { clipped: 0, pullback: 0 };
|
|
42
|
+
let clipped = 0;
|
|
43
|
+
let pullbackSum = 0;
|
|
44
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
45
|
+
const r = data[i] / 255;
|
|
46
|
+
const g = data[i + 1] / 255;
|
|
47
|
+
const b = data[i + 2] / 255;
|
|
48
|
+
const out = [0, 0, 0];
|
|
49
|
+
for (let c = 0; c < 3; c++) {
|
|
50
|
+
out[c] =
|
|
51
|
+
m[c * 5] * r + m[c * 5 + 1] * g + m[c * 5 + 2] * b + m[c * 5 + 4];
|
|
52
|
+
}
|
|
53
|
+
if (out[0] >= 0 &&
|
|
54
|
+
out[0] <= 1 &&
|
|
55
|
+
out[1] >= 0 &&
|
|
56
|
+
out[1] <= 1 &&
|
|
57
|
+
out[2] >= 0 &&
|
|
58
|
+
out[2] <= 1) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
clipped++;
|
|
62
|
+
const l = Math.min(1, Math.max(0, LUMA[0] * out[0] + LUMA[1] * out[1] + LUMA[2] * out[2]));
|
|
63
|
+
const t = Math.min(fitScale(out[0], l), fitScale(out[1], l), fitScale(out[2], l));
|
|
64
|
+
pullbackSum += 1 - t;
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
clipped: clipped / total,
|
|
68
|
+
pullback: clipped === 0 ? 0 : pullbackSum / clipped,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
// The i-th value where the cumulative histogram first reaches p·total.
|
|
72
|
+
function percentile(hist, total, p) {
|
|
73
|
+
const target = p * total;
|
|
74
|
+
let cum = 0;
|
|
75
|
+
for (let i = 0; i < 256; i++) {
|
|
76
|
+
cum += hist[i];
|
|
77
|
+
if (cum >= target)
|
|
78
|
+
return i;
|
|
79
|
+
}
|
|
80
|
+
return 255;
|
|
81
|
+
}
|
|
82
|
+
// Compute robust statistics in a single pass — one fixed 256-int histogram, no
|
|
83
|
+
// per-pixel array (the old approach pushed every pixel's brightness into a growing
|
|
84
|
+
// list, millions of floats for a full-res photo).
|
|
85
|
+
export function analyzeStats(pixels) {
|
|
86
|
+
const { data, width, height } = pixels;
|
|
87
|
+
const pixelCount = width * height;
|
|
88
|
+
if (pixelCount === 0) {
|
|
89
|
+
return {
|
|
90
|
+
meanBrightness: 0,
|
|
91
|
+
medianBrightness: 0,
|
|
92
|
+
shadow: 0,
|
|
93
|
+
highlight: 0,
|
|
94
|
+
avgSaturation: 0,
|
|
95
|
+
contrast: 0,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
const hist = new Uint32Array(256);
|
|
99
|
+
let totalBrightness = 0;
|
|
100
|
+
let totalSaturation = 0;
|
|
101
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
102
|
+
const r = data[i];
|
|
103
|
+
const g = data[i + 1];
|
|
104
|
+
const b = data[i + 2];
|
|
105
|
+
const brightness = 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
106
|
+
totalBrightness += brightness;
|
|
107
|
+
hist[Math.min(255, Math.max(0, Math.round(brightness)))]++;
|
|
108
|
+
const max = Math.max(r, g, b);
|
|
109
|
+
const min = Math.min(r, g, b);
|
|
110
|
+
const l = (max + min) / 2 / 255;
|
|
111
|
+
const sat = max === min ? 0 : (max - min) / 255 / (l <= 0.5 ? 2 * l : 2 - 2 * l);
|
|
112
|
+
totalSaturation += Math.min(sat, 1);
|
|
113
|
+
}
|
|
114
|
+
const shadow = percentile(hist, pixelCount, 0.05);
|
|
115
|
+
const highlight = percentile(hist, pixelCount, 0.95);
|
|
116
|
+
return {
|
|
117
|
+
meanBrightness: totalBrightness / pixelCount,
|
|
118
|
+
medianBrightness: percentile(hist, pixelCount, 0.5),
|
|
119
|
+
shadow,
|
|
120
|
+
highlight,
|
|
121
|
+
avgSaturation: totalSaturation / pixelCount,
|
|
122
|
+
contrast: (highlight - shadow) / 255,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
// Inspect a decoded RGBA buffer and recommend print-optimization settings. Pure —
|
|
126
|
+
// the caller decodes (e.g. via freshcoat's `decodePixels`, optionally rendering
|
|
127
|
+
// the layer as it appears on the card); for-print never touches a canvas.
|
|
128
|
+
// `imageWidth`/`imageHeight` reflect the buffer you pass.
|
|
129
|
+
export function analyzePixels(pixels) {
|
|
130
|
+
const stats = analyzeStats(pixels);
|
|
131
|
+
const { recommendation, notes } = buildRecommendation(stats);
|
|
132
|
+
const detectedOrientation = detectOrientation(pixels.width, pixels.height);
|
|
133
|
+
notes.unshift(`Buffer is ${pixels.width}x${pixels.height} — ${detectedOrientation} orientation`);
|
|
134
|
+
const gamut = measureGamut(pixels, recommendation);
|
|
135
|
+
if (gamut.clipped > 0) {
|
|
136
|
+
notes.push(`${Math.round(gamut.clipped * 100)}% of this image is too saturated for the correction to keep — ` +
|
|
137
|
+
`pulled back ${Math.round(gamut.pullback * 100)}% there to hold the hue`);
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
avgBrightness: stats.meanBrightness,
|
|
141
|
+
avgSaturation: stats.avgSaturation,
|
|
142
|
+
contrast: stats.contrast,
|
|
143
|
+
detectedOrientation,
|
|
144
|
+
imageWidth: pixels.width,
|
|
145
|
+
imageHeight: pixels.height,
|
|
146
|
+
recommendation,
|
|
147
|
+
gamut,
|
|
148
|
+
notes,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
// Pure: derive recommended print settings from robust image statistics. Brightness
|
|
152
|
+
// decisions key off the median (not the mean), with explicit guards for the tails —
|
|
153
|
+
// protect near-clipping highlights, and ease off darkening when shadows are already
|
|
154
|
+
// crushed. Contrast keys off the p5–p95 spread.
|
|
155
|
+
export function buildRecommendation(stats) {
|
|
156
|
+
const { medianBrightness: mid, shadow, highlight, avgSaturation, contrast, } = stats;
|
|
157
|
+
const notes = [];
|
|
158
|
+
let saturation = 1.0;
|
|
159
|
+
let contrastMul = 1.0;
|
|
160
|
+
let gamma = 1.0;
|
|
161
|
+
const sharpness = 0.3;
|
|
162
|
+
let darkness = 0.0;
|
|
163
|
+
// Pastel/light designs: bright, desaturated, low tonal spread.
|
|
164
|
+
const isPastel = mid > 180 && avgSaturation < 0.3 && contrast < 0.3;
|
|
165
|
+
// Saturated designed art — a brand gradient or flat vector ground rather than a
|
|
166
|
+
// photograph. The intersection of the two buckets below that would otherwise
|
|
167
|
+
// fire hardest on it: already at the top saturation bucket, and inside the
|
|
168
|
+
// low-spread contrast bucket. Its narrow luma spread is one hue sitting in a
|
|
169
|
+
// band, not a flat exposure, so the boosts those buckets prescribe are aimed at
|
|
170
|
+
// a problem it doesn't have and push its leading channel past full scale.
|
|
171
|
+
const isBrandArt = avgSaturation > 0.55 && contrast < 0.35;
|
|
172
|
+
if (isPastel) {
|
|
173
|
+
saturation = 1.2;
|
|
174
|
+
gamma = 0.92;
|
|
175
|
+
darkness = 0.1;
|
|
176
|
+
contrastMul = 1.1;
|
|
177
|
+
notes.push("Pastel/light design detected — using gentle settings to preserve subtle colors");
|
|
178
|
+
notes.push("Tip: adjust darkness slider manually to taste, heavy overlay will shift delicate tones");
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
// --- Saturation ---
|
|
182
|
+
if (isBrandArt) {
|
|
183
|
+
saturation = 1.0;
|
|
184
|
+
notes.push("Saturated brand art — already dense, no saturation boost applied");
|
|
185
|
+
}
|
|
186
|
+
else if (avgSaturation < 0.2) {
|
|
187
|
+
saturation = 1.45;
|
|
188
|
+
notes.push("Very low saturation — aggressive boost needed for dye-sub");
|
|
189
|
+
}
|
|
190
|
+
else if (avgSaturation < 0.35) {
|
|
191
|
+
saturation = 1.35;
|
|
192
|
+
notes.push("Low saturation — strong boost for dye-sub");
|
|
193
|
+
}
|
|
194
|
+
else if (avgSaturation < 0.55) {
|
|
195
|
+
saturation = 1.25;
|
|
196
|
+
notes.push("Moderate saturation — standard dye-sub boost");
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
saturation = 1.15;
|
|
200
|
+
notes.push("Good saturation — light dye-sub compensation");
|
|
201
|
+
}
|
|
202
|
+
// --- Brightness / darkness (median-driven) ---
|
|
203
|
+
if (mid > 200) {
|
|
204
|
+
gamma = 0.85;
|
|
205
|
+
darkness = 0.3;
|
|
206
|
+
notes.push("Very bright image — 30% overlay + gamma to prevent washout");
|
|
207
|
+
}
|
|
208
|
+
else if (mid > 160) {
|
|
209
|
+
gamma = 0.85;
|
|
210
|
+
darkness = 0.2;
|
|
211
|
+
notes.push("Bright image — 20% overlay + gamma for dye-sub");
|
|
212
|
+
}
|
|
213
|
+
else if (mid > 120) {
|
|
214
|
+
gamma = 0.88;
|
|
215
|
+
darkness = 0.15;
|
|
216
|
+
notes.push("Moderate brightness — light overlay + gamma");
|
|
217
|
+
}
|
|
218
|
+
else if (mid < 60) {
|
|
219
|
+
gamma = 0.95;
|
|
220
|
+
darkness = 0.0;
|
|
221
|
+
notes.push("Dark image — minimal adjustment, already dense");
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
gamma = 0.9;
|
|
225
|
+
darkness = 0.1;
|
|
226
|
+
notes.push("Medium-dark image — light dye-sub compensation");
|
|
227
|
+
}
|
|
228
|
+
// Highlight guard: a bright subject on a mid ground has a moderate median but
|
|
229
|
+
// blown highlights — add a touch of darkening to hold them even so.
|
|
230
|
+
if (highlight > 248 && darkness < 0.15) {
|
|
231
|
+
darkness = 0.15;
|
|
232
|
+
notes.push("Highlights near clipping — light overlay to hold detail");
|
|
233
|
+
}
|
|
234
|
+
// Shadow guard: if the darkest content is already dense, ease off darkening so
|
|
235
|
+
// shadow detail survives the dye-sub compression.
|
|
236
|
+
if (shadow < 12 && darkness > 0.1) {
|
|
237
|
+
darkness = 0.1;
|
|
238
|
+
notes.push("Shadows already dense — easing darkening to keep detail");
|
|
239
|
+
}
|
|
240
|
+
// --- Contrast (p5–p95 spread) ---
|
|
241
|
+
if (isBrandArt) {
|
|
242
|
+
contrastMul = 1.05;
|
|
243
|
+
notes.push("Narrow spread is the hue, not flatness — minimal contrast boost");
|
|
244
|
+
}
|
|
245
|
+
else if (contrast < 0.35) {
|
|
246
|
+
contrastMul = 1.3;
|
|
247
|
+
notes.push("Low tonal spread — strong contrast boost to add definition");
|
|
248
|
+
}
|
|
249
|
+
else if (contrast < 0.6) {
|
|
250
|
+
contrastMul = 1.2;
|
|
251
|
+
notes.push("Moderate tonal spread — standard contrast boost");
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
contrastMul = 1.1;
|
|
255
|
+
notes.push("Wide tonal spread — light compensation for dye spread");
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return {
|
|
259
|
+
recommendation: {
|
|
260
|
+
saturation,
|
|
261
|
+
contrast: contrastMul,
|
|
262
|
+
gamma,
|
|
263
|
+
sharpness,
|
|
264
|
+
darkness,
|
|
265
|
+
},
|
|
266
|
+
notes,
|
|
267
|
+
};
|
|
268
|
+
}
|
package/src/balance.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ChartReading } from "./measure.js";
|
|
2
|
+
import type { ChannelBalance } from "./types.js";
|
|
3
|
+
export type FitChannelBalanceOptions = {
|
|
4
|
+
allowUnsafe?: boolean;
|
|
5
|
+
};
|
|
6
|
+
export declare function fitChannelBalance(reading: ChartReading, options?: FitChannelBalanceOptions): ChannelBalance | null;
|
|
7
|
+
export declare function isBalanceMeaningful(balance: ChannelBalance): boolean;
|