1 /* Compile with
2 
3    gcc rawsave.c `pkg-config vips --cflags --libs`
4 
5 */
6 
7 #include <vips/vips.h>
8 #include <lcms2.h>
9 
10 // Create the ICC virtual profile for adobe rgb space
11 cmsHPROFILE
dt_colorspaces_create_adobergb_profile(void)12 dt_colorspaces_create_adobergb_profile(void)
13 {
14   cmsHPROFILE  hAdobeRGB;
15 
16   cmsCIEXYZTRIPLE Colorants =
17   {
18     {0.609741, 0.311111, 0.019470},
19     {0.205276, 0.625671, 0.060867},
20     {0.149185, 0.063217, 0.744568}
21   };
22 
23   cmsCIEXYZ black = { 0, 0, 0 };
24   cmsCIEXYZ D65 = { 0.95045, 1, 1.08905 };
25   cmsToneCurve* transferFunction;
26 
27   // AdobeRGB's "2.2" gamma is technically defined as 2 + 51/256
28   transferFunction = cmsBuildGamma(NULL, 2.19921875);
29 
30   hAdobeRGB = cmsCreateProfilePlaceholder(0);
31 
32   cmsSetProfileVersion(hAdobeRGB, 2.1);
33 
34   cmsMLU *mlu0 = cmsMLUalloc(NULL, 1);
35   cmsMLUsetASCII(mlu0, "en", "US", "Public Domain");
36   cmsMLU *mlu1 = cmsMLUalloc(NULL, 1);
37   cmsMLUsetASCII(mlu1, "en", "US", "Adobe RGB (compatible)");
38   cmsMLU *mlu2 = cmsMLUalloc(NULL, 1);
39   cmsMLUsetASCII(mlu2, "en", "US", "Darktable");
40   cmsMLU *mlu3 = cmsMLUalloc(NULL, 1);
41   cmsMLUsetASCII(mlu3, "en", "US", "Adobe RGB");
42   // this will only be displayed when the embedded profile is read by for example GIMP
43   cmsWriteTag(hAdobeRGB, cmsSigCopyrightTag,          mlu0);
44   cmsWriteTag(hAdobeRGB, cmsSigProfileDescriptionTag, mlu1);
45   cmsWriteTag(hAdobeRGB, cmsSigDeviceMfgDescTag,      mlu2);
46   cmsWriteTag(hAdobeRGB, cmsSigDeviceModelDescTag,    mlu3);
47   cmsMLUfree(mlu0);
48   cmsMLUfree(mlu1);
49   cmsMLUfree(mlu2);
50   cmsMLUfree(mlu3);
51 
52   cmsSetDeviceClass(hAdobeRGB, cmsSigDisplayClass);
53   cmsSetColorSpace(hAdobeRGB, cmsSigRgbData);
54   cmsSetPCS(hAdobeRGB, cmsSigXYZData);
55 
56   cmsWriteTag(hAdobeRGB, cmsSigMediaWhitePointTag, &D65);
57   cmsWriteTag(hAdobeRGB, cmsSigMediaBlackPointTag, &black);
58 
59   cmsWriteTag(hAdobeRGB, cmsSigRedColorantTag, (void*) &Colorants.Red);
60   cmsWriteTag(hAdobeRGB, cmsSigGreenColorantTag, (void*) &Colorants.Green);
61   cmsWriteTag(hAdobeRGB, cmsSigBlueColorantTag, (void*) &Colorants.Blue);
62 
63   cmsWriteTag(hAdobeRGB, cmsSigRedTRCTag, (void*) transferFunction);
64   cmsLinkTag(hAdobeRGB, cmsSigGreenTRCTag, cmsSigRedTRCTag );
65   cmsLinkTag(hAdobeRGB, cmsSigBlueTRCTag, cmsSigRedTRCTag );
66 
67   return hAdobeRGB;
68 }
69 
70 
71 int
main(int argc,char ** argv)72 main( int argc, char **argv )
73 {
74   VipsImage *image;
75   VipsImage *image2;
76   VipsImage *out;
77 
78   if( vips_init( argv[0] ) )
79     vips_error_exit( "unable to init" );
80 
81   im_concurrency_set( 1 );
82 
83   if( !(image = vips_image_new_from_file( argv[1], NULL )) )
84     vips_error_exit( NULL );
85 
86   GType type = vips_image_get_typeof(image, VIPS_META_ICC_NAME );
87   if( type ) printf( "Embedded profile found after vips_image_new_from_file()\n" );
88   else printf( "Embedded profile not found after vips_image_new_from_file()\n" );
89 
90   if( vips_copy( image, &image2, NULL ) )
91     vips_error_exit( NULL );
92   g_object_unref( image );
93 
94   image = image2;
95 
96   cmsHPROFILE out_profile = dt_colorspaces_create_adobergb_profile();
97   if( out_profile ) {
98     cmsUInt32Number out_length;
99     cmsSaveProfileToMem( out_profile, NULL, &out_length);
100     void* buf = malloc( out_length );
101     cmsSaveProfileToMem( out_profile, buf, &out_length);
102     vips_image_set_blob( image, VIPS_META_ICC_NAME,
103        (VipsCallbackFn) g_free, buf, out_length );
104     //char tstr[1024];
105     //cmsGetProfileInfoASCII(out_profile, cmsInfoDescription, "en", "US", tstr, 1024);
106     //printf("RawOutputPar::build(): embedded profile: %s\n",tstr);
107   }
108 
109   type = vips_image_get_typeof(image, VIPS_META_ICC_NAME );
110   if( type ) printf( "Embedded profile found after vips_image_set_blob()\n" );
111   else printf( "Embedded profile not found after vips_image_set_blob()\n" );
112 
113 
114   vips_image_write_to_file( image, argv[2], NULL );
115   type = vips_image_get_typeof(image, VIPS_META_ICC_NAME );
116   if( type ) printf( "Embedded profile found after vips_image_write_to_file()\n" );
117   else printf( "Embedded profile not found after vips_image_write_to_file()\n" );
118 
119   g_object_unref( image2 );
120 
121   return( 0 );
122 }
123