File indexing completed on 2025-01-05 03:57:09

0001 /* -*- C++ -*-
0002  * Copyright 2019-2021 LibRaw LLC (info@libraw.org)
0003  *
0004  LibRaw uses code from dcraw.c -- Dave Coffin's raw photo decoder,
0005  dcraw.c is copyright 1997-2018 by Dave Coffin, dcoffin a cybercom o net.
0006  LibRaw do not use RESTRICTED code from dcraw.c
0007 
0008  LibRaw is free software; you can redistribute it and/or modify
0009  it under the terms of the one of two licenses as you choose:
0010 
0011 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
0012    (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
0013 
0014 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
0015    (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
0016 
0017  */
0018 
0019 #include "../../internal/dcraw_fileio_defs.h"
0020 
0021 #ifndef NO_LCMS
0022 void LibRaw::apply_profile(const char *input, const char *output)
0023 {
0024   char *prof;
0025   cmsHPROFILE hInProfile = 0, hOutProfile = 0;
0026   cmsHTRANSFORM hTransform;
0027   FILE *fp;
0028   unsigned size;
0029 
0030   if (strcmp(input, "embed"))
0031     hInProfile = cmsOpenProfileFromFile(input, "r");
0032   else if (profile_length)
0033   {
0034     hInProfile = cmsOpenProfileFromMem(imgdata.color.profile, profile_length);
0035   }
0036   else
0037   {
0038     imgdata.process_warnings |= LIBRAW_WARN_NO_EMBEDDED_PROFILE;
0039   }
0040   if (!hInProfile)
0041   {
0042     imgdata.process_warnings |= LIBRAW_WARN_NO_INPUT_PROFILE;
0043     return;
0044   }
0045   if (!output)
0046     hOutProfile = cmsCreate_sRGBProfile();
0047   else if ((fp = fopen(output, "rb")))
0048   {
0049     fread(&size, 4, 1, fp);
0050     fseek(fp, 0, SEEK_SET);
0051     oprof = (unsigned *)malloc(size = ntohl(size));
0052     fread(oprof, 1, size, fp);
0053     fclose(fp);
0054     if (!(hOutProfile = cmsOpenProfileFromMem(oprof, size)))
0055     {
0056       free(oprof);
0057       oprof = 0;
0058     }
0059   }
0060   if (!hOutProfile)
0061   {
0062     imgdata.process_warnings |= LIBRAW_WARN_BAD_OUTPUT_PROFILE;
0063     goto quit;
0064   }
0065   RUN_CALLBACK(LIBRAW_PROGRESS_APPLY_PROFILE, 0, 2);
0066   hTransform = cmsCreateTransform(hInProfile, TYPE_RGBA_16, hOutProfile,
0067                                   TYPE_RGBA_16, INTENT_PERCEPTUAL, 0);
0068   cmsDoTransform(hTransform, image, image, width * height);
0069   raw_color = 1; /* Don't use rgb_cam with a profile */
0070   cmsDeleteTransform(hTransform);
0071   cmsCloseProfile(hOutProfile);
0072 quit:
0073   cmsCloseProfile(hInProfile);
0074   RUN_CALLBACK(LIBRAW_PROGRESS_APPLY_PROFILE, 1, 2);
0075 }
0076 #endif