{"id":77,"date":"2006-08-11T07:00:53","date_gmt":"2006-08-11T11:00:53","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=77"},"modified":"2019-10-22T14:04:51","modified_gmt":"2019-10-22T18:04:51","slug":"discrete-cosine-transforms-jpeg-and-software-compatibility","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/08\/11\/discrete-cosine-transforms-jpeg-and-software-compatibility\/","title":{"rendered":"Discrete cosine transforms, JPEG, and software compatibility"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Software compatibility is a tricky subject. No one likes it when an incompatible change occurs in a software application they\r\n      rely on.  So why do we sometimes find it necessary to make an incompatible change? Here's an illustrative story from the early\r\n      days of the Image Processing Toolbox.\r\n   <\/p>\r\n   <p>The function <tt>dct2<\/tt>, which computes the two-dimensional discrete cosine transform (DCT), appeared in the original 1993 version of the toolbox.\r\n      The authors of the toolbox relied on several authoritative references, including <a href=\"http:\/\/www.amazon.com\/gp\/product\/0139353224\/sr=8-1\/qid=1154107182\/ref=sr_1_1\/104-1319363-5457505?ie=UTF8\"><i>Two-Dimensional Signal and Image Processing<\/i><\/a> by Lim. This book contains the following one-dimensional DCT equation:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/77\/dct_jpeg_compatibility_eq204022.png\"> <\/p>\r\n   <p>The original implementation of <tt>dct2<\/tt> used this formula.\r\n   <\/p>\r\n   <p>At roughly the same time as the first toolbox release, the newly-standardized JPEG image compression method and format was\r\n      becoming popular.  The DCT is the mathematical heart of the JPEG algorithm. Here's the 8-point one-dimensional DCT equation\r\n      that appears in <a href=\"http:\/\/www.amazon.com\/gp\/product\/0442012721\/sr=8-1\/qid=1154107219\/ref=sr_1_1\/104-1319363-5457505?ie=UTF8\"><i>JPEG: Still Image Data Compression Standard<\/i><\/a> by Pennebaker and Mitchell.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/77\/dct_jpeg_compatibility_eq92311.png\"> <\/p>\r\n   <p>where<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/77\/dct_jpeg_compatibility_eq761154.png\"> <\/p>\r\n   <p>The variable name differences are trivial, but the scale factor differences are very important!  The JPEG DCT is an orthonormal\r\n      transform, whereas the DCT defined in Lim's book is not.\r\n   <\/p>\r\n   <p>Users of version 1 began to contact tech support, wondering why <tt>dct2<\/tt> was giving the \"wrong\" answer.  We realized that we would have to do something, but what?  Changing the output of <tt>dct2<\/tt> would introduce an incompatibility.  Not changing it would let user confusion and tech support calls continue indefinitely.\r\n   <\/p>\r\n   <p>We eventually made a decision based on the fact that image compression is the only widespread application of the DCT in image\r\n      processing. Therefore, it made no sense for our function to produce an answer inconsistent with the standardized DCT-based\r\n      image compression method. So we changed the output of <tt>dct2<\/tt> in version 2 of the toolbox.  When no complaints came in about the change, we breathed a sigh of relief.\r\n   <\/p>\r\n   <p>Today at The MathWorks we have processes in place to carefully evaluate changes that might result in an incompatibility. \r\n      What options have the developers considered?  What steps are being taken to mitigate possible negative impacts of the change?\r\n       What is the benefit to the user of the change?\r\n   <\/p>\r\n   <p>Whether we are doing a good job or not is up to you to judge.  How do you think we are doing?  How has the evolution of MATLAB\r\n      over the years affected your work?  Have any good stories to tell?  Leave your comments <a href=\"https:\/\/blogs.mathworks.com\/steve\/?p=77#respond\">here<\/a>.\r\n   <\/p>\r\n <script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_77() {\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='77 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 77';\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 = 'Steve Eddins';\r\n        copyright = 'Copyright 2006 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      <\/script>\r\n<noscript>\r\n<em>A JavaScript-enabled browser is required to use the \"Get the MATLAB code\" link.<\/em>\r\n<\/noscript>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_77()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code<\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.2<br><\/p>\r\n<\/div>\r\n<!--\r\n77 ##### SOURCE BEGIN #####\r\n%% Discrete cosine transforms, JPEG, and software compatibility\r\n%\r\n% Software compatibility is a tricky subject. No one likes it when an\r\n% incompatible change occurs in a software application they rely on.  So\r\n% why do we sometimes find it necessary to make an incompatible change?\r\n% Here's an illustrative story from the early days of the Image Processing\r\n% Toolbox.\r\n%\r\n% The function <https:\/\/www.mathworks.com\/help\/images\/index.htmldct2.html \r\n% |dct2|>, which computes the two-dimensional discrete cosine\r\n% transform (DCT), appeared in the original 1993 version of the toolbox.\r\n% The authors of the toolbox relied on several authoritative references,\r\n% including \r\n% <http:\/\/www.amazon.com\/gp\/product\/0139353224\/sr=8-1\/qid=1154107182\/ref=sr_1_1\/104-1319363-5457505?ie=UTF8 \r\n% _Two-Dimensional Signal and Image Processing_> by Lim.\r\n% This book contains the following one-dimensional DCT equation:\r\n% \r\n% $$C_x(k) = \\sum_{n=0}^{N-1} 2x(n) \\cos \\frac{\\pi}{2N} k(2n+1),\\ 0 \\leq k \r\n% \\leq N-1$$\r\n% \r\n% The original implementation of |dct2| used this formula.\r\n%\r\n% At roughly the same time as the first toolbox release, the\r\n% newly-standardized JPEG image compression method and format was becoming\r\n% popular.  The DCT is the mathematical heart of the JPEG algorithm. Here's\r\n% the 8-point one-dimensional DCT equation that appears in \r\n% <http:\/\/www.amazon.com\/gp\/product\/0442012721\/sr=8-1\/qid=1154107219\/ref=sr_1_1\/104-1319363-5457505?ie=UTF8 \r\n% _JPEG: Still Image Data Compression Standard_> by Pennebaker and Mitchell.\r\n% \r\n% $$S(u) = \\frac{C(u)}{2} \\sum_{x=0}^7 s(x) \r\n% \\cos[(2x+1)u\\pi\/16]$$\r\n% \r\n% where\r\n%\r\n% $$C(u) = \\left\\{ \\begin{array}{ll} 1\/\\sqrt{2} & \\qquad\\mbox{for}\\qquad u=0 \\\\\r\n% 1 & \\qquad\\mbox{for}\\qquad u>0 \\end{array} \\right.$$\r\n% \r\n% The variable name differences are trivial, but the scale factor \r\n% differences are very important!  The JPEG DCT is an orthonormal\r\n% transform, whereas the DCT defined in Lim's book is not.\r\n%\r\n% Users of version 1 began to contact tech support, wondering why |dct2|\r\n% was giving the \"wrong\" answer.  We realized that we would have to do\r\n% something, but what?  Changing the output of |dct2| would introduce an\r\n% incompatibility.  Not changing it would let user confusion and tech\r\n% support calls continue indefinitely.\r\n%\r\n% We eventually made a decision based on the fact that image compression is\r\n% the only widespread application of the DCT in image processing.\r\n% Therefore, it made no sense for our function to produce an answer\r\n% inconsistent with the standardized DCT-based image compression method.\r\n% So we changed the output of |dct2| in version 2 of the toolbox.  When no\r\n% complaints came in about the change, we breathed a sigh of relief.\r\n%\r\n% Today at The MathWorks we have processes in place to carefully evaluate\r\n% changes that might result in an incompatibility.  What options have the\r\n% developers considered?  What steps are being taken to mitigate possible \r\n% negative impacts of the change?  What is the benefit to the user of the\r\n% change?\r\n%\r\n% Whether we are doing a good job or not is up to you to judge.  How do you\r\n% think we are doing?  How has the evolution of MATLAB over the years\r\n% affected your work?  Have any good stories to tell?  Leave your comments\r\n% <https:\/\/blogs.mathworks.com\/steve\/?p=77#respond here>.\r\n\r\n\r\n\r\n##### SOURCE END ##### 77\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Software compatibility is a tricky subject. No one likes it when an incompatible change occurs in a software application they\r\n      rely on.  So why do we sometimes find it necessary to make an... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/08\/11\/discrete-cosine-transforms-jpeg-and-software-compatibility\/\">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":[204],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/77"}],"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=77"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":2200,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/77\/revisions\/2200"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}