{"id":296,"date":"2009-10-20T13:15:38","date_gmt":"2009-10-20T17:15:38","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2009\/10\/20\/embedding-an-icc-profile-into-a-tiff-file\/"},"modified":"2019-10-29T08:01:53","modified_gmt":"2019-10-29T12:01:53","slug":"embedding-an-icc-profile-into-a-tiff-file","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2009\/10\/20\/embedding-an-icc-profile-into-a-tiff-file\/","title":{"rendered":"Embedding an ICC profile into a TIFF file"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>The R2009b release of MATLAB contains a new <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/import_export\/f5-123068.html#br_c_iz-1\">Tiff class<\/a>.  The primary purpose of this class is to provide lower-level access to creating and modifying image data and metadata in\r\n      an existing TIFF file.\r\n   <\/p>\r\n   <p>Several customers have asked for the ability to embed an ICC profile into a TIFF file.  The Image Processing Toolbox function\r\n      <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/images\/index.html?\/access\/helpdesk\/help\/releases\/R2009b\/toolbox\/images\/iccread.html\"><tt>iccread<\/tt><\/a> can read a profile embedded in a TIFF file, but <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/images\/index.html?\/access\/helpdesk\/help\/releases\/R2009b\/toolbox\/images\/iccwrite.html\"><tt>iccwrite<\/tt><\/a> can only write a stand-alone profile file.\r\n   <\/p>\r\n   <p>Here is code using the new Tiff class to embed a profile in an existing TIFF file. Let's start by rewriting one of the Image\r\n      Processing Toolbox sample PNG image files as a TIFF file.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">rgb = imread(<span style=\"color: #A020F0\">'peppers.png'<\/span>);\r\nimwrite(rgb, <span style=\"color: #A020F0\">'peppers.tif'<\/span>);\r\ns = dir(<span style=\"color: #A020F0\">'peppers.tif'<\/span>)<\/pre><pre style=\"font-style:oblique\">\r\ns = \r\n\r\n       name: 'peppers.tif'\r\n       date: '20-Oct-2009 09:14:29'\r\n      bytes: 593880\r\n      isdir: 0\r\n    datenum: 7.3407e+005\r\n\r\n<\/pre><p>Now let's embed the sample profile sRGB.icm into the TIFF file we just made.<\/p>\r\n   <p>Step 1. Read in the raw bytes of the profile file.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">fid = fopen(<span style=\"color: #A020F0\">'sRGB.icm'<\/span>);\r\nraw_profile_bytes = fread(fid, Inf, <span style=\"color: #A020F0\">'uint8=&gt;uint8'<\/span>);\r\nfclose(fid);<\/pre><p>Step 2. Initialize a Tiff object using 'r+' mode (read and modify).<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">tif = Tiff(<span style=\"color: #A020F0\">'peppers.tif'<\/span>, <span style=\"color: #A020F0\">'r+'<\/span>)<\/pre><pre style=\"font-style:oblique\">\r\ntif = \r\n\r\n                  TIFF File: 'peppers.tif'\r\n                       Mode: 'r+'\r\n    Current Image Directory: 1\r\n           Number Of Strips: 77\r\n                SubFileType: Tiff.SubFileType.Default\r\n                Photometric: Tiff.Photometric.RGB\r\n                ImageLength: 384\r\n                 ImageWidth: 512\r\n               RowsPerStrip: 5\r\n              BitsPerSample: 8\r\n                Compression: Tiff.Compression.PackBits\r\n               SampleFormat: Tiff.SampleFormat.UInt\r\n            SamplesPerPixel: 3\r\n        PlanarConfiguration: Tiff.PlanarConfiguration.Chunky\r\n                Orientation: Tiff.Orientation.TopLeft\r\n\r\n<\/pre><p>Step 3. Embed the profile bytes as a TIFF tag.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">tif.setTag(<span style=\"color: #A020F0\">'ICCProfile'<\/span>, raw_profile_bytes);<\/pre><p>Step 4. Tell the Tiff object to update the image metadata in the file.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">tif.rewriteDirectory();<\/pre><p>Step 5. Close the Tiff object.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">tif.close();<\/pre><p>Now the TIFF file contains the profile. Notice the file size has changed.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s = dir(<span style=\"color: #A020F0\">'peppers.tif'<\/span>)<\/pre><pre style=\"font-style:oblique\">\r\ns = \r\n\r\n       name: 'peppers.tif'\r\n       date: '20-Oct-2009 09:14:30'\r\n      bytes: 597828\r\n      isdir: 0\r\n    datenum: 7.3407e+005\r\n\r\n<\/pre><p>And we can read in the profile using <tt>iccread<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">p = iccread(<span style=\"color: #A020F0\">'peppers.tif'<\/span>)<\/pre><pre style=\"font-style:oblique\">\r\np = \r\n\r\n               Header: [1x1 struct]\r\n             TagTable: {17x3 cell}\r\n            Copyright: 'Copyright (c) 1999 Hewlett-Packard Company'\r\n          Description: [1x1 struct]\r\n      MediaWhitePoint: [0.9505 1 1.0891]\r\n      MediaBlackPoint: [0 0 0]\r\n        DeviceMfgDesc: [1x1 struct]\r\n      DeviceModelDesc: [1x1 struct]\r\n      ViewingCondDesc: [1x1 struct]\r\n    ViewingConditions: [1x1 struct]\r\n            Luminance: [76.0365 80 87.1246]\r\n          Measurement: [1x1 struct]\r\n           Technology: 'Cathode Ray Tube Display'\r\n               MatTRC: [1x1 struct]\r\n          PrivateTags: {}\r\n             Filename: 'peppers.tif'\r\n\r\n<\/pre><p>It would be logical to enhance <tt>iccwrite<\/tt> to make this easier for you. We'll look into that, although I'm not sure when that might happen.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_9eb468b777734c0285ee9b48dff42d03() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='9eb468b777734c0285ee9b48dff42d03 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 9eb468b777734c0285ee9b48dff42d03';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = '';\r\n        copyright = 'Copyright 2009 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_9eb468b777734c0285ee9b48dff42d03()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.9<br><\/p>\r\n<\/div>\r\n<!--\r\n9eb468b777734c0285ee9b48dff42d03 ##### SOURCE BEGIN #####\r\n%%\r\n% The R2009b release of MATLAB contains a new \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/import_export\/f5-123068.html#br_c_iz-1 \r\n% Tiff class>.  The primary purpose\r\n% of this class is to provide lower-level access to creating and modifying\r\n% image data and metadata in an existing TIFF file.\r\n%\r\n% Several customers have asked for the ability to embed an ICC profile into a\r\n% TIFF file.  The Image Processing Toolbox function \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/images\/index.html?\/access\/helpdesk\/help\/releases\/R2009b\/toolbox\/images\/iccread.html \r\n% |iccread|> can read a profile\r\n% embedded in a TIFF file, but \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/toolbox\/images\/index.html?\/access\/helpdesk\/help\/releases\/R2009b\/toolbox\/images\/iccwrite.html \r\n% |iccwrite|> can only write a stand-alone profile\r\n% file.\r\n%\r\n% Here is code using the new Tiff class to embed a profile in an existing TIFF\r\n% file. Let's start by rewriting one of the Image Processing Toolbox sample\r\n% PNG image files as a TIFF file.\r\n\r\nrgb = imread('peppers.png');\r\nimwrite(rgb, 'peppers.tif');\r\ns = dir('peppers.tif')\r\n\r\n%%\r\n% Now let's embed the sample profile sRGB.icm into the TIFF file we just made.\r\n%\r\n% Step 1. Read in the raw bytes of the profile file.\r\nfid = fopen('sRGB.icm');\r\nraw_profile_bytes = fread(fid, Inf, 'uint8=>uint8');\r\nfclose(fid);\r\n\r\n%%\r\n% Step 2. Initialize a Tiff object using 'r+' mode (read and modify).\r\ntif = Tiff('peppers.tif', 'r+')\r\n\r\n%%\r\n% Step 3. Embed the profile bytes as a TIFF tag.\r\ntif.setTag('ICCProfile', raw_profile_bytes);\r\n\r\n%%\r\n% Step 4. Tell the Tiff object to update the image metadata in the file.\r\ntif.rewriteDirectory();\r\n\r\n%%\r\n% Step 5. Close the Tiff object.\r\ntif.close();\r\n\r\n%%\r\n% Now the TIFF file contains the profile. Notice the file size has changed.\r\ns = dir('peppers.tif')\r\n\r\n%%\r\n% And we can read in the profile using |iccread|.\r\np = iccread('peppers.tif')\r\n\r\n%%\r\n% It would be logical to enhance |iccwrite| to make this easier for you. We'll look\r\n% into that, although I'm not sure when that might happen.\r\n##### SOURCE END ##### 9eb468b777734c0285ee9b48dff42d03\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   The R2009b release of MATLAB contains a new Tiff class.  The primary purpose of this class is to provide lower-level access to creating and modifying image data and metadata in\r\n      an... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/10\/20\/embedding-an-icc-profile-into-a-tiff-file\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[278,154,681,677,679,530,478,76,164,687,685,683],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/296"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=296"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/296\/revisions"}],"predecessor-version":[{"id":3657,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/296\/revisions\/3657"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}