2
|
1 |
/*
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
3 |
*
|
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
|
6 |
* published by the Free Software Foundation. Sun designates this
|
|
7 |
* particular file as subject to the "Classpath" exception as provided
|
|
8 |
* by Sun in the LICENSE file that accompanied this code.
|
|
9 |
*
|
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
14 |
* accompanied this code).
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GNU General Public License version
|
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
19 |
*
|
|
20 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
21 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
22 |
* have any questions.
|
|
23 |
*/
|
|
24 |
|
|
25 |
// This file is available under and governed by the GNU General Public
|
|
26 |
// License version 2 only, as published by the Free Software Foundation.
|
|
27 |
// However, the following notice accompanied the original version of this
|
|
28 |
// file:
|
|
29 |
//
|
|
30 |
//
|
|
31 |
// Little cms
|
2394
|
32 |
// Copyright (C) 1998-2007 Marti Maria
|
2
|
33 |
//
|
|
34 |
// Permission is hereby granted, free of charge, to any person obtaining
|
|
35 |
// a copy of this software and associated documentation files (the "Software"),
|
|
36 |
// to deal in the Software without restriction, including without limitation
|
|
37 |
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
38 |
// and/or sell copies of the Software, and to permit persons to whom the Software
|
|
39 |
// is furnished to do so, subject to the following conditions:
|
|
40 |
//
|
|
41 |
// The above copyright notice and this permission notice shall be included in
|
|
42 |
// all copies or substantial portions of the Software.
|
|
43 |
//
|
|
44 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
45 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
|
46 |
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
47 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
48 |
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
49 |
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
50 |
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
51 |
|
|
52 |
|
|
53 |
// Postscript level 2 operators
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
#include "lcms.h"
|
|
58 |
#include <time.h>
|
|
59 |
#include <stdarg.h>
|
|
60 |
|
|
61 |
// PostScript ColorRenderingDictionary and ColorSpaceArray
|
|
62 |
|
|
63 |
LCMSAPI DWORD LCMSEXPORT cmsGetPostScriptCSA(cmsHPROFILE hProfile, int Intent, LPVOID Buffer, DWORD dwBufferLen);
|
|
64 |
LCMSAPI DWORD LCMSEXPORT cmsGetPostScriptCRD(cmsHPROFILE hProfile, int Intent, LPVOID Buffer, DWORD dwBufferLen);
|
|
65 |
LCMSAPI DWORD LCMSEXPORT cmsGetPostScriptCRDEx(cmsHPROFILE hProfile, int Intent, DWORD dwFlags, LPVOID Buffer, DWORD dwBufferLen);
|
|
66 |
// -------------------------------------------------------------------- Implementation
|
|
67 |
|
|
68 |
#define MAXPSCOLS 60 // Columns on tables
|
|
69 |
|
|
70 |
/*
|
|
71 |
Implementation
|
|
72 |
--------------
|
|
73 |
|
|
74 |
PostScript does use XYZ as its internal PCS. But since PostScript
|
|
75 |
interpolation tables are limited to 8 bits, I use Lab as a way to
|
|
76 |
improve the accuracy, favoring perceptual results. So, for the creation
|
|
77 |
of each CRD, CSA the profiles are converted to Lab via a device
|
|
78 |
link between profile -> Lab or Lab -> profile. The PS code necessary to
|
|
79 |
convert Lab <-> XYZ is also included.
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
Color Space Arrays (CSA)
|
|
84 |
==================================================================================
|
|
85 |
|
|
86 |
In order to obtain precission, code chooses between three ways to implement
|
|
87 |
the device -> XYZ transform. These cases identifies monochrome profiles (often
|
|
88 |
implemented as a set of curves), matrix-shaper and LUT-based.
|
|
89 |
|
|
90 |
Monochrome
|
|
91 |
-----------
|
|
92 |
|
|
93 |
This is implemented as /CIEBasedA CSA. The prelinearization curve is
|
|
94 |
placed into /DecodeA section, and matrix equals to D50. Since here is
|
|
95 |
no interpolation tables, I do the conversion directly to XYZ
|
|
96 |
|
|
97 |
NOTE: CLUT-based monochrome profiles are NOT supported. So, cmsFLAGS_MATRIXINPUT
|
|
98 |
flag is forced on such profiles.
|
|
99 |
|
|
100 |
[ /CIEBasedA
|
|
101 |
<<
|
|
102 |
/DecodeA { transfer function } bind
|
|
103 |
/MatrixA [D50]
|
|
104 |
/RangeLMN [ 0.0 D50X 0.0 D50Y 0.0 D50Z ]
|
|
105 |
/WhitePoint [D50]
|
|
106 |
/BlackPoint [BP]
|
|
107 |
/RenderingIntent (intent)
|
|
108 |
>>
|
|
109 |
]
|
|
110 |
|
|
111 |
On simpler profiles, the PCS is already XYZ, so no conversion is required.
|
|
112 |
|
|
113 |
|
|
114 |
Matrix-shaper based
|
|
115 |
-------------------
|
|
116 |
|
|
117 |
This is implemented both with /CIEBasedABC or /CIEBasedDEF on dependig
|
|
118 |
of profile implementation. Since here is no interpolation tables, I do
|
|
119 |
the conversion directly to XYZ
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
[ /CIEBasedABC
|
|
124 |
<<
|
|
125 |
/DecodeABC [ {transfer1} {transfer2} {transfer3} ]
|
|
126 |
/MatrixABC [Matrix]
|
|
127 |
/RangeLMN [ 0.0 D50X 0.0 D50Y 0.0 D50Z ]
|
|
128 |
/DecodeLMN [ { / 2} dup dup ]
|
|
129 |
/WhitePoint [D50]
|
|
130 |
/BlackPoint [BP]
|
|
131 |
/RenderingIntent (intent)
|
|
132 |
>>
|
|
133 |
]
|
|
134 |
|
|
135 |
|
|
136 |
CLUT based
|
|
137 |
----------
|
|
138 |
|
|
139 |
Lab is used in such cases.
|
|
140 |
|
|
141 |
[ /CIEBasedDEF
|
|
142 |
<<
|
|
143 |
/DecodeDEF [ <prelinearization> ]
|
|
144 |
/Table [ p p p [<...>]]
|
|
145 |
/RangeABC [ 0 1 0 1 0 1]
|
|
146 |
/DecodeABC[ <postlinearization> ]
|
2394
|
147 |
/RangeLMN [ -0.236 1.254 0 1 -0.635 1.640 ]
|
|
148 |
% -128/500 1+127/500 0 1 -127/200 1+128/200
|
2
|
149 |
/MatrixABC [ 1 1 1 1 0 0 0 0 -1]
|
|
150 |
/WhitePoint [D50]
|
|
151 |
/BlackPoint [BP]
|
|
152 |
/RenderingIntent (intent)
|
|
153 |
]
|
|
154 |
|
|
155 |
|
|
156 |
Color Rendering Dictionaries (CRD)
|
|
157 |
==================================
|
|
158 |
These are always implemented as CLUT, and always are using Lab. Since CRD are expected to
|
|
159 |
be used as resources, the code adds the definition as well.
|
|
160 |
|
|
161 |
<<
|
|
162 |
/ColorRenderingType 1
|
|
163 |
/WhitePoint [ D50 ]
|
|
164 |
/BlackPoint [BP]
|
|
165 |
/MatrixPQR [ Bradford ]
|
|
166 |
/RangePQR [-0.125 1.375 -0.125 1.375 -0.125 1.375 ]
|
|
167 |
/TransformPQR [
|
|
168 |
{4 index 3 get div 2 index 3 get mul exch pop exch pop exch pop exch pop } bind
|
|
169 |
{4 index 4 get div 2 index 4 get mul exch pop exch pop exch pop exch pop } bind
|
|
170 |
{4 index 5 get div 2 index 5 get mul exch pop exch pop exch pop exch pop } bind
|
|
171 |
]
|
|
172 |
/MatrixABC <...>
|
|
173 |
/EncodeABC <...>
|
|
174 |
/RangeABC <.. used for XYZ -> Lab>
|
|
175 |
/EncodeLMN
|
|
176 |
/RenderTable [ p p p [<...>]]
|
|
177 |
|
|
178 |
/RenderingIntent (Perceptual)
|
|
179 |
>>
|
|
180 |
/Current exch /ColorRendering defineresource pop
|
|
181 |
|
|
182 |
|
|
183 |
The following stages are used to convert from XYZ to Lab
|
|
184 |
--------------------------------------------------------
|
|
185 |
|
|
186 |
Input is given at LMN stage on X, Y, Z
|
|
187 |
|
|
188 |
Encode LMN gives us f(X/Xn), f(Y/Yn), f(Z/Zn)
|
|
189 |
|
|
190 |
/EncodeLMN [
|
|
191 |
|
|
192 |
{ 0.964200 div dup 0.008856 le {7.787 mul 16 116 div add}{1 3 div exp} ifelse } bind
|
|
193 |
{ 1.000000 div dup 0.008856 le {7.787 mul 16 116 div add}{1 3 div exp} ifelse } bind
|
|
194 |
{ 0.824900 div dup 0.008856 le {7.787 mul 16 116 div add}{1 3 div exp} ifelse } bind
|
|
195 |
|
|
196 |
]
|
|
197 |
|
|
198 |
|
|
199 |
MatrixABC is used to compute f(Y/Yn), f(X/Xn) - f(Y/Yn), f(Y/Yn) - f(Z/Zn)
|
|
200 |
|
|
201 |
| 0 1 0|
|
|
202 |
| 1 -1 0|
|
|
203 |
| 0 1 -1|
|
|
204 |
|
|
205 |
/MatrixABC [ 0 1 0 1 -1 1 0 0 -1 ]
|
|
206 |
|
|
207 |
EncodeABC finally gives Lab values.
|
|
208 |
|
|
209 |
/EncodeABC [
|
|
210 |
{ 116 mul 16 sub 100 div } bind
|
|
211 |
{ 500 mul 128 add 255 div } bind
|
|
212 |
{ 200 mul 128 add 255 div } bind
|
|
213 |
]
|
|
214 |
|
|
215 |
The following stages are used to convert Lab to XYZ
|
|
216 |
----------------------------------------------------
|
|
217 |
|
|
218 |
/RangeABC [ 0 1 0 1 0 1]
|
|
219 |
/DecodeABC [ { 100 mul 16 add 116 div } bind
|
|
220 |
{ 255 mul 128 sub 500 div } bind
|
|
221 |
{ 255 mul 128 sub 200 div } bind
|
|
222 |
]
|
|
223 |
|
|
224 |
/MatrixABC [ 1 1 1 1 0 0 0 0 -1]
|
|
225 |
/DecodeLMN [
|
|
226 |
{dup 6 29 div ge {dup dup mul mul} {4 29 div sub 108 841 div mul} ifelse 0.964200 mul} bind
|
|
227 |
{dup 6 29 div ge {dup dup mul mul} {4 29 div sub 108 841 div mul} ifelse } bind
|
|
228 |
{dup 6 29 div ge {dup dup mul mul} {4 29 div sub 108 841 div mul} ifelse 0.824900 mul} bind
|
|
229 |
]
|
|
230 |
|
|
231 |
|
|
232 |
*/
|
|
233 |
|
|
234 |
/*
|
|
235 |
|
|
236 |
PostScript algorithms discussion.
|
|
237 |
=========================================================================================================
|
|
238 |
|
|
239 |
1D interpolation algorithm
|
|
240 |
|
|
241 |
|
|
242 |
1D interpolation (float)
|
|
243 |
------------------------
|
|
244 |
|
|
245 |
val2 = Domain * Value;
|
|
246 |
|
|
247 |
cell0 = (int) floor(val2);
|
|
248 |
cell1 = (int) ceil(val2);
|
|
249 |
|
|
250 |
rest = val2 - cell0;
|
|
251 |
|
|
252 |
y0 = LutTable[cell0] ;
|
|
253 |
y1 = LutTable[cell1] ;
|
|
254 |
|
|
255 |
y = y0 + (y1 - y0) * rest;
|
|
256 |
|
|
257 |
|
|
258 |
|
|
259 |
PostScript code Stack
|
|
260 |
================================================
|
|
261 |
|
|
262 |
{ % v
|
|
263 |
<check 0..1.0>
|
|
264 |
[array] % v tab
|
|
265 |
dup % v tab tab
|
|
266 |
length 1 sub % v tab dom
|
|
267 |
|
|
268 |
3 -1 roll % tab dom v
|
|
269 |
|
|
270 |
mul % tab val2
|
|
271 |
dup % tab val2 val2
|
|
272 |
dup % tab val2 val2 val2
|
|
273 |
floor cvi % tab val2 val2 cell0
|
|
274 |
exch % tab val2 cell0 val2
|
|
275 |
ceiling cvi % tab val2 cell0 cell1
|
|
276 |
|
|
277 |
3 index % tab val2 cell0 cell1 tab
|
|
278 |
exch % tab val2 cell0 tab cell1
|
|
279 |
get % tab val2 cell0 y1
|
|
280 |
|
|
281 |
4 -1 roll % val2 cell0 y1 tab
|
|
282 |
3 -1 roll % val2 y1 tab cell0
|
|
283 |
get % val2 y1 y0
|
|
284 |
|
|
285 |
dup % val2 y1 y0 y0
|
|
286 |
3 1 roll % val2 y0 y1 y0
|
|
287 |
|
|
288 |
sub % val2 y0 (y1-y0)
|
|
289 |
3 -1 roll % y0 (y1-y0) val2
|
|
290 |
dup % y0 (y1-y0) val2 val2
|
|
291 |
floor cvi % y0 (y1-y0) val2 floor(val2)
|
|
292 |
sub % y0 (y1-y0) rest
|
|
293 |
mul % y0 t1
|
|
294 |
add % y
|
|
295 |
65535 div % result
|
|
296 |
|
|
297 |
} bind
|
|
298 |
|
|
299 |
|
|
300 |
*/
|
|
301 |
|
|
302 |
static icTagSignature Device2PCSTab[] = {icSigAToB0Tag, // Perceptual
|
|
303 |
icSigAToB1Tag, // Relative colorimetric
|
|
304 |
icSigAToB2Tag, // Saturation
|
|
305 |
icSigAToB1Tag }; // Absolute colorimetric
|
|
306 |
// (Relative/WhitePoint)
|
|
307 |
|
|
308 |
|
|
309 |
// --------------------------------------------------------------- Memory Stream
|
|
310 |
//
|
|
311 |
// This struct holds the memory block currently being write
|
|
312 |
//
|
|
313 |
|
|
314 |
typedef struct {
|
|
315 |
LPBYTE Block;
|
|
316 |
LPBYTE Ptr;
|
|
317 |
DWORD dwMax;
|
|
318 |
DWORD dwUsed;
|
|
319 |
int MaxCols;
|
|
320 |
int Col;
|
|
321 |
int HasError;
|
|
322 |
|
|
323 |
} MEMSTREAM, FAR* LPMEMSTREAM;
|
|
324 |
|
|
325 |
|
|
326 |
typedef struct {
|
|
327 |
LPLUT Lut;
|
|
328 |
LPMEMSTREAM m;
|
|
329 |
|
|
330 |
int FirstComponent;
|
|
331 |
int SecondComponent;
|
|
332 |
|
|
333 |
int bps;
|
|
334 |
const char* PreMaj;
|
|
335 |
const char* PostMaj;
|
|
336 |
const char* PreMin;
|
|
337 |
const char* PostMin;
|
|
338 |
|
|
339 |
int lIsInput; // Handle L* encoding
|
|
340 |
int FixWhite; // Force mapping of pure white
|
|
341 |
|
|
342 |
icColorSpaceSignature ColorSpace; // ColorSpace of profile
|
|
343 |
|
|
344 |
|
|
345 |
} SAMPLERCARGO, FAR* LPSAMPLERCARGO;
|
|
346 |
|
|
347 |
|
|
348 |
// Creates a ready to use memory stream
|
|
349 |
static
|
|
350 |
LPMEMSTREAM CreateMemStream(LPBYTE Buffer, DWORD dwMax, int MaxCols)
|
|
351 |
{
|
2394
|
352 |
LPMEMSTREAM m = (LPMEMSTREAM) _cmsMalloc(sizeof(MEMSTREAM));
|
|
353 |
if (m == NULL) return NULL;
|
2
|
354 |
|
|
355 |
ZeroMemory(m, sizeof(MEMSTREAM));
|
|
356 |
|
|
357 |
m -> Block = m -> Ptr = Buffer;
|
|
358 |
m -> dwMax = dwMax;
|
|
359 |
m -> dwUsed = 0;
|
|
360 |
m -> MaxCols = MaxCols;
|
|
361 |
m -> Col = 0;
|
|
362 |
m -> HasError = 0;
|
|
363 |
|
|
364 |
return m;
|
|
365 |
}
|
|
366 |
|
|
367 |
|
|
368 |
|
|
369 |
// Convert to byte
|
|
370 |
static
|
|
371 |
BYTE Word2Byte(WORD w)
|
|
372 |
{
|
|
373 |
return (BYTE) floor((double) w / 257.0 + 0.5);
|
|
374 |
}
|
|
375 |
|
|
376 |
|
|
377 |
// Convert to byte (using ICC2 notation)
|
|
378 |
|
|
379 |
static
|
|
380 |
BYTE L2Byte(WORD w)
|
|
381 |
{
|
2394
|
382 |
int ww = w + 0x0080;
|
2
|
383 |
|
2394
|
384 |
if (ww > 0xFFFF) return 0xFF;
|
2
|
385 |
|
|
386 |
return (BYTE) ((WORD) (ww >> 8) & 0xFF);
|
|
387 |
}
|
|
388 |
|
|
389 |
// Write a raw, uncooked byte. Check for space
|
|
390 |
static
|
|
391 |
void WriteRawByte(LPMEMSTREAM m, BYTE b)
|
|
392 |
{
|
|
393 |
if (m -> dwUsed + 1 > m -> dwMax) {
|
|
394 |
m -> HasError = 1;
|
|
395 |
}
|
|
396 |
|
|
397 |
if (!m ->HasError && m ->Block) {
|
|
398 |
*m ->Ptr++ = b;
|
|
399 |
}
|
|
400 |
|
|
401 |
m -> dwUsed++;
|
|
402 |
}
|
|
403 |
|
|
404 |
// Write a cooked byte
|
|
405 |
static
|
|
406 |
void WriteByte(LPMEMSTREAM m, BYTE b)
|
|
407 |
{
|
|
408 |
static const BYTE Hex[] = "0123456789ABCDEF";
|
|
409 |
BYTE c;
|
|
410 |
|
|
411 |
c = Hex[(b >> 4) & 0x0f];
|
|
412 |
WriteRawByte(m, c);
|
|
413 |
|
|
414 |
c = Hex[b & 0x0f];
|
|
415 |
WriteRawByte(m, c);
|
|
416 |
|
|
417 |
m -> Col += 2;
|
|
418 |
|
|
419 |
if (m -> Col > m -> MaxCols) {
|
|
420 |
|
|
421 |
WriteRawByte(m, '\n');
|
|
422 |
m -> Col = 0;
|
|
423 |
}
|
|
424 |
|
|
425 |
}
|
|
426 |
|
2394
|
427 |
// Does write a formatted string. Guaranteed to be 2048 bytes at most.
|
2
|
428 |
static
|
|
429 |
void Writef(LPMEMSTREAM m, const char *frm, ...)
|
|
430 |
{
|
|
431 |
va_list args;
|
|
432 |
LPBYTE pt;
|
|
433 |
BYTE Buffer[2048];
|
|
434 |
|
|
435 |
va_start(args, frm);
|
|
436 |
|
2394
|
437 |
vsnprintf((char*) Buffer, 2048, frm, args);
|
2
|
438 |
|
|
439 |
for (pt = Buffer; *pt; pt++) {
|
|
440 |
|
|
441 |
WriteRawByte(m, *pt);
|
|
442 |
}
|
|
443 |
|
|
444 |
va_end(args);
|
|
445 |
}
|
|
446 |
|
|
447 |
|
|
448 |
|
|
449 |
// ----------------------------------------------------------------- PostScript generation
|
|
450 |
|
|
451 |
|
|
452 |
// Removes offending Carriage returns
|
|
453 |
static
|
|
454 |
char* RemoveCR(const char* txt)
|
|
455 |
{
|
|
456 |
static char Buffer[2048];
|
|
457 |
char* pt;
|
|
458 |
|
|
459 |
strncpy(Buffer, txt, 2047);
|
|
460 |
Buffer[2047] = 0;
|
|
461 |
for (pt = Buffer; *pt; pt++)
|
|
462 |
if (*pt == '\n' || *pt == '\r') *pt = ' ';
|
|
463 |
|
|
464 |
return Buffer;
|
|
465 |
|
|
466 |
}
|
|
467 |
|
|
468 |
static
|
|
469 |
void EmitHeader(LPMEMSTREAM m, const char* Title, cmsHPROFILE hProfile)
|
|
470 |
{
|
|
471 |
|
|
472 |
time_t timer;
|
|
473 |
|
|
474 |
time(&timer);
|
|
475 |
|
|
476 |
Writef(m, "%%!PS-Adobe-3.0\n");
|
|
477 |
Writef(m, "%%\n");
|
|
478 |
Writef(m, "%% %s\n", Title);
|
|
479 |
Writef(m, "%% Source: %s\n", RemoveCR(cmsTakeProductName(hProfile)));
|
|
480 |
Writef(m, "%% Description: %s\n", RemoveCR(cmsTakeProductDesc(hProfile)));
|
|
481 |
Writef(m, "%% Created: %s", ctime(&timer)); // ctime appends a \n!!!
|
|
482 |
Writef(m, "%%\n");
|
|
483 |
Writef(m, "%%%%BeginResource\n");
|
|
484 |
|
|
485 |
}
|
|
486 |
|
|
487 |
|
|
488 |
// Emits White & Black point. White point is always D50, Black point is the device
|
|
489 |
// Black point adapted to D50.
|
|
490 |
|
|
491 |
static
|
|
492 |
void EmitWhiteBlackD50(LPMEMSTREAM m, LPcmsCIEXYZ BlackPoint)
|
|
493 |
{
|
|
494 |
|
|
495 |
Writef(m, "/BlackPoint [%f %f %f]\n", BlackPoint -> X,
|
|
496 |
BlackPoint -> Y,
|
|
497 |
BlackPoint -> Z);
|
|
498 |
|
|
499 |
Writef(m, "/WhitePoint [%f %f %f]\n", cmsD50_XYZ()->X,
|
|
500 |
cmsD50_XYZ()->Y,
|
|
501 |
cmsD50_XYZ()->Z);
|
|
502 |
}
|
|
503 |
|
|
504 |
|
|
505 |
static
|
|
506 |
void EmitRangeCheck(LPMEMSTREAM m)
|
|
507 |
{
|
|
508 |
Writef(m, "dup 0.0 lt { pop 0.0 } if "
|
|
509 |
"dup 1.0 gt { pop 1.0 } if ");
|
|
510 |
|
|
511 |
}
|
|
512 |
|
|
513 |
// Does write the intent
|
|
514 |
|
|
515 |
static
|
|
516 |
void EmitIntent(LPMEMSTREAM m, int RenderingIntent)
|
|
517 |
{
|
|
518 |
const char *intent;
|
|
519 |
|
|
520 |
switch (RenderingIntent) {
|
|
521 |
|
|
522 |
case INTENT_PERCEPTUAL: intent = "Perceptual"; break;
|
|
523 |
case INTENT_RELATIVE_COLORIMETRIC: intent = "RelativeColorimetric"; break;
|
|
524 |
case INTENT_ABSOLUTE_COLORIMETRIC: intent = "AbsoluteColorimetric"; break;
|
|
525 |
case INTENT_SATURATION: intent = "Saturation"; break;
|
|
526 |
|
|
527 |
default: intent = "Undefined"; break;
|
|
528 |
}
|
|
529 |
|
|
530 |
Writef(m, "/RenderingIntent (%s)\n", intent );
|
|
531 |
}
|
|
532 |
|
|
533 |
//
|
|
534 |
// Convert L* to Y
|
|
535 |
//
|
|
536 |
// Y = Yn*[ (L* + 16) / 116] ^ 3 if (L*) >= 6 / 29
|
|
537 |
// = Yn*( L* / 116) / 7.787 if (L*) < 6 / 29
|
|
538 |
//
|
|
539 |
|
|
540 |
/*
|
|
541 |
static
|
|
542 |
void EmitL2Y(LPMEMSTREAM m)
|
|
543 |
{
|
|
544 |
Writef(m,
|
|
545 |
"{ "
|
|
546 |
"100 mul 16 add 116 div " // (L * 100 + 16) / 116
|
|
547 |
"dup 6 29 div ge " // >= 6 / 29 ?
|
|
548 |
"{ dup dup mul mul } " // yes, ^3 and done
|
|
549 |
"{ 4 29 div sub 108 841 div mul } " // no, slope limiting
|
|
550 |
"ifelse } bind ");
|
|
551 |
}
|
|
552 |
*/
|
|
553 |
|
|
554 |
|
|
555 |
// Lab -> XYZ, see the discussion above
|
|
556 |
|
|
557 |
static
|
|
558 |
void EmitLab2XYZ(LPMEMSTREAM m)
|
|
559 |
{
|
|
560 |
Writef(m, "/RangeABC [ 0 1 0 1 0 1]\n");
|
|
561 |
Writef(m, "/DecodeABC [\n");
|
|
562 |
Writef(m, "{100 mul 16 add 116 div } bind\n");
|
|
563 |
Writef(m, "{255 mul 128 sub 500 div } bind\n");
|
|
564 |
Writef(m, "{255 mul 128 sub 200 div } bind\n");
|
|
565 |
Writef(m, "]\n");
|
|
566 |
Writef(m, "/MatrixABC [ 1 1 1 1 0 0 0 0 -1]\n");
|
2394
|
567 |
Writef(m, "/RangeLMN [ -0.236 1.254 0 1 -0.635 1.640 ]\n");
|
2
|
568 |
Writef(m, "/DecodeLMN [\n");
|
|
569 |
Writef(m, "{dup 6 29 div ge {dup dup mul mul} {4 29 div sub 108 841 div mul} ifelse 0.964200 mul} bind\n");
|
|
570 |
Writef(m, "{dup 6 29 div ge {dup dup mul mul} {4 29 div sub 108 841 div mul} ifelse } bind\n");
|
|
571 |
Writef(m, "{dup 6 29 div ge {dup dup mul mul} {4 29 div sub 108 841 div mul} ifelse 0.824900 mul} bind\n");
|
|
572 |
Writef(m, "]\n");
|
|
573 |
}
|
|
574 |
|
|
575 |
|
|
576 |
|
|
577 |
// Outputs a table of words. It does use 16 bits
|
|
578 |
|
|
579 |
static
|
|
580 |
void Emit1Gamma(LPMEMSTREAM m, LPWORD Table, int nEntries)
|
|
581 |
{
|
|
582 |
int i;
|
|
583 |
double gamma;
|
|
584 |
|
|
585 |
|
|
586 |
if (nEntries <= 0) return; // Empty table
|
|
587 |
|
|
588 |
// Suppress whole if identity
|
2394
|
589 |
if (cmsIsLinear(Table, nEntries)) {
|
|
590 |
Writef(m, "{} ");
|
|
591 |
return;
|
|
592 |
}
|
|
593 |
|
2
|
594 |
|
|
595 |
// Check if is really an exponential. If so, emit "exp"
|
|
596 |
gamma = cmsEstimateGammaEx(Table, nEntries, 0.001);
|
|
597 |
if (gamma > 0) {
|
|
598 |
Writef(m, "{ %g exp } bind ", gamma);
|
|
599 |
return;
|
|
600 |
}
|
|
601 |
|
|
602 |
Writef(m, "{ ");
|
|
603 |
|
|
604 |
// Bounds check
|
|
605 |
EmitRangeCheck(m);
|
|
606 |
|
|
607 |
// Emit intepolation code
|
|
608 |
|
|
609 |
// PostScript code Stack
|
|
610 |
// =============== ========================
|
|
611 |
// v
|
|
612 |
Writef(m, " [");
|
|
613 |
|
|
614 |
// TODO: Check for endianess!!!
|
|
615 |
|
|
616 |
for (i=0; i < nEntries; i++) {
|
|
617 |
Writef(m, "%d ", Table[i]);
|
|
618 |
}
|
|
619 |
|
|
620 |
Writef(m, "] "); // v tab
|
|
621 |
|
|
622 |
Writef(m, "dup "); // v tab tab
|
|
623 |
Writef(m, "length 1 sub "); // v tab dom
|
|
624 |
Writef(m, "3 -1 roll "); // tab dom v
|
|
625 |
Writef(m, "mul "); // tab val2
|
|
626 |
Writef(m, "dup "); // tab val2 val2
|
|
627 |
Writef(m, "dup "); // tab val2 val2 val2
|
|
628 |
Writef(m, "floor cvi "); // tab val2 val2 cell0
|
|
629 |
Writef(m, "exch "); // tab val2 cell0 val2
|
|
630 |
Writef(m, "ceiling cvi "); // tab val2 cell0 cell1
|
|
631 |
Writef(m, "3 index "); // tab val2 cell0 cell1 tab
|
|
632 |
Writef(m, "exch "); // tab val2 cell0 tab cell1
|
|
633 |
Writef(m, "get "); // tab val2 cell0 y1
|
|
634 |
Writef(m, "4 -1 roll "); // val2 cell0 y1 tab
|
|
635 |
Writef(m, "3 -1 roll "); // val2 y1 tab cell0
|
|
636 |
Writef(m, "get "); // val2 y1 y0
|
|
637 |
Writef(m, "dup "); // val2 y1 y0 y0
|
|
638 |
Writef(m, "3 1 roll "); // val2 y0 y1 y0
|
|
639 |
Writef(m, "sub "); // val2 y0 (y1-y0)
|
|
640 |
Writef(m, "3 -1 roll "); // y0 (y1-y0) val2
|
|
641 |
Writef(m, "dup "); // y0 (y1-y0) val2 val2
|
|
642 |
Writef(m, "floor cvi "); // y0 (y1-y0) val2 floor(val2)
|
|
643 |
Writef(m, "sub "); // y0 (y1-y0) rest
|
|
644 |
Writef(m, "mul "); // y0 t1
|
|
645 |
Writef(m, "add "); // y
|
|
646 |
Writef(m, "65535 div "); // result
|
|
647 |
|
|
648 |
Writef(m, " } bind ");
|
|
649 |
}
|
|
650 |
|
|
651 |
|
|
652 |
// Compare gamma table
|
|
653 |
|
|
654 |
static
|
2394
|
655 |
LCMSBOOL GammaTableEquals(LPWORD g1, LPWORD g2, int nEntries)
|
2
|
656 |
{
|
|
657 |
return memcmp(g1, g2, nEntries* sizeof(WORD)) == 0;
|
|
658 |
}
|
|
659 |
|
|
660 |
|
|
661 |
// Does write a set of gamma curves
|
|
662 |
|
|
663 |
static
|
|
664 |
void EmitNGamma(LPMEMSTREAM m, int n, LPWORD g[], int nEntries)
|
|
665 |
{
|
|
666 |
int i;
|
|
667 |
|
|
668 |
for( i=0; i < n; i++ )
|
|
669 |
{
|
|
670 |
if (i > 0 && GammaTableEquals(g[i-1], g[i], nEntries)) {
|
|
671 |
|
|
672 |
Writef(m, "dup ");
|
|
673 |
}
|
|
674 |
else {
|
|
675 |
Emit1Gamma(m, g[i], nEntries);
|
|
676 |
}
|
|
677 |
}
|
|
678 |
|
|
679 |
}
|
|
680 |
|
|
681 |
|
|
682 |
// Check whatever a profile has CLUT tables (only on input)
|
|
683 |
|
|
684 |
static
|
2394
|
685 |
LCMSBOOL IsLUTbased(cmsHPROFILE hProfile, int Intent)
|
2
|
686 |
{
|
|
687 |
icTagSignature Tag;
|
|
688 |
|
|
689 |
// Check if adequate tag is present
|
|
690 |
Tag = Device2PCSTab[Intent];
|
|
691 |
|
|
692 |
if (cmsIsTag(hProfile, Tag)) return 1;
|
|
693 |
|
|
694 |
// If not present, revert to default (perceptual)
|
|
695 |
Tag = icSigAToB0Tag;
|
|
696 |
|
|
697 |
// If no tag present, try matrix-shaper
|
|
698 |
return cmsIsTag(hProfile, Tag);
|
|
699 |
}
|
|
700 |
|
|
701 |
|
|
702 |
|
|
703 |
// Following code dumps a LUT onto memory stream
|
|
704 |
|
|
705 |
|
|
706 |
// This is the sampler. Intended to work in SAMPLER_INSPECT mode,
|
|
707 |
// that is, the callback will be called for each knot with
|
|
708 |
//
|
|
709 |
// In[] The grid location coordinates, normalized to 0..ffff
|
|
710 |
// Out[] The LUT values, normalized to 0..ffff
|
|
711 |
//
|
|
712 |
// Returning a value other than 0 does terminate the sampling process
|
|
713 |
//
|
|
714 |
// Each row contains LUT values for all but first component. So, I
|
|
715 |
// detect row changing by keeping a copy of last value of first
|
|
716 |
// component. -1 is used to mark begining of whole block.
|
|
717 |
|
|
718 |
static
|
|
719 |
int OutputValueSampler(register WORD In[], register WORD Out[], register LPVOID Cargo)
|
|
720 |
{
|
|
721 |
LPSAMPLERCARGO sc = (LPSAMPLERCARGO) Cargo;
|
|
722 |
unsigned int i;
|
|
723 |
|
|
724 |
|
|
725 |
if (sc -> FixWhite) {
|
|
726 |
|
2394
|
727 |
if (In[0] == 0xFFFF) { // Only in L* = 100, ab = [-8..8]
|
2
|
728 |
|
2394
|
729 |
if ((In[1] >= 0x7800 && In[1] <= 0x8800) &&
|
|
730 |
(In[2] >= 0x7800 && In[2] <= 0x8800)) {
|
2
|
731 |
|
|
732 |
WORD* Black;
|
|
733 |
WORD* White;
|
|
734 |
int nOutputs;
|
|
735 |
|
|
736 |
if (!_cmsEndPointsBySpace(sc ->ColorSpace, &White, &Black, &nOutputs))
|
|
737 |
return 0;
|
|
738 |
|
|
739 |
for (i=0; i < (unsigned int) nOutputs; i++)
|
|
740 |
Out[i] = White[i];
|
|
741 |
}
|
|
742 |
|
|
743 |
|
|
744 |
}
|
|
745 |
}
|
|
746 |
|
|
747 |
|
|
748 |
// Hadle the parenthesis on rows
|
|
749 |
|
|
750 |
if (In[0] != sc ->FirstComponent) {
|
|
751 |
|
|
752 |
if (sc ->FirstComponent != -1) {
|
|
753 |
|
|
754 |
Writef(sc ->m, sc ->PostMin);
|
|
755 |
sc ->SecondComponent = -1;
|
|
756 |
Writef(sc ->m, sc ->PostMaj);
|
|
757 |
}
|
|
758 |
|
|
759 |
// Begin block
|
|
760 |
sc->m->Col = 0;
|
|
761 |
|
|
762 |
Writef(sc ->m, sc ->PreMaj);
|
|
763 |
sc ->FirstComponent = In[0];
|
|
764 |
}
|
|
765 |
|
|
766 |
|
|
767 |
if (In[1] != sc ->SecondComponent) {
|
|
768 |
|
|
769 |
if (sc ->SecondComponent != -1) {
|
|
770 |
|
|
771 |
Writef(sc ->m, sc ->PostMin);
|
|
772 |
}
|
|
773 |
|
|
774 |
Writef(sc ->m, sc ->PreMin);
|
|
775 |
sc ->SecondComponent = In[1];
|
|
776 |
}
|
|
777 |
|
|
778 |
|
|
779 |
|
|
780 |
// Dump table. Could be Word or byte based on
|
|
781 |
// depending on bps member (16 bps mode is not currently
|
|
782 |
// being used at all, but is here for future ampliations)
|
|
783 |
|
|
784 |
for (i=0; i < sc -> Lut ->OutputChan; i++) {
|
|
785 |
|
|
786 |
WORD wWordOut = Out[i];
|
|
787 |
|
|
788 |
if (sc ->bps == 8) {
|
|
789 |
|
|
790 |
// Value as byte
|
|
791 |
BYTE wByteOut;
|
|
792 |
|
|
793 |
// If is input, convert from Lab2 to Lab4 (just divide by 256)
|
|
794 |
|
|
795 |
if (sc ->lIsInput) {
|
|
796 |
|
|
797 |
|
|
798 |
wByteOut = L2Byte(wWordOut);
|
|
799 |
}
|
|
800 |
else
|
|
801 |
wByteOut = Word2Byte(wWordOut);
|
|
802 |
|
|
803 |
WriteByte(sc -> m, wByteOut);
|
|
804 |
}
|
|
805 |
else {
|
|
806 |
|
|
807 |
// Value as word
|
|
808 |
WriteByte(sc -> m, (BYTE) (wWordOut & 0xFF));
|
|
809 |
WriteByte(sc -> m, (BYTE) ((wWordOut >> 8) & 0xFF));
|
|
810 |
}
|
|
811 |
}
|
|
812 |
|
|
813 |
return 1;
|
|
814 |
}
|
|
815 |
|
|
816 |
// Writes a LUT on memstream. Could be 8 or 16 bits based
|
|
817 |
|
|
818 |
static
|
|
819 |
void WriteCLUT(LPMEMSTREAM m, LPLUT Lut, int bps, const char* PreMaj,
|
|
820 |
const char* PostMaj,
|
|
821 |
const char* PreMin,
|
|
822 |
const char* PostMin,
|
|
823 |
int lIsInput,
|
|
824 |
int FixWhite,
|
|
825 |
icColorSpaceSignature ColorSpace)
|
|
826 |
{
|
|
827 |
unsigned int i;
|
|
828 |
SAMPLERCARGO sc;
|
|
829 |
|
|
830 |
sc.FirstComponent = -1;
|
|
831 |
sc.SecondComponent = -1;
|
|
832 |
sc.Lut = Lut;
|
|
833 |
sc.m = m;
|
|
834 |
sc.bps = bps;
|
|
835 |
sc.PreMaj = PreMaj;
|
|
836 |
sc.PostMaj= PostMaj;
|
|
837 |
|
2394
|
838 |
sc.PreMin = PreMin;
|
|
839 |
sc.PostMin = PostMin;
|
2
|
840 |
sc.lIsInput = lIsInput;
|
|
841 |
sc.FixWhite = FixWhite;
|
|
842 |
sc.ColorSpace = ColorSpace;
|
|
843 |
|
|
844 |
Writef(m, "[");
|
|
845 |
|
|
846 |
for (i=0; i < Lut ->InputChan; i++)
|
|
847 |
Writef(m, " %d ", Lut ->cLutPoints);
|
|
848 |
|
|
849 |
Writef(m, " [\n");
|
|
850 |
|
|
851 |
|
|
852 |
|
|
853 |
cmsSample3DGrid(Lut, OutputValueSampler, (LPVOID) &sc, SAMPLER_INSPECT);
|
|
854 |
|
|
855 |
|
|
856 |
Writef(m, PostMin);
|
|
857 |
Writef(m, PostMaj);
|
|
858 |
Writef(m, "] ");
|
|
859 |
|
|
860 |
|
|
861 |
|
|
862 |
}
|
|
863 |
|
|
864 |
|
|
865 |
// Dumps CIEBasedA Color Space Array
|
|
866 |
|
|
867 |
static
|
|
868 |
int EmitCIEBasedA(LPMEMSTREAM m, LPWORD Tab, int nEntries, LPcmsCIEXYZ BlackPoint)
|
|
869 |
{
|
|
870 |
|
|
871 |
Writef(m, "[ /CIEBasedA\n");
|
|
872 |
Writef(m, " <<\n");
|
|
873 |
|
|
874 |
Writef(m, "/DecodeA ");
|
|
875 |
|
|
876 |
Emit1Gamma(m,Tab, nEntries);
|
|
877 |
|
|
878 |
Writef(m, " \n");
|
|
879 |
|
|
880 |
Writef(m, "/MatrixA [ 0.9642 1.0000 0.8249 ]\n");
|
|
881 |
Writef(m, "/RangeLMN [ 0.0 0.9642 0.0 1.0000 0.0 0.8249 ]\n");
|
|
882 |
|
|
883 |
EmitWhiteBlackD50(m, BlackPoint);
|
|
884 |
EmitIntent(m, INTENT_PERCEPTUAL);
|
|
885 |
|
|
886 |
Writef(m, ">>\n");
|
|
887 |
Writef(m, "]\n");
|
|
888 |
|
|
889 |
return 1;
|
|
890 |
}
|
|
891 |
|
|
892 |
|
|
893 |
// Dumps CIEBasedABC Color Space Array
|
|
894 |
|
|
895 |
static
|
|
896 |
int EmitCIEBasedABC(LPMEMSTREAM m, LPWORD L[], int nEntries, LPWMAT3 Matrix, LPcmsCIEXYZ BlackPoint)
|
|
897 |
{
|
|
898 |
int i;
|
|
899 |
|
|
900 |
Writef(m, "[ /CIEBasedABC\n");
|
|
901 |
Writef(m, "<<\n");
|
|
902 |
Writef(m, "/DecodeABC [ ");
|
|
903 |
|
|
904 |
EmitNGamma(m, 3, L, nEntries);
|
|
905 |
|
|
906 |
Writef(m, "]\n");
|
|
907 |
|
|
908 |
Writef(m, "/MatrixABC [ " );
|
|
909 |
|
|
910 |
for( i=0; i < 3; i++ ) {
|
|
911 |
|
|
912 |
Writef(m, "%.6f %.6f %.6f ",
|
|
913 |
FIXED_TO_DOUBLE(Matrix->v[0].n[i]),
|
|
914 |
FIXED_TO_DOUBLE(Matrix->v[1].n[i]),
|
|
915 |
FIXED_TO_DOUBLE(Matrix->v[2].n[i]));
|
|
916 |
}
|
|
917 |
|
|
918 |
|
|
919 |
Writef(m, "]\n");
|
|
920 |
|
|
921 |
Writef(m, "/RangeLMN [ 0.0 0.9642 0.0 1.0000 0.0 0.8249 ]\n");
|
|
922 |
|
|
923 |
EmitWhiteBlackD50(m, BlackPoint);
|
|
924 |
EmitIntent(m, INTENT_PERCEPTUAL);
|
|
925 |
|
|
926 |
Writef(m, ">>\n");
|
|
927 |
Writef(m, "]\n");
|
|
928 |
|
|
929 |
|
|
930 |
return 1;
|
|
931 |
}
|
|
932 |
|
|
933 |
|
|
934 |
static
|
|
935 |
int EmitCIEBasedDEF(LPMEMSTREAM m, LPLUT Lut, int Intent, LPcmsCIEXYZ BlackPoint)
|
|
936 |
{
|
|
937 |
const char* PreMaj;
|
|
938 |
const char* PostMaj;
|
|
939 |
const char* PreMin, *PostMin;
|
|
940 |
|
|
941 |
switch (Lut ->InputChan) {
|
|
942 |
case 3:
|
|
943 |
|
|
944 |
Writef(m, "[ /CIEBasedDEF\n");
|
|
945 |
PreMaj ="<";
|
|
946 |
PostMaj= ">\n";
|
|
947 |
PreMin = PostMin = "";
|
|
948 |
break;
|
|
949 |
case 4:
|
|
950 |
Writef(m, "[ /CIEBasedDEFG\n");
|
|
951 |
PreMaj = "[";
|
|
952 |
PostMaj = "]\n";
|
|
953 |
PreMin = "<";
|
|
954 |
PostMin = ">\n";
|
|
955 |
break;
|
|
956 |
default:
|
|
957 |
return 0;
|
|
958 |
|
|
959 |
}
|
|
960 |
|
|
961 |
Writef(m, "<<\n");
|
|
962 |
|
|
963 |
if (Lut ->wFlags & LUT_HASTL1) {
|
|
964 |
|
|
965 |
Writef(m, "/DecodeDEF [ ");
|
|
966 |
EmitNGamma(m, Lut ->InputChan, Lut ->L1, Lut ->CLut16params.nSamples);
|
|
967 |
Writef(m, "]\n");
|
|
968 |
}
|
|
969 |
|
|
970 |
|
|
971 |
|
|
972 |
if (Lut ->wFlags & LUT_HAS3DGRID) {
|
|
973 |
|
|
974 |
Writef(m, "/Table ");
|
|
975 |
WriteCLUT(m, Lut, 8, PreMaj, PostMaj, PreMin, PostMin, TRUE, FALSE, (icColorSpaceSignature) 0);
|
|
976 |
Writef(m, "]\n");
|
|
977 |
}
|
|
978 |
|
|
979 |
EmitLab2XYZ(m);
|
|
980 |
EmitWhiteBlackD50(m, BlackPoint);
|
|
981 |
EmitIntent(m, Intent);
|
|
982 |
|
|
983 |
Writef(m, " >>\n");
|
|
984 |
Writef(m, "]\n");
|
|
985 |
|
|
986 |
|
|
987 |
return 1;
|
|
988 |
}
|
|
989 |
|
|
990 |
// Generates a curve from a gray profile
|
|
991 |
|
|
992 |
static
|
|
993 |
LPGAMMATABLE ExtractGray2Y(cmsHPROFILE hProfile, int Intent)
|
|
994 |
{
|
|
995 |
LPGAMMATABLE Out = cmsAllocGamma(256);
|
|
996 |
cmsHPROFILE hXYZ = cmsCreateXYZProfile();
|
|
997 |
cmsHTRANSFORM xform = cmsCreateTransform(hProfile, TYPE_GRAY_8, hXYZ, TYPE_XYZ_DBL, Intent, cmsFLAGS_NOTPRECALC);
|
|
998 |
int i;
|
|
999 |
|
|
1000 |
for (i=0; i < 256; i++) {
|
|
1001 |
|
|
1002 |
BYTE Gray = (BYTE) i;
|
|
1003 |
cmsCIEXYZ XYZ;
|
|
1004 |
|
|
1005 |
cmsDoTransform(xform, &Gray, &XYZ, 1);
|
|
1006 |
|
|
1007 |
Out ->GammaTable[i] =_cmsClampWord((int) floor(XYZ.Y * 65535.0 + 0.5));
|
|
1008 |
}
|
|
1009 |
|
|
1010 |
cmsDeleteTransform(xform);
|
|
1011 |
cmsCloseProfile(hXYZ);
|
|
1012 |
return Out;
|
|
1013 |
}
|
|
1014 |
|
|
1015 |
|
|
1016 |
|
|
1017 |
// Because PostScrip has only 8 bits in /Table, we should use
|
|
1018 |
// a more perceptually uniform space... I do choose Lab.
|
|
1019 |
|
|
1020 |
static
|
|
1021 |
int WriteInputLUT(LPMEMSTREAM m, cmsHPROFILE hProfile, int Intent)
|
|
1022 |
{
|
|
1023 |
cmsHPROFILE hLab;
|
|
1024 |
cmsHTRANSFORM xform;
|
|
1025 |
icColorSpaceSignature ColorSpace;
|
|
1026 |
int nChannels;
|
|
1027 |
DWORD InputFormat;
|
|
1028 |
int rc;
|
|
1029 |
cmsHPROFILE Profiles[2];
|
|
1030 |
cmsCIEXYZ BlackPointAdaptedToD50;
|
|
1031 |
|
|
1032 |
// Does create a device-link based transform.
|
|
1033 |
// The DeviceLink is next dumped as working CSA.
|
|
1034 |
|
|
1035 |
hLab = cmsCreateLabProfile(NULL);
|
|
1036 |
ColorSpace = cmsGetColorSpace(hProfile);
|
|
1037 |
nChannels = _cmsChannelsOf(ColorSpace);
|
|
1038 |
InputFormat = CHANNELS_SH(nChannels) | BYTES_SH(2);
|
|
1039 |
|
|
1040 |
cmsDetectBlackPoint(&BlackPointAdaptedToD50, hProfile, Intent,LCMS_BPFLAGS_D50_ADAPTED);
|
|
1041 |
|
|
1042 |
// Is a devicelink profile?
|
|
1043 |
if (cmsGetDeviceClass(hProfile) == icSigLinkClass) {
|
|
1044 |
|
|
1045 |
// if devicelink output already Lab, use it directly
|
|
1046 |
|
|
1047 |
if (cmsGetPCS(hProfile) == icSigLabData) {
|
|
1048 |
|
|
1049 |
xform = cmsCreateTransform(hProfile, InputFormat, NULL,
|
|
1050 |
TYPE_Lab_DBL, Intent, 0);
|
|
1051 |
}
|
|
1052 |
else {
|
|
1053 |
|
|
1054 |
// Nope, adjust output to Lab if possible
|
|
1055 |
|
|
1056 |
Profiles[0] = hProfile;
|
|
1057 |
Profiles[1] = hLab;
|
|
1058 |
|
|
1059 |
xform = cmsCreateMultiprofileTransform(Profiles, 2, InputFormat,
|
|
1060 |
TYPE_Lab_DBL, Intent, 0);
|
|
1061 |
}
|
|
1062 |
|
|
1063 |
|
|
1064 |
}
|
|
1065 |
else {
|
|
1066 |
|
|
1067 |
// This is a normal profile
|
|
1068 |
xform = cmsCreateTransform(hProfile, InputFormat, hLab,
|
|
1069 |
TYPE_Lab_DBL, Intent, 0);
|
|
1070 |
}
|
|
1071 |
|
|
1072 |
|
|
1073 |
|
|
1074 |
if (xform == NULL) {
|
|
1075 |
|
|
1076 |
cmsSignalError(LCMS_ERRC_ABORTED, "Cannot create transform Profile -> Lab");
|
|
1077 |
return 0;
|
|
1078 |
}
|
|
1079 |
|
|
1080 |
// Only 1, 3 and 4 channels are allowed
|
|
1081 |
|
|
1082 |
switch (nChannels) {
|
|
1083 |
|
|
1084 |
case 1: {
|
|
1085 |
LPGAMMATABLE Gray2Y = ExtractGray2Y(hProfile, Intent);
|
|
1086 |
EmitCIEBasedA(m, Gray2Y->GammaTable, Gray2Y ->nEntries, &BlackPointAdaptedToD50);
|
|
1087 |
cmsFreeGamma(Gray2Y);
|
|
1088 |
}
|
|
1089 |
break;
|
|
1090 |
|
|
1091 |
case 3:
|
|
1092 |
case 4: {
|
|
1093 |
LPLUT DeviceLink;
|
|
1094 |
_LPcmsTRANSFORM v = (_LPcmsTRANSFORM) xform;
|
|
1095 |
|
|
1096 |
if (v ->DeviceLink)
|
|
1097 |
rc = EmitCIEBasedDEF(m, v->DeviceLink, Intent, &BlackPointAdaptedToD50);
|
|
1098 |
else {
|
|
1099 |
DeviceLink = _cmsPrecalculateDeviceLink(xform, 0);
|
|
1100 |
rc = EmitCIEBasedDEF(m, DeviceLink, Intent, &BlackPointAdaptedToD50);
|
|
1101 |
cmsFreeLUT(DeviceLink);
|
|
1102 |
}
|
|
1103 |
}
|
|
1104 |
break;
|
|
1105 |
|
|
1106 |
default:
|
|
1107 |
|
|
1108 |
cmsSignalError(LCMS_ERRC_ABORTED, "Only 3, 4 channels supported for CSA. This profile has %d channels.", nChannels);
|
|
1109 |
return 0;
|
|
1110 |
}
|
|
1111 |
|
|
1112 |
|
|
1113 |
cmsDeleteTransform(xform);
|
|
1114 |
cmsCloseProfile(hLab);
|
|
1115 |
return 1;
|
|
1116 |
}
|
|
1117 |
|
|
1118 |
|
|
1119 |
|
|
1120 |
// Does create CSA based on matrix-shaper. Allowed types are gray and RGB based
|
|
1121 |
|
|
1122 |
static
|
|
1123 |
int WriteInputMatrixShaper(LPMEMSTREAM m, cmsHPROFILE hProfile)
|
|
1124 |
{
|
|
1125 |
icColorSpaceSignature ColorSpace;
|
|
1126 |
LPMATSHAPER MatShaper;
|
|
1127 |
int rc;
|
|
1128 |
cmsCIEXYZ BlackPointAdaptedToD50;
|
|
1129 |
|
|
1130 |
|
|
1131 |
ColorSpace = cmsGetColorSpace(hProfile);
|
|
1132 |
MatShaper = cmsBuildInputMatrixShaper(hProfile);
|
|
1133 |
|
|
1134 |
cmsDetectBlackPoint(&BlackPointAdaptedToD50, hProfile, INTENT_RELATIVE_COLORIMETRIC, LCMS_BPFLAGS_D50_ADAPTED);
|
|
1135 |
|
|
1136 |
if (MatShaper == NULL) {
|
|
1137 |
|
|
1138 |
cmsSignalError(LCMS_ERRC_ABORTED, "This profile is not suitable for input");
|
|
1139 |
return 0;
|
|
1140 |
}
|
|
1141 |
|
|
1142 |
if (ColorSpace == icSigGrayData) {
|
|
1143 |
|
|
1144 |
rc = EmitCIEBasedA(m, MatShaper ->L[0],
|
|
1145 |
MatShaper ->p16.nSamples,
|
|
1146 |
&BlackPointAdaptedToD50);
|
|
1147 |
|
|
1148 |
}
|
|
1149 |
else
|
|
1150 |
if (ColorSpace == icSigRgbData) {
|
|
1151 |
|
|
1152 |
|
|
1153 |
rc = EmitCIEBasedABC(m, MatShaper->L,
|
|
1154 |
MatShaper ->p16.nSamples,
|
|
1155 |
&MatShaper ->Matrix,
|
|
1156 |
&BlackPointAdaptedToD50);
|
|
1157 |
}
|
|
1158 |
else {
|
|
1159 |
|
|
1160 |
cmsSignalError(LCMS_ERRC_ABORTED, "Profile is not suitable for CSA. Unsupported colorspace.");
|
|
1161 |
return 0;
|
|
1162 |
}
|
|
1163 |
|
|
1164 |
cmsFreeMatShaper(MatShaper);
|
|
1165 |
return rc;
|
|
1166 |
}
|
|
1167 |
|
|
1168 |
|
|
1169 |
|
|
1170 |
// Creates a PostScript color list from a named profile data.
|
|
1171 |
// This is a HP extension, and it works in Lab instead of XYZ
|
|
1172 |
|
|
1173 |
static
|
|
1174 |
int WriteNamedColorCSA(LPMEMSTREAM m, cmsHPROFILE hNamedColor, int Intent)
|
|
1175 |
{
|
|
1176 |
cmsHTRANSFORM xform;
|
|
1177 |
cmsHPROFILE hLab;
|
|
1178 |
int i, nColors;
|
|
1179 |
char ColorName[32];
|
|
1180 |
|
|
1181 |
|
|
1182 |
hLab = cmsCreateLabProfile(NULL);
|
|
1183 |
xform = cmsCreateTransform(hNamedColor, TYPE_NAMED_COLOR_INDEX,
|
|
1184 |
hLab, TYPE_Lab_DBL, Intent, cmsFLAGS_NOTPRECALC);
|
|
1185 |
if (xform == NULL) return 0;
|
|
1186 |
|
|
1187 |
|
|
1188 |
Writef(m, "<<\n");
|
|
1189 |
Writef(m, "(colorlistcomment) (%s)\n", "Named color CSA");
|
|
1190 |
Writef(m, "(Prefix) [ (Pantone ) (PANTONE ) ]\n");
|
|
1191 |
Writef(m, "(Suffix) [ ( CV) ( CVC) ( C) ]\n");
|
|
1192 |
|
|
1193 |
nColors = cmsNamedColorCount(xform);
|
|
1194 |
|
|
1195 |
|
|
1196 |
for (i=0; i < nColors; i++) {
|
|
1197 |
|
|
1198 |
WORD In[1];
|
|
1199 |
cmsCIELab Lab;
|
|
1200 |
|
|
1201 |
In[0] = (WORD) i;
|
|
1202 |
|
|
1203 |
if (!cmsNamedColorInfo(xform, i, ColorName, NULL, NULL))
|
|
1204 |
continue;
|
|
1205 |
|
|
1206 |
cmsDoTransform(xform, In, &Lab, 1);
|
|
1207 |
Writef(m, " (%s) [ %.3f %.3f %.3f ]\n", ColorName, Lab.L, Lab.a, Lab.b);
|
|
1208 |
}
|
|
1209 |
|
|
1210 |
|
|
1211 |
|
|
1212 |
Writef(m, ">>\n");
|
|
1213 |
|
|
1214 |
cmsDeleteTransform(xform);
|
|
1215 |
cmsCloseProfile(hLab);
|
|
1216 |
return 1;
|
|
1217 |
}
|
|
1218 |
|
|
1219 |
|
|
1220 |
// Does create a Color Space Array on XYZ colorspace for PostScript usage
|
|
1221 |
|
|
1222 |
DWORD LCMSEXPORT cmsGetPostScriptCSA(cmsHPROFILE hProfile,
|
|
1223 |
int Intent,
|
|
1224 |
LPVOID Buffer, DWORD dwBufferLen)
|
|
1225 |
{
|
|
1226 |
|
|
1227 |
LPMEMSTREAM mem;
|
|
1228 |
DWORD dwBytesUsed;
|
|
1229 |
|
|
1230 |
// Set up the serialization engine
|
|
1231 |
mem = CreateMemStream((LPBYTE) Buffer, dwBufferLen, MAXPSCOLS);
|
|
1232 |
if (!mem) return 0;
|
|
1233 |
|
|
1234 |
|
|
1235 |
// Is a named color profile?
|
|
1236 |
if (cmsGetDeviceClass(hProfile) == icSigNamedColorClass) {
|
|
1237 |
|
|
1238 |
if (!WriteNamedColorCSA(mem, hProfile, Intent)) {
|
|
1239 |
|
2394
|
1240 |
_cmsFree((void*) mem);
|
2
|
1241 |
return 0;
|
|
1242 |
}
|
|
1243 |
}
|
|
1244 |
else {
|
|
1245 |
|
|
1246 |
|
|
1247 |
// Any profile class are allowed (including devicelink), but
|
|
1248 |
// output (PCS) colorspace must be XYZ or Lab
|
|
1249 |
icColorSpaceSignature ColorSpace = cmsGetPCS(hProfile);
|
|
1250 |
|
|
1251 |
if (ColorSpace != icSigXYZData &&
|
|
1252 |
ColorSpace != icSigLabData) {
|
|
1253 |
|
|
1254 |
cmsSignalError(LCMS_ERRC_ABORTED, "Invalid output color space");
|
2394
|
1255 |
_cmsFree((void*) mem);
|
2
|
1256 |
return 0;
|
|
1257 |
}
|
|
1258 |
|
|
1259 |
// Is there any CLUT?
|
|
1260 |
if (IsLUTbased(hProfile, Intent)) {
|
|
1261 |
|
|
1262 |
// Yes, so handle as LUT-based
|
|
1263 |
if (!WriteInputLUT(mem, hProfile, Intent)) {
|
|
1264 |
|
2394
|
1265 |
_cmsFree((void*) mem);
|
2
|
1266 |
return 0;
|
|
1267 |
}
|
|
1268 |
}
|
|
1269 |
else {
|
|
1270 |
|
|
1271 |
// No, try Matrix-shaper (this only works on XYZ)
|
|
1272 |
|
|
1273 |
if (!WriteInputMatrixShaper(mem, hProfile)) {
|
|
1274 |
|
2394
|
1275 |
_cmsFree((void*) mem); // Something went wrong
|
2
|
1276 |
return 0;
|
|
1277 |
}
|
|
1278 |
}
|
|
1279 |
}
|
|
1280 |
|
|
1281 |
|
|
1282 |
// Done, keep memory usage
|
|
1283 |
dwBytesUsed = mem ->dwUsed;
|
|
1284 |
|
|
1285 |
// Get rid of memory stream
|
2394
|
1286 |
_cmsFree((void*) mem);
|
2
|
1287 |
|
|
1288 |
// Finally, return used byte count
|
|
1289 |
return dwBytesUsed;
|
|
1290 |
}
|
|
1291 |
|
|
1292 |
// ------------------------------------------------------ Color Rendering Dictionary (CRD)
|
|
1293 |
|
|
1294 |
|
|
1295 |
|
|
1296 |
/*
|
|
1297 |
|
|
1298 |
Black point compensation plus chromatic adaptation:
|
|
1299 |
|
|
1300 |
Step 1 - Chromatic adaptation
|
|
1301 |
=============================
|
|
1302 |
|
|
1303 |
WPout
|
|
1304 |
X = ------- PQR
|
|
1305 |
Wpin
|
|
1306 |
|
|
1307 |
Step 2 - Black point compensation
|
|
1308 |
=================================
|
|
1309 |
|
|
1310 |
(WPout - BPout)*X - WPout*(BPin - BPout)
|
|
1311 |
out = ---------------------------------------
|
|
1312 |
WPout - BPin
|
|
1313 |
|
|
1314 |
|
|
1315 |
Algorithm discussion
|
|
1316 |
====================
|
|
1317 |
|
|
1318 |
TransformPQR(WPin, BPin, WPout, BPout, PQR)
|
|
1319 |
|
|
1320 |
Wpin,etc= { Xws Yws Zws Pws Qws Rws }
|
|
1321 |
|
|
1322 |
|
|
1323 |
Algorithm Stack 0...n
|
|
1324 |
===========================================================
|
|
1325 |
PQR BPout WPout BPin WPin
|
|
1326 |
4 index 3 get WPin PQR BPout WPout BPin WPin
|
|
1327 |
div (PQR/WPin) BPout WPout BPin WPin
|
|
1328 |
2 index 3 get WPout (PQR/WPin) BPout WPout BPin WPin
|
|
1329 |
mult WPout*(PQR/WPin) BPout WPout BPin WPin
|
|
1330 |
|
|
1331 |
2 index 3 get WPout WPout*(PQR/WPin) BPout WPout BPin WPin
|
|
1332 |
2 index 3 get BPout WPout WPout*(PQR/WPin) BPout WPout BPin WPin
|
|
1333 |
sub (WPout-BPout) WPout*(PQR/WPin) BPout WPout BPin WPin
|
|
1334 |
mult (WPout-BPout)* WPout*(PQR/WPin) BPout WPout BPin WPin
|
|
1335 |
|
|
1336 |
2 index 3 get WPout (BPout-WPout)* WPout*(PQR/WPin) BPout WPout BPin WPin
|
|
1337 |
4 index 3 get BPin WPout (BPout-WPout)* WPout*(PQR/WPin) BPout WPout BPin WPin
|
|
1338 |
3 index 3 get BPout BPin WPout (BPout-WPout)* WPout*(PQR/WPin) BPout WPout BPin WPin
|
|
1339 |
|
|
1340 |
sub (BPin-BPout) WPout (BPout-WPout)* WPout*(PQR/WPin) BPout WPout BPin WPin
|
|
1341 |
mult (BPin-BPout)*WPout (BPout-WPout)* WPout*(PQR/WPin) BPout WPout BPin WPin
|
|
1342 |
sub (BPout-WPout)* WPout*(PQR/WPin)-(BPin-BPout)*WPout BPout WPout BPin WPin
|
|
1343 |
|
|
1344 |
3 index 3 get BPin (BPout-WPout)* WPout*(PQR/WPin)-(BPin-BPout)*WPout BPout WPout BPin WPin
|
|
1345 |
3 index 3 get WPout BPin (BPout-WPout)* WPout*(PQR/WPin)-(BPin-BPout)*WPout BPout WPout BPin WPin
|
|
1346 |
exch
|
|
1347 |
sub (WPout-BPin) (BPout-WPout)* WPout*(PQR/WPin)-(BPin-BPout)*WPout BPout WPout BPin WPin
|
|
1348 |
div
|
|
1349 |
|
|
1350 |
exch pop
|
|
1351 |
exch pop
|
|
1352 |
exch pop
|
|
1353 |
exch pop
|
|
1354 |
|
|
1355 |
*/
|
|
1356 |
|
|
1357 |
|
|
1358 |
static
|
2394
|
1359 |
void EmitPQRStage(LPMEMSTREAM m, cmsHPROFILE hProfile, int DoBPC, int lIsAbsolute)
|
2
|
1360 |
{
|
|
1361 |
|
|
1362 |
|
2394
|
1363 |
if (lIsAbsolute) {
|
|
1364 |
|
|
1365 |
// For absolute colorimetric intent, encode back to relative
|
|
1366 |
// and generate a relative LUT
|
|
1367 |
|
|
1368 |
// Relative encoding is obtained across XYZpcs*(D50/WhitePoint)
|
|
1369 |
|
|
1370 |
cmsCIEXYZ White;
|
|
1371 |
|
|
1372 |
cmsTakeMediaWhitePoint(&White, hProfile);
|
|
1373 |
|
|
1374 |
Writef(m,"/MatrixPQR [1 0 0 0 1 0 0 0 1 ]\n");
|
|
1375 |
Writef(m,"/RangePQR [ -0.5 2 -0.5 2 -0.5 2 ]\n");
|
|
1376 |
|
|
1377 |
Writef(m, "%% Absolute colorimetric -- encode to relative to maximize LUT usage\n"
|
|
1378 |
"/TransformPQR [\n"
|
|
1379 |
"{0.9642 mul %g div exch pop exch pop exch pop exch pop} bind\n"
|
|
1380 |
"{1.0000 mul %g div exch pop exch pop exch pop exch pop} bind\n"
|
|
1381 |
"{0.8249 mul %g div exch pop exch pop exch pop exch pop} bind\n]\n",
|
|
1382 |
White.X, White.Y, White.Z);
|
|
1383 |
return;
|
|
1384 |
}
|
|
1385 |
|
|
1386 |
|
2
|
1387 |
Writef(m,"%% Bradford Cone Space\n"
|
|
1388 |
"/MatrixPQR [0.8951 -0.7502 0.0389 0.2664 1.7135 -0.0685 -0.1614 0.0367 1.0296 ] \n");
|
|
1389 |
|
|
1390 |
Writef(m, "/RangePQR [ -0.5 2 -0.5 2 -0.5 2 ]\n");
|
|
1391 |
|
|
1392 |
|
|
1393 |
// No BPC
|
|
1394 |
|
|
1395 |
if (!DoBPC) {
|
|
1396 |
|
|
1397 |
Writef(m, "%% VonKries-like transform in Bradford Cone Space\n"
|
|
1398 |
"/TransformPQR [\n"
|
|
1399 |
"{exch pop exch 3 get mul exch pop exch 3 get div} bind\n"
|
|
1400 |
"{exch pop exch 4 get mul exch pop exch 4 get div} bind\n"
|
|
1401 |
"{exch pop exch 5 get mul exch pop exch 5 get div} bind\n]\n");
|
|
1402 |
} else {
|
|
1403 |
|
|
1404 |
// BPC
|
|
1405 |
|
|
1406 |
Writef(m, "%% VonKries-like transform in Bradford Cone Space plus BPC\n"
|
|
1407 |
"/TransformPQR [\n");
|
|
1408 |
|
|
1409 |
Writef(m, "{4 index 3 get div 2 index 3 get mul "
|
|
1410 |
"2 index 3 get 2 index 3 get sub mul "
|
|
1411 |
"2 index 3 get 4 index 3 get 3 index 3 get sub mul sub "
|
|
1412 |
"3 index 3 get 3 index 3 get exch sub div "
|
|
1413 |
"exch pop exch pop exch pop exch pop } bind\n");
|
|
1414 |
|
|
1415 |
Writef(m, "{4 index 4 get div 2 index 4 get mul "
|
|
1416 |
"2 index 4 get 2 index 4 get sub mul "
|
|
1417 |
"2 index 4 get 4 index 4 get 3 index 4 get sub mul sub "
|
|
1418 |
"3 index 4 get 3 index 4 get exch sub div "
|
|
1419 |
"exch pop exch pop exch pop exch pop } bind\n");
|
|
1420 |
|
|
1421 |
Writef(m, "{4 index 5 get div 2 index 5 get mul "
|
|
1422 |
"2 index 5 get 2 index 5 get sub mul "
|
|
1423 |
"2 index 5 get 4 index 5 get 3 index 5 get sub mul sub "
|
|
1424 |
"3 index 5 get 3 index 5 get exch sub div "
|
|
1425 |
"exch pop exch pop exch pop exch pop } bind\n]\n");
|
|
1426 |
|
|
1427 |
}
|
|
1428 |
|
|
1429 |
|
|
1430 |
}
|
|
1431 |
|
|
1432 |
|
|
1433 |
static
|
|
1434 |
void EmitXYZ2Lab(LPMEMSTREAM m)
|
|
1435 |
{
|
2394
|
1436 |
Writef(m, "/RangeLMN [ -0.635 2.0 0 2 -0.635 2.0 ]\n");
|
2
|
1437 |
Writef(m, "/EncodeLMN [\n");
|
|
1438 |
Writef(m, "{ 0.964200 div dup 0.008856 le {7.787 mul 16 116 div add}{1 3 div exp} ifelse } bind\n");
|
|
1439 |
Writef(m, "{ 1.000000 div dup 0.008856 le {7.787 mul 16 116 div add}{1 3 div exp} ifelse } bind\n");
|
|
1440 |
Writef(m, "{ 0.824900 div dup 0.008856 le {7.787 mul 16 116 div add}{1 3 div exp} ifelse } bind\n");
|
|
1441 |
Writef(m, "]\n");
|
|
1442 |
Writef(m, "/MatrixABC [ 0 1 0 1 -1 1 0 0 -1 ]\n");
|
|
1443 |
Writef(m, "/EncodeABC [\n");
|
|
1444 |
|
|
1445 |
|
|
1446 |
Writef(m, "{ 116 mul 16 sub 100 div } bind\n");
|
2394
|
1447 |
Writef(m, "{ 500 mul 128 add 256 div } bind\n");
|
|
1448 |
Writef(m, "{ 200 mul 128 add 256 div } bind\n");
|
2
|
1449 |
|
|
1450 |
|
|
1451 |
Writef(m, "]\n");
|
|
1452 |
|
|
1453 |
|
|
1454 |
}
|
|
1455 |
|
|
1456 |
// Due to impedance mismatch between XYZ and almost all RGB and CMYK spaces
|
|
1457 |
// I choose to dump LUTS in Lab instead of XYZ. There is still a lot of wasted
|
|
1458 |
// space on 3D CLUT, but since space seems not to be a problem here, 33 points
|
|
1459 |
// would give a reasonable accurancy. Note also that CRD tables must operate in
|
|
1460 |
// 8 bits.
|
|
1461 |
|
|
1462 |
static
|
|
1463 |
int WriteOutputLUT(LPMEMSTREAM m, cmsHPROFILE hProfile, int Intent, DWORD dwFlags)
|
|
1464 |
{
|
|
1465 |
cmsHPROFILE hLab;
|
|
1466 |
cmsHTRANSFORM xform;
|
|
1467 |
icColorSpaceSignature ColorSpace;
|
|
1468 |
int i, nChannels;
|
|
1469 |
DWORD OutputFormat;
|
|
1470 |
_LPcmsTRANSFORM v;
|
|
1471 |
LPLUT DeviceLink;
|
|
1472 |
cmsHPROFILE Profiles[3];
|
|
1473 |
cmsCIEXYZ BlackPointAdaptedToD50;
|
2394
|
1474 |
LCMSBOOL lFreeDeviceLink = FALSE;
|
|
1475 |
LCMSBOOL lDoBPC = (dwFlags & cmsFLAGS_BLACKPOINTCOMPENSATION);
|
|
1476 |
LCMSBOOL lFixWhite = !(dwFlags & cmsFLAGS_NOWHITEONWHITEFIXUP);
|
|
1477 |
int RelativeEncodingIntent;
|
2
|
1478 |
|
|
1479 |
|
|
1480 |
|
2394
|
1481 |
hLab = cmsCreateLabProfile(NULL);
|
2
|
1482 |
|
|
1483 |
ColorSpace = cmsGetColorSpace(hProfile);
|
|
1484 |
nChannels = _cmsChannelsOf(ColorSpace);
|
|
1485 |
OutputFormat = CHANNELS_SH(nChannels) | BYTES_SH(2);
|
|
1486 |
|
2394
|
1487 |
// For absolute colorimetric, the LUT is encoded as relative
|
|
1488 |
// in order to preserve precission.
|
|
1489 |
|
|
1490 |
RelativeEncodingIntent = Intent;
|
|
1491 |
if (RelativeEncodingIntent == INTENT_ABSOLUTE_COLORIMETRIC)
|
|
1492 |
RelativeEncodingIntent = INTENT_RELATIVE_COLORIMETRIC;
|
|
1493 |
|
|
1494 |
|
2
|
1495 |
// Is a devicelink profile?
|
|
1496 |
if (cmsGetDeviceClass(hProfile) == icSigLinkClass) {
|
|
1497 |
|
|
1498 |
// if devicelink input already in Lab
|
|
1499 |
|
|
1500 |
if (ColorSpace == icSigLabData) {
|
|
1501 |
|
2394
|
1502 |
// adjust input to Lab to our v4
|
2
|
1503 |
|
|
1504 |
Profiles[0] = hLab;
|
|
1505 |
Profiles[1] = hProfile;
|
|
1506 |
|
|
1507 |
xform = cmsCreateMultiprofileTransform(Profiles, 2, TYPE_Lab_DBL,
|
2394
|
1508 |
OutputFormat, RelativeEncodingIntent,
|
|
1509 |
dwFlags|cmsFLAGS_NOWHITEONWHITEFIXUP|cmsFLAGS_NOPRELINEARIZATION);
|
2
|
1510 |
|
|
1511 |
}
|
|
1512 |
else {
|
|
1513 |
cmsSignalError(LCMS_ERRC_ABORTED, "Cannot use devicelink profile for CRD creation");
|
|
1514 |
return 0;
|
|
1515 |
}
|
|
1516 |
|
|
1517 |
|
|
1518 |
}
|
|
1519 |
else {
|
|
1520 |
|
|
1521 |
// This is a normal profile
|
|
1522 |
xform = cmsCreateTransform(hLab, TYPE_Lab_DBL, hProfile,
|
2394
|
1523 |
OutputFormat, RelativeEncodingIntent, dwFlags|cmsFLAGS_NOWHITEONWHITEFIXUP|cmsFLAGS_NOPRELINEARIZATION);
|
2
|
1524 |
}
|
|
1525 |
|
|
1526 |
if (xform == NULL) {
|
|
1527 |
|
|
1528 |
cmsSignalError(LCMS_ERRC_ABORTED, "Cannot create transform Lab -> Profile in CRD creation");
|
|
1529 |
return 0;
|
|
1530 |
}
|
|
1531 |
|
|
1532 |
// Get the internal precalculated devicelink
|
|
1533 |
|
|
1534 |
v = (_LPcmsTRANSFORM) xform;
|
|
1535 |
DeviceLink = v ->DeviceLink;
|
|
1536 |
|
|
1537 |
if (!DeviceLink) {
|
|
1538 |
|
2394
|
1539 |
DeviceLink = _cmsPrecalculateDeviceLink(xform, cmsFLAGS_NOPRELINEARIZATION);
|
2
|
1540 |
lFreeDeviceLink = TRUE;
|
|
1541 |
}
|
|
1542 |
|
|
1543 |
Writef(m, "<<\n");
|
|
1544 |
Writef(m, "/ColorRenderingType 1\n");
|
|
1545 |
|
|
1546 |
|
|
1547 |
cmsDetectBlackPoint(&BlackPointAdaptedToD50, hProfile, Intent, LCMS_BPFLAGS_D50_ADAPTED);
|
|
1548 |
|
|
1549 |
// Emit headers, etc.
|
|
1550 |
EmitWhiteBlackD50(m, &BlackPointAdaptedToD50);
|
2394
|
1551 |
EmitPQRStage(m, hProfile, lDoBPC, Intent == INTENT_ABSOLUTE_COLORIMETRIC);
|
2
|
1552 |
EmitXYZ2Lab(m);
|
|
1553 |
|
|
1554 |
if (DeviceLink ->wFlags & LUT_HASTL1) {
|
|
1555 |
|
|
1556 |
// Shouldn't happen
|
|
1557 |
cmsSignalError(LCMS_ERRC_ABORTED, "Internal error (prelinearization on CRD)");
|
|
1558 |
return 0;
|
|
1559 |
}
|
|
1560 |
|
|
1561 |
|
|
1562 |
// FIXUP: map Lab (100, 0, 0) to perfect white, because the particular encoding for Lab
|
|
1563 |
// does map a=b=0 not falling into any specific node. Since range a,b goes -128..127,
|
|
1564 |
// zero is slightly moved towards right, so assure next node (in L=100 slice) is mapped to
|
|
1565 |
// zero. This would sacrifice a bit of highlights, but failure to do so would cause
|
|
1566 |
// scum dot. Ouch.
|
|
1567 |
|
2394
|
1568 |
if (Intent == INTENT_ABSOLUTE_COLORIMETRIC)
|
|
1569 |
lFixWhite = FALSE;
|
|
1570 |
|
2
|
1571 |
Writef(m, "/RenderTable ");
|
|
1572 |
|
|
1573 |
WriteCLUT(m, DeviceLink, 8, "<", ">\n", "", "", FALSE,
|
2394
|
1574 |
lFixWhite, ColorSpace);
|
2
|
1575 |
|
|
1576 |
Writef(m, " %d {} bind ", nChannels);
|
|
1577 |
|
|
1578 |
for (i=1; i < nChannels; i++)
|
|
1579 |
Writef(m, "dup ");
|
|
1580 |
|
|
1581 |
Writef(m, "]\n");
|
|
1582 |
|
|
1583 |
|
|
1584 |
EmitIntent(m, Intent);
|
|
1585 |
|
|
1586 |
Writef(m, ">>\n");
|
|
1587 |
|
|
1588 |
if (!(dwFlags & cmsFLAGS_NODEFAULTRESOURCEDEF)) {
|
|
1589 |
|
|
1590 |
Writef(m, "/Current exch /ColorRendering defineresource pop\n");
|
|
1591 |
}
|
|
1592 |
|
|
1593 |
if (lFreeDeviceLink) cmsFreeLUT(DeviceLink);
|
|
1594 |
cmsDeleteTransform(xform);
|
|
1595 |
cmsCloseProfile(hLab);
|
|
1596 |
|
|
1597 |
return 1;
|
|
1598 |
}
|
|
1599 |
|
|
1600 |
|
|
1601 |
// Builds a ASCII string containing colorant list in 0..1.0 range
|
|
1602 |
static
|
|
1603 |
void BuildColorantList(char *Colorant, int nColorant, WORD Out[])
|
|
1604 |
{
|
|
1605 |
char Buff[32];
|
|
1606 |
int j;
|
|
1607 |
|
|
1608 |
Colorant[0] = 0;
|
2394
|
1609 |
if (nColorant > MAXCHANNELS)
|
|
1610 |
nColorant = MAXCHANNELS;
|
|
1611 |
|
2
|
1612 |
for (j=0; j < nColorant; j++) {
|
|
1613 |
|
|
1614 |
sprintf(Buff, "%.3f", Out[j] / 65535.0);
|
|
1615 |
strcat(Colorant, Buff);
|
|
1616 |
if (j < nColorant -1)
|
|
1617 |
strcat(Colorant, " ");
|
|
1618 |
|
|
1619 |
}
|
|
1620 |
}
|
|
1621 |
|
|
1622 |
|
|
1623 |
// Creates a PostScript color list from a named profile data.
|
|
1624 |
// This is a HP extension.
|
|
1625 |
|
|
1626 |
static
|
|
1627 |
int WriteNamedColorCRD(LPMEMSTREAM m, cmsHPROFILE hNamedColor, int Intent, DWORD dwFlags)
|
|
1628 |
{
|
|
1629 |
cmsHTRANSFORM xform;
|
|
1630 |
int i, nColors, nColorant;
|
|
1631 |
DWORD OutputFormat;
|
|
1632 |
char ColorName[32];
|
|
1633 |
char Colorant[128];
|
|
1634 |
|
|
1635 |
nColorant = _cmsChannelsOf(cmsGetColorSpace(hNamedColor));
|
|
1636 |
OutputFormat = CHANNELS_SH(nColorant) | BYTES_SH(2);
|
|
1637 |
|
|
1638 |
xform = cmsCreateTransform(hNamedColor, TYPE_NAMED_COLOR_INDEX,
|
|
1639 |
NULL, OutputFormat, Intent, cmsFLAGS_NOTPRECALC);
|
|
1640 |
if (xform == NULL) return 0;
|
|
1641 |
|
|
1642 |
|
|
1643 |
Writef(m, "<<\n");
|
|
1644 |
Writef(m, "(colorlistcomment) (%s) \n", "Named profile");
|
|
1645 |
Writef(m, "(Prefix) [ (Pantone ) (PANTONE ) ]\n");
|
|
1646 |
Writef(m, "(Suffix) [ ( CV) ( CVC) ( C) ]\n");
|
|
1647 |
|
|
1648 |
nColors = cmsNamedColorCount(xform);
|
|
1649 |
|
|
1650 |
|
|
1651 |
for (i=0; i < nColors; i++) {
|
|
1652 |
|
|
1653 |
WORD In[1];
|
|
1654 |
WORD Out[MAXCHANNELS];
|
|
1655 |
|
|
1656 |
In[0] = (WORD) i;
|
|
1657 |
|
|
1658 |
if (!cmsNamedColorInfo(xform, i, ColorName, NULL, NULL))
|
|
1659 |
continue;
|
|
1660 |
|
|
1661 |
cmsDoTransform(xform, In, Out, 1);
|
|
1662 |
BuildColorantList(Colorant, nColorant, Out);
|
|
1663 |
Writef(m, " (%s) [ %s ]\n", ColorName, Colorant);
|
|
1664 |
}
|
|
1665 |
|
|
1666 |
Writef(m, " >>");
|
|
1667 |
|
|
1668 |
if (!(dwFlags & cmsFLAGS_NODEFAULTRESOURCEDEF)) {
|
|
1669 |
|
|
1670 |
Writef(m, " /Current exch /HPSpotTable defineresource pop\n");
|
|
1671 |
}
|
|
1672 |
|
|
1673 |
cmsDeleteTransform(xform);
|
|
1674 |
return 1;
|
|
1675 |
}
|
|
1676 |
|
|
1677 |
|
|
1678 |
|
|
1679 |
// This one does create a Color Rendering Dictionary.
|
|
1680 |
// CRD are always LUT-Based, no matter if profile is
|
|
1681 |
// implemented as matrix-shaper.
|
|
1682 |
|
|
1683 |
DWORD LCMSEXPORT cmsGetPostScriptCRDEx(cmsHPROFILE hProfile,
|
|
1684 |
int Intent, DWORD dwFlags,
|
|
1685 |
LPVOID Buffer, DWORD dwBufferLen)
|
|
1686 |
{
|
|
1687 |
|
|
1688 |
LPMEMSTREAM mem;
|
|
1689 |
DWORD dwBytesUsed;
|
|
1690 |
|
|
1691 |
// Set up the serialization artifact
|
|
1692 |
mem = CreateMemStream((LPBYTE) Buffer, dwBufferLen, MAXPSCOLS);
|
|
1693 |
if (!mem) return 0;
|
|
1694 |
|
|
1695 |
|
|
1696 |
if (!(dwFlags & cmsFLAGS_NODEFAULTRESOURCEDEF)) {
|
|
1697 |
|
|
1698 |
EmitHeader(mem, "Color Rendering Dictionary (CRD)", hProfile);
|
|
1699 |
}
|
|
1700 |
|
|
1701 |
|
|
1702 |
// Is a named color profile?
|
|
1703 |
if (cmsGetDeviceClass(hProfile) == icSigNamedColorClass) {
|
|
1704 |
|
|
1705 |
if (!WriteNamedColorCRD(mem, hProfile, Intent, dwFlags)) {
|
|
1706 |
|
2394
|
1707 |
_cmsFree((void*) mem);
|
2
|
1708 |
return 0;
|
|
1709 |
}
|
|
1710 |
}
|
|
1711 |
else {
|
|
1712 |
|
|
1713 |
// CRD are always implemented as LUT.
|
|
1714 |
|
|
1715 |
|
|
1716 |
if (!WriteOutputLUT(mem, hProfile, Intent, dwFlags)) {
|
2394
|
1717 |
_cmsFree((void*) mem);
|
2
|
1718 |
return 0;
|
|
1719 |
}
|
|
1720 |
}
|
|
1721 |
|
|
1722 |
if (!(dwFlags & cmsFLAGS_NODEFAULTRESOURCEDEF)) {
|
|
1723 |
|
|
1724 |
Writef(mem, "%%%%EndResource\n");
|
|
1725 |
Writef(mem, "\n%% CRD End\n");
|
|
1726 |
}
|
|
1727 |
|
|
1728 |
// Done, keep memory usage
|
|
1729 |
dwBytesUsed = mem ->dwUsed;
|
|
1730 |
|
|
1731 |
// Get rid of memory stream
|
2394
|
1732 |
_cmsFree((void*) mem);
|
2
|
1733 |
|
|
1734 |
// Finally, return used byte count
|
|
1735 |
return dwBytesUsed;
|
|
1736 |
}
|
|
1737 |
|
|
1738 |
|
|
1739 |
// For compatibility with previous versions
|
|
1740 |
|
|
1741 |
DWORD LCMSEXPORT cmsGetPostScriptCRD(cmsHPROFILE hProfile,
|
|
1742 |
int Intent,
|
|
1743 |
LPVOID Buffer, DWORD dwBufferLen)
|
|
1744 |
{
|
|
1745 |
return cmsGetPostScriptCRDEx(hProfile, Intent, 0, Buffer, dwBufferLen);
|
|
1746 |
}
|