jdk

Форк
0
712 строк · 22.0 Кб
1
/*
2
 * Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
 *
5
 * This code is free software; you can redistribute it and/or modify it
6
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
10
 *
11
 * This code is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14
 * version 2 for more details (a copy is included in the LICENSE file that
15
 * accompanied this code).
16
 *
17
 * You should have received a copy of the GNU General Public License version
18
 * 2 along with this work; if not, write to the Free Software Foundation,
19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
 *
21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
 * or visit www.oracle.com if you need additional information or have any
23
 * questions.
24
 */
25

26
#include <stdio.h>
27
#include <stdlib.h>
28
#include <memory.h>
29
#include "sun_java2d_cmm_lcms_LCMS.h"
30
#include "sun_java2d_cmm_lcms_LCMSImageLayout.h"
31
#include "jni_util.h"
32
#include "Trace.h"
33
#include "Disposer.h"
34
#include <lcms2.h>
35
#include <lcms2_plugin.h>
36
#include "jlong.h"
37

38
#define SigMake(a,b,c,d) \
39
                    ( ( ((int) ((unsigned char) (a))) << 24) | \
40
                      ( ((int) ((unsigned char) (b))) << 16) | \
41
                      ( ((int) ((unsigned char) (c))) <<  8) | \
42
                          (int) ((unsigned char) (d)))
43

44
#define TagIdConst(a, b, c, d) \
45
                ((int) SigMake ((a), (b), (c), (d)))
46

47
#define SigHead TagIdConst('h','e','a','d')
48

49
#define DT_BYTE     sun_java2d_cmm_lcms_LCMSImageLayout_DT_BYTE
50
#define DT_SHORT    sun_java2d_cmm_lcms_LCMSImageLayout_DT_SHORT
51
#define DT_INT      sun_java2d_cmm_lcms_LCMSImageLayout_DT_INT
52

53
/* Default temp profile list size */
54
#define DF_ICC_BUF_SIZE 32
55

56
#define ERR_MSG_SIZE 256
57

58
typedef struct lcmsProfile_s {
59
    cmsHPROFILE pf;
60
} lcmsProfile_t, *lcmsProfile_p;
61

62
typedef union {
63
    cmsTagSignature cms;
64
    jint j;
65
} TagSignature_t, *TagSignature_p;
66

67
JavaVM *javaVM;
68

69
void errorHandler(cmsContext ContextID, cmsUInt32Number errorCode,
70
                  const char *errorText) {
71
    JNIEnv *env;
72
    char errMsg[ERR_MSG_SIZE] = {0};
73

74
    snprintf(errMsg, ERR_MSG_SIZE, "LCMS error %d: %s", errorCode, errorText);
75

76
    (*javaVM)->AttachCurrentThread(javaVM, (void**)&env, NULL);
77
    if (!(*env)->ExceptionCheck(env)) { // errorHandler may throw it before
78
        JNU_ThrowByName(env, "java/awt/color/CMMException", errMsg);
79
    }
80
}
81

82
JNIEXPORT jint JNICALL DEF_JNI_OnLoad(JavaVM *jvm, void *reserved) {
83
    javaVM = jvm;
84

85
    cmsSetLogErrorHandler(errorHandler);
86
    return JNI_VERSION_1_6;
87
}
88

89
void LCMS_freeProfile(JNIEnv *env, jlong ptr) {
90
    lcmsProfile_p p = (lcmsProfile_p)jlong_to_ptr(ptr);
91

92
    if (p != NULL) {
93
        if (p->pf != NULL) {
94
            cmsCloseProfile(p->pf);
95
        }
96
        free(p);
97
    }
98
}
99

100
void LCMS_freeTransform(JNIEnv *env, jlong ID)
101
{
102
    cmsHTRANSFORM sTrans = jlong_to_ptr(ID);
103
    /* Passed ID is always valid native ref so there is no check for zero */
104
    cmsDeleteTransform(sTrans);
105
}
106

107
/*
108
 * Throw an IllegalArgumentException and init the cause.
109
 */
110
static void ThrowIllegalArgumentException(JNIEnv *env, const char *msg) {
111
    jthrowable cause = (*env)->ExceptionOccurred(env);
112
    if (cause != NULL) {
113
        (*env)->ExceptionClear(env);
114
    }
115
    jstring str = JNU_NewStringPlatform(env, msg);
116
    if (str != NULL) {
117
        jobject iae = JNU_NewObjectByName(env,
118
                                "java/lang/IllegalArgumentException",
119
                                "(Ljava/lang/String;Ljava/lang/Throwable;)V",
120
                                str, cause);
121
        if (iae != NULL) {
122
            (*env)->Throw(env, iae);
123
        }
124
    }
125
}
126

127
/*
128
 * Class:     sun_java2d_cmm_lcms_LCMS
129
 * Method:    createNativeTransform
130
 * Signature: ([JIIZIZLjava/lang/Object;)J
131
 */
132
JNIEXPORT jlong JNICALL Java_sun_java2d_cmm_lcms_LCMS_createNativeTransform
133
  (JNIEnv *env, jclass cls, jlongArray profileIDs, jint renderingIntent,
134
   jint inFormatter, jint outFormatter, jobject disposerRef)
135
{
136
    cmsHPROFILE _iccArray[DF_ICC_BUF_SIZE];
137
    cmsHPROFILE *iccArray = &_iccArray[0];
138
    cmsHTRANSFORM sTrans = NULL;
139
    int i, j, size;
140
    jlong* ids;
141

142
    size = (*env)->GetArrayLength (env, profileIDs);
143
    ids = (*env)->GetLongArrayElements(env, profileIDs, 0);
144
    if (ids == NULL) {
145
        // An exception should have already been thrown.
146
        return 0L;
147
    }
148

149
    if (DF_ICC_BUF_SIZE < size*2) {
150
        iccArray = (cmsHPROFILE*) malloc(
151
            size*2*sizeof(cmsHPROFILE));
152
        if (iccArray == NULL) {
153
            (*env)->ReleaseLongArrayElements(env, profileIDs, ids, 0);
154

155
            J2dRlsTraceLn(J2D_TRACE_ERROR, "getXForm: iccArray == NULL");
156
            return 0L;
157
        }
158
    }
159

160
    j = 0;
161
    for (i = 0; i < size; i++) {
162
        cmsColorSpaceSignature cs;
163
        lcmsProfile_p profilePtr = (lcmsProfile_p)jlong_to_ptr(ids[i]);
164
        cmsHPROFILE icc = profilePtr->pf;
165

166
        iccArray[j++] = icc;
167

168
        /* Middle non-abstract profiles should be doubled before passing to
169
         * the cmsCreateMultiprofileTransform function
170
         */
171

172
        cs = cmsGetColorSpace(icc);
173
        if (size > 2 && i != 0 && i != size - 1 &&
174
            cs != cmsSigXYZData && cs != cmsSigLabData)
175
        {
176
            iccArray[j++] = icc;
177
        }
178
    }
179

180
    cmsUInt32Number dwFlags = 0;
181
    if (T_EXTRA(inFormatter) > 0 && T_EXTRA(outFormatter) > 0) {
182
        dwFlags |= cmsFLAGS_COPY_ALPHA;
183
    }
184

185
    sTrans = cmsCreateMultiprofileTransform(iccArray, j,
186
        inFormatter, outFormatter, renderingIntent, dwFlags);
187

188
    (*env)->ReleaseLongArrayElements(env, profileIDs, ids, 0);
189

190
    if (sTrans == NULL) {
191
        J2dRlsTraceLn(J2D_TRACE_ERROR, "LCMS_createNativeTransform: "
192
                                       "sTrans == NULL");
193
        if (!(*env)->ExceptionCheck(env)) { // errorHandler may throw it
194
            JNU_ThrowByName(env, "java/awt/color/CMMException",
195
                            "Cannot get color transform");
196
        }
197
    } else {
198
        Disposer_AddRecord(env, disposerRef, LCMS_freeTransform, ptr_to_jlong(sTrans));
199
    }
200

201
    if (iccArray != &_iccArray[0]) {
202
        free(iccArray);
203
    }
204
    return ptr_to_jlong(sTrans);
205
}
206

207

208
/*
209
 * Class:     sun_java2d_cmm_lcms_LCMS
210
 * Method:    loadProfileNative
211
 * Signature: ([BLjava/lang/Object;)J
212
 */
213
JNIEXPORT jlong JNICALL Java_sun_java2d_cmm_lcms_LCMS_loadProfileNative
214
  (JNIEnv *env, jclass cls, jbyteArray data, jobject disposerRef)
215
{
216
    jbyte* dataArray;
217
    jint dataSize;
218
    lcmsProfile_p sProf = NULL;
219
    cmsHPROFILE pf;
220

221
    if (JNU_IsNull(env, data)) {
222
        ThrowIllegalArgumentException(env, "Invalid profile data");
223
        return 0L;
224
    }
225

226
    dataArray = (*env)->GetByteArrayElements (env, data, 0);
227
    if (dataArray == NULL) {
228
        // An exception should have already been thrown.
229
        return 0L;
230
    }
231

232
    dataSize = (*env)->GetArrayLength (env, data);
233

234
    pf = cmsOpenProfileFromMem((const void *)dataArray,
235
                                     (cmsUInt32Number) dataSize);
236

237
    (*env)->ReleaseByteArrayElements (env, data, dataArray, 0);
238

239
    if (pf == NULL) {
240
        ThrowIllegalArgumentException(env, "Invalid profile data");
241
    } else {
242
        /* Sanity check: try to save the profile in order
243
         * to force basic validation.
244
         */
245
        cmsUInt32Number pfSize = 0;
246
        if (!cmsSaveProfileToMem(pf, NULL, &pfSize) ||
247
            pfSize < sizeof(cmsICCHeader))
248
        {
249
            ThrowIllegalArgumentException(env, "Invalid profile data");
250
            cmsCloseProfile(pf);
251
            pf = NULL;
252
        }
253
    }
254

255
    if (pf != NULL) {
256
        // create profile holder
257
        sProf = (lcmsProfile_p)malloc(sizeof(lcmsProfile_t));
258
        if (sProf != NULL) {
259
            // register the disposer record
260
            sProf->pf = pf;
261
            Disposer_AddRecord(env, disposerRef, LCMS_freeProfile, ptr_to_jlong(sProf));
262
        } else {
263
            cmsCloseProfile(pf);
264
        }
265
    }
266

267
    return ptr_to_jlong(sProf);
268
}
269

270
/*
271
 * Class:     sun_java2d_cmm_lcms_LCMS
272
 * Method:    getProfileDataNative
273
 * Signature: (J)[B
274
 */
275
JNIEXPORT jbyteArray JNICALL Java_sun_java2d_cmm_lcms_LCMS_getProfileDataNative
276
  (JNIEnv *env, jclass cls, jlong id)
277
{
278
    lcmsProfile_p sProf = (lcmsProfile_p)jlong_to_ptr(id);
279
    cmsUInt32Number pfSize = 0;
280

281
    // determine actual profile size
282
    if (!cmsSaveProfileToMem(sProf->pf, NULL, &pfSize)) {
283
        if (!(*env)->ExceptionCheck(env)) { // errorHandler may throw it
284
            JNU_ThrowByName(env, "java/awt/color/CMMException",
285
                            "Can not access specified profile.");
286
        }
287
        return NULL;
288
    }
289

290
    jbyteArray data = (*env)->NewByteArray(env, pfSize);
291
    if (data == NULL) {
292
        // An exception should have already been thrown.
293
        return NULL;
294
    }
295

296
    jbyte* dataArray = (*env)->GetByteArrayElements(env, data, 0);
297
    if (dataArray == NULL) {
298
        // An exception should have already been thrown.
299
        return NULL;
300
    }
301

302
    cmsBool status = cmsSaveProfileToMem(sProf->pf, dataArray, &pfSize);
303

304
    (*env)->ReleaseByteArrayElements(env, data, dataArray, 0);
305

306
    if (!status) {
307
        if (!(*env)->ExceptionCheck(env)) { // errorHandler may throw it
308
            JNU_ThrowByName(env, "java/awt/color/CMMException",
309
                            "Can not access specified profile.");
310
        }
311
        return NULL;
312
    }
313
    return data;
314
}
315

316
/* Get profile header info */
317
static cmsBool _getHeaderInfo(cmsHPROFILE pf, jbyte* pBuffer, jint bufferSize);
318
static cmsBool _setHeaderInfo(cmsHPROFILE pf, jbyte* pBuffer, jint bufferSize);
319
static cmsHPROFILE _writeCookedTag(cmsHPROFILE pfTarget, cmsTagSignature sig, jbyte *pData, jint size);
320

321

322
/*
323
 * Class:     sun_java2d_cmm_lcms_LCMS
324
 * Method:    getTagNative
325
 * Signature: (JI)[B
326
 */
327
JNIEXPORT jbyteArray JNICALL Java_sun_java2d_cmm_lcms_LCMS_getTagNative
328
  (JNIEnv *env, jclass cls, jlong id, jint tagSig)
329
{
330
    lcmsProfile_p sProf = (lcmsProfile_p)jlong_to_ptr(id);
331
    TagSignature_t sig;
332
    cmsUInt32Number tagSize;
333

334
    jbyte* dataArray = NULL;
335
    jbyteArray data = NULL;
336

337
    cmsUInt32Number bufSize;
338

339
    sig.j = tagSig;
340

341
    if (tagSig == SigHead) {
342
        cmsBool status;
343

344
        // allocate java array
345
        bufSize = sizeof(cmsICCHeader);
346
        data = (*env)->NewByteArray(env, bufSize);
347

348
        if (data == NULL) {
349
            // An exception should have already been thrown.
350
            return NULL;
351
        }
352

353
        dataArray = (*env)->GetByteArrayElements (env, data, 0);
354

355
        if (dataArray == NULL) {
356
            // An exception should have already been thrown.
357
            return NULL;
358
        }
359

360
        status = _getHeaderInfo(sProf->pf, dataArray, bufSize);
361

362
        (*env)->ReleaseByteArrayElements (env, data, dataArray, 0);
363

364
        if (!status) {
365
            if (!(*env)->ExceptionCheck(env)) { // errorHandler may throw it
366
                JNU_ThrowByName(env, "java/awt/color/CMMException",
367
                                "ICC Profile header not found");
368
            }
369
            return NULL;
370
        }
371

372
        return data;
373
    }
374

375
    if (cmsIsTag(sProf->pf, sig.cms)) {
376
        tagSize = cmsReadRawTag(sProf->pf, sig.cms, NULL, 0);
377
    } else {
378
        if (!(*env)->ExceptionCheck(env)) { // errorHandler may throw it
379
            JNU_ThrowByName(env, "java/awt/color/CMMException",
380
                            "ICC profile tag not found");
381
        }
382
        return NULL;
383
    }
384

385
    // allocate java array
386
    data = (*env)->NewByteArray(env, tagSize);
387
    if (data == NULL) {
388
        // An exception should have already been thrown.
389
        return NULL;
390
    }
391

392
    dataArray = (*env)->GetByteArrayElements (env, data, 0);
393

394
    if (dataArray == NULL) {
395
        // An exception should have already been thrown.
396
        return NULL;
397
    }
398

399
    bufSize = cmsReadRawTag(sProf->pf, sig.cms, dataArray, tagSize);
400

401
    (*env)->ReleaseByteArrayElements (env, data, dataArray, 0);
402

403
    if (bufSize != tagSize) {
404
        if (!(*env)->ExceptionCheck(env)) { // errorHandler may throw it
405
            JNU_ThrowByName(env, "java/awt/color/CMMException",
406
                            "Can not get tag data.");
407
        }
408
        return NULL;
409
    }
410
    return data;
411
}
412

413
/*
414
 * Class:     sun_java2d_cmm_lcms_LCMS
415
 * Method:    setTagDataNative
416
 * Signature: (JI[B)V
417
 */
418
JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_setTagDataNative
419
  (JNIEnv *env, jclass cls, jlong id, jint tagSig, jbyteArray data)
420
{
421
    lcmsProfile_p sProf = (lcmsProfile_p)jlong_to_ptr(id);
422
    cmsHPROFILE pfReplace = NULL;
423

424
    TagSignature_t sig;
425
    cmsBool status = FALSE;
426
    jbyte* dataArray;
427
    int tagSize;
428

429
    sig.j = tagSig;
430

431
    if (JNU_IsNull(env, data)) {
432
        ThrowIllegalArgumentException(env, "Can not write tag data.");
433
        return;
434
    }
435

436
    tagSize =(*env)->GetArrayLength(env, data);
437

438
    dataArray = (*env)->GetByteArrayElements(env, data, 0);
439

440
    if (dataArray == NULL) {
441
        // An exception should have already been thrown.
442
        return;
443
    }
444

445
    if (tagSig == SigHead) {
446
        status  = _setHeaderInfo(sProf->pf, dataArray, tagSize);
447
    } else {
448
        /*
449
        * New strategy for generic tags: create a place holder,
450
        * dump all existing tags there, dump externally supplied
451
        * tag, and return the new profile to the java.
452
        */
453
        pfReplace = _writeCookedTag(sProf->pf, sig.cms, dataArray, tagSize);
454
        status = (pfReplace != NULL);
455
    }
456

457
    (*env)->ReleaseByteArrayElements(env, data, dataArray, 0);
458

459
    if (!status) {
460
        ThrowIllegalArgumentException(env, "Can not write tag data.");
461
    } else if (pfReplace != NULL) {
462
        cmsCloseProfile(sProf->pf);
463
        sProf->pf = pfReplace;
464
    }
465
}
466

467
static void *getILData(JNIEnv *env, jobject data, jint type) {
468
    switch (type) {
469
        case DT_BYTE:
470
            return (*env)->GetByteArrayElements(env, data, 0);
471
        case DT_SHORT:
472
            return (*env)->GetShortArrayElements(env, data, 0);
473
        case DT_INT:
474
            return (*env)->GetIntArrayElements(env, data, 0);
475
        default:
476
            return NULL;
477
    }
478
}
479

480
static void releaseILData(JNIEnv *env, void *pData, jint type, jobject data,
481
                          jint mode) {
482
    switch (type) {
483
        case DT_BYTE:
484
            (*env)->ReleaseByteArrayElements(env, data, (jbyte *) pData, mode);
485
            break;
486
        case DT_SHORT:
487
            (*env)->ReleaseShortArrayElements(env, data, (jshort *) pData, mode);
488
            break;
489
        case DT_INT:
490
            (*env)->ReleaseIntArrayElements(env, data, (jint *) pData, mode);
491
            break;
492
    }
493
}
494

495
/*
496
 * Class:     sun_java2d_cmm_lcms_LCMS
497
 * Method:    colorConvert
498
 * Signature: (JIIIIIIZZLjava/lang/Object;Ljava/lang/Object;)V
499
 */
500
JNIEXPORT void JNICALL Java_sun_java2d_cmm_lcms_LCMS_colorConvert
501
  (JNIEnv *env, jclass cls, jlong ID, jint width, jint height, jint srcOffset,
502
   jint srcNextRowOffset, jint dstOffset, jint dstNextRowOffset,
503
   jobject srcData, jobject dstData, jint srcDType, jint dstDType)
504
{
505
    cmsHTRANSFORM sTrans = jlong_to_ptr(ID);
506

507
    if (sTrans == NULL) {
508
        J2dRlsTraceLn(J2D_TRACE_ERROR, "LCMS_colorConvert: transform == NULL");
509
        JNU_ThrowByName(env, "java/awt/color/CMMException",
510
                        "Cannot get color transform");
511
        return;
512
    }
513

514
    void *inputBuffer = getILData(env, srcData, srcDType);
515
    if (inputBuffer == NULL) {
516
        J2dRlsTraceLn(J2D_TRACE_ERROR, "");
517
        // An exception should have already been thrown.
518
        return;
519
    }
520

521
    void *outputBuffer = getILData(env, dstData, dstDType);
522
    if (outputBuffer == NULL) {
523
        releaseILData(env, inputBuffer, srcDType, srcData, JNI_ABORT);
524
        // An exception should have already been thrown.
525
        return;
526
    }
527

528
    char *input = (char *) inputBuffer + srcOffset;
529
    char *output = (char *) outputBuffer + dstOffset;
530

531
    cmsDoTransformLineStride(sTrans, input, output, width, height,
532
                             srcNextRowOffset, dstNextRowOffset, 0, 0);
533

534
    releaseILData(env, inputBuffer, srcDType, srcData, JNI_ABORT);
535
    releaseILData(env, outputBuffer, dstDType, dstData, 0);
536
}
537

538
static cmsBool _getHeaderInfo(cmsHPROFILE pf, jbyte* pBuffer, jint bufferSize)
539
{
540
  cmsUInt32Number pfSize = 0;
541
  cmsUInt8Number* pfBuffer = NULL;
542
  cmsBool status = FALSE;
543

544
  if (!cmsSaveProfileToMem(pf, NULL, &pfSize) ||
545
      pfSize < sizeof(cmsICCHeader) ||
546
      bufferSize < (jint)sizeof(cmsICCHeader))
547
  {
548
    return FALSE;
549
  }
550

551
  pfBuffer = malloc(pfSize);
552
  if (pfBuffer == NULL) {
553
    return FALSE;
554
  }
555

556
  // load raw profile data into the buffer
557
  if (cmsSaveProfileToMem(pf, pfBuffer, &pfSize)) {
558
    memcpy(pBuffer, pfBuffer, sizeof(cmsICCHeader));
559
    status = TRUE;
560
  }
561
  free(pfBuffer);
562
  return status;
563
}
564

565
static cmsBool _setHeaderInfo(cmsHPROFILE pf, jbyte* pBuffer, jint bufferSize)
566
{
567
  cmsICCHeader pfHeader;
568

569
  if (pBuffer == NULL || bufferSize < (jint)sizeof(cmsICCHeader)) {
570
    return FALSE;
571
  }
572

573
  memcpy(&pfHeader, pBuffer, sizeof(cmsICCHeader));
574

575
  // now set header fields, which we can access using the lcms2 public API
576
  cmsSetHeaderFlags(pf, _cmsAdjustEndianess32(pfHeader.flags));
577
  cmsSetHeaderManufacturer(pf, _cmsAdjustEndianess32(pfHeader.manufacturer));
578
  cmsSetHeaderModel(pf, _cmsAdjustEndianess32(pfHeader.model));
579
  cmsUInt64Number attributes;
580
  _cmsAdjustEndianess64(&attributes, &pfHeader.attributes);
581
  cmsSetHeaderAttributes(pf, attributes);
582
  cmsSetHeaderProfileID(pf, (cmsUInt8Number*)&(pfHeader.profileID));
583
  cmsSetHeaderRenderingIntent(pf, _cmsAdjustEndianess32(pfHeader.renderingIntent));
584
  cmsSetPCS(pf, _cmsAdjustEndianess32(pfHeader.pcs));
585
  cmsSetColorSpace(pf, _cmsAdjustEndianess32(pfHeader.colorSpace));
586
  cmsSetDeviceClass(pf, _cmsAdjustEndianess32(pfHeader.deviceClass));
587
  cmsSetEncodedICCversion(pf, _cmsAdjustEndianess32(pfHeader.version));
588

589
  return TRUE;
590
}
591

592
/* Returns new profile handler, if it was created successfully,
593
   NULL otherwise.
594
   */
595
static cmsHPROFILE _writeCookedTag(const cmsHPROFILE pfTarget,
596
                               const cmsTagSignature sig,
597
                               jbyte *pData, jint size)
598
{
599
    cmsUInt32Number pfSize = 0;
600
    const cmsInt32Number tagCount = cmsGetTagCount(pfTarget);
601
    cmsInt32Number i;
602
    cmsHPROFILE pfSanity = NULL;
603

604
    cmsICCHeader hdr;
605

606
    cmsHPROFILE p = cmsCreateProfilePlaceholder(NULL);
607

608
    if (NULL == p) {
609
        return NULL;
610
    }
611
    memset(&hdr, 0, sizeof(cmsICCHeader));
612

613
    // Populate the placeholder's header according to target profile
614
    hdr.flags = cmsGetHeaderFlags(pfTarget);
615
    hdr.renderingIntent = cmsGetHeaderRenderingIntent(pfTarget);
616
    hdr.manufacturer = cmsGetHeaderManufacturer(pfTarget);
617
    hdr.model = cmsGetHeaderModel(pfTarget);
618
    hdr.pcs = cmsGetPCS(pfTarget);
619
    hdr.colorSpace = cmsGetColorSpace(pfTarget);
620
    hdr.deviceClass = cmsGetDeviceClass(pfTarget);
621
    hdr.version = cmsGetEncodedICCversion(pfTarget);
622
    cmsGetHeaderAttributes(pfTarget, &hdr.attributes);
623
    cmsGetHeaderProfileID(pfTarget, (cmsUInt8Number*)&hdr.profileID);
624

625
    cmsSetHeaderFlags(p, hdr.flags);
626
    cmsSetHeaderManufacturer(p, hdr.manufacturer);
627
    cmsSetHeaderModel(p, hdr.model);
628
    cmsSetHeaderAttributes(p, hdr.attributes);
629
    cmsSetHeaderProfileID(p, (cmsUInt8Number*)&(hdr.profileID));
630
    cmsSetHeaderRenderingIntent(p, hdr.renderingIntent);
631
    cmsSetPCS(p, hdr.pcs);
632
    cmsSetColorSpace(p, hdr.colorSpace);
633
    cmsSetDeviceClass(p, hdr.deviceClass);
634
    cmsSetEncodedICCversion(p, hdr.version);
635

636
    // now write the user supplied tag
637
    if (size <= 0 || !cmsWriteRawTag(p, sig, pData, size)) {
638
        cmsCloseProfile(p);
639
        return NULL;
640
    }
641

642
    // copy tags from the original profile
643
    for (i = 0; i < tagCount; i++) {
644
        cmsBool isTagReady = FALSE;
645
        const cmsTagSignature s = cmsGetTagSignature(pfTarget, i);
646
        const cmsUInt32Number tagSize = cmsReadRawTag(pfTarget, s, NULL, 0);
647

648
        if (s == sig) {
649
            // skip the user supplied tag
650
            continue;
651
        }
652

653
        // read raw tag from the original profile
654
        if (tagSize > 0) {
655
            cmsUInt8Number* buf = (cmsUInt8Number*)malloc(tagSize);
656
            if (buf != NULL) {
657
                if (tagSize ==  cmsReadRawTag(pfTarget, s, buf, tagSize)) {
658
                    // now we are ready to write the tag
659
                    isTagReady = cmsWriteRawTag(p, s, buf, tagSize);
660
                }
661
                free(buf);
662
            }
663
        }
664

665
        if (!isTagReady) {
666
            cmsCloseProfile(p);
667
            return NULL;
668
        }
669
    }
670

671
    // now we have all tags moved to the new profile.
672
    // do some sanity checks: write it to a memory buffer and read again.
673
    void* buf = NULL;
674
    if (cmsSaveProfileToMem(p, NULL, &pfSize)) {
675
        buf = malloc(pfSize);
676
        if (buf != NULL) {
677
            // load raw profile data into the buffer
678
            if (cmsSaveProfileToMem(p, buf, &pfSize)) {
679
                pfSanity = cmsOpenProfileFromMem(buf, pfSize);
680
            }
681
        }
682
    }
683

684
    cmsCloseProfile(p); // No longer needed.
685

686
    if (pfSanity == NULL) {
687
        // for some reason, we failed to save and read the updated profile
688
        // It likely indicates that the profile is not correct, so we report
689
        // a failure here.
690
        free(buf);
691
        return NULL;
692
    } else {
693
        // do final check whether we can read and handle the target tag.
694
        const void* pTag = cmsReadTag(pfSanity, sig);
695
        if (pTag == NULL) {
696
            // the tag can not be cooked
697
            free(buf);
698
            cmsCloseProfile(pfSanity);
699
            return NULL;
700
        }
701
        // The profile we used for sanity checking needs to be returned
702
        // since the one we updated is raw - not cooked.
703
        // Except we want to re-open it since the call to cmsReadTag()
704
        // means we may not get back the same bytes as we set.
705
        // Whilst this may change later anyway, we can at least prevent
706
        // it from happening immediately.
707
        cmsCloseProfile(pfSanity);
708
        pfSanity = cmsOpenProfileFromMem(buf, pfSize);
709
        free(buf);
710
        return pfSanity;
711
    }
712
}
713

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.