JavaでSVGファイルをPNGファイルに変換する

こんにちは。
「[AndroidでSVGを画像としてImageViewに表示する](http://qiita.com/wakwak/items/af230c3161b9c7eff970)」と題した投稿で、Androidアプリ開発時にSVGファイルを画像として表示する、ということにトライしてみました。その際には、Android標準APIを利用する策と、サードパーティ製のライブラリを使う策の2つの方法を提示しました。

そこで今回は、その時にふと思いついたのが、”土俵をAndroidアプリではなく、普通にJavaでSVGファイルを画像ファイルに変換できないものだろうか？”という好奇心です。

# 今回用意したSVGファイル

Scalable Vector Graphicsは、画像をJPEGやPNGのようなビットマップデータではなく、ベクターデータの形式で表したものです。記法はXMLです。SVGの仕様はW3Cによって開発され、オープン標準として勧告されています。
実は私も詳しいことは分かっていません。『画像をXMLで表現したヤツ』という認識しかないです。

```xml:delorean.svg
<?xml version="1.0" ?><!DOCTYPE svg  PUBLIC '-//W3C//DTD SVG 1.1//EN'  'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'><svg height="512px" id="Layer_1" style="enable-background:new 0 0 512 512;" version="1.1" viewBox="0 0 512 512" width="512px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g><path d="M512.398,256.43c0,141.383-114.619,255.998-256.003,255.998   c-141.383,0-255.998-114.615-255.998-255.998c0-141.388,114.614-256.002,255.998-256.002   C397.779,0.427,512.398,115.042,512.398,256.43z" style="fill:#0277BD;"/><g><g><g><path d="M320.895,8.635c-20.604-5.348-42.22-8.208-64.499-8.208c-22.282,0-43.893,2.86-64.5,8.208      H320.895z" style="fill:#2B5791;"/></g><g><path d="M450.838,90.004H157.96V71.082h274.92c-17.261-16.439-36.797-30.487-58.115-41.629H138.028      C99.065,49.814,66.013,79.893,42.064,116.5h428.672C464.668,107.229,458.021,98.389,450.838,90.004z" style="fill:#0D47A1;"/></g><g><path d="M337.732,279.238v-9.462h174.322c0.231-4.419,0.344-8.871,0.344-13.347      c0-5.696-0.251-11.328-0.613-16.932H411.534v-11.351h99.276c-0.558-5.098-1.3-10.139-2.154-15.139H330.165v-15.14h175.427      c-1.652-7.057-3.574-14.001-5.798-20.817H352.872v-17.029h140.691c-5.089-12.502-11.091-24.531-18.004-35.956H37.239      C13.861,162.692,0.398,207.985,0.398,256.43c0,141.383,114.614,255.998,255.998,255.998      c133.69,0,243.394-102.498,254.944-233.189H337.732z" style="fill:#0D47A1;"/></g></g></g><path d="M189.824,151.541c1.437,0.506,1.806,1.226,1.994,1.853c0.947,3.306-2.068,9.782-5.766,15.645   c2.433,0.418,4.891,0.845,7.377,1.277c3.5-5.994,6.599-13.333,5.044-18.812c-0.887-3.097-3.094-5.334-6.372-6.481   c-11.834-4.136-43.575,9.411-56.82,15.488c2.86,0.464,7.161,1.174,12.486,2.056C164.237,155.659,183.294,149.28,189.824,151.541z" style="fill:#231F20;"/><path d="M333.688,202.2v-0.004c11.5,3.523,19.87,6.263,22.567,7.154c0.659-10.445,1.305-25.859,0.339-37.038   c-1.615-18.454-9.238-38.918-9.238-38.918s-71.249,37.089-93.653,47.971c4.383,0.873,8.444,1.708,12.057,2.493   C288.601,188.816,315.101,196.5,333.688,202.2z"/><path d="M92.545,168.37v46.978l-89.006,1.026c-1.597,10.162-2.549,20.534-2.913,31.054H101.25   c4.689-9.317,8.918-19.35,9.304-24.731c0.313-4.336,0.471-24.405,0.55-43.7c-2.18-7.73-3.28-14.703-3.823-19.499l-87.323-1.3   c-1.342,3.227-2.642,6.481-3.854,9.772L92.545,168.37z" style="fill:#D1D1D1;"/><path d="M155.456,228.7c-3.062-0.938-6.077-1.982-8.988-3.268c-17.846-7.897-27.674-24.174-33.094-39.318   c-0.086,16.899-0.239,32.851-0.52,36.75c-0.411,5.654-4.505,15.492-9.011,24.563h31.848   C141.055,239.303,147.942,233.161,155.456,228.7z" style="fill:#D1D1D1;"/><path d="M276.726,247.428c-5.135-2.916-10.854-5.469-17.299-7.475c-14.944-4.656-32.964-5.009-50.396-5.353   c-8.665-0.172-17.439-0.372-26.068-1.059c-10.037,1.328-20.775,5.687-29.22,13.886H276.726z" style="fill:#D1D1D1;"/><path d="M135.282,160.511c-2.565-0.423-4-0.655-4-0.655l-0.727-0.009c0.121,0.85,0.25,1.722,0.408,2.679   C131.983,162.038,133.473,161.337,135.282,160.511z" style="fill:#D1D1D1;"/><path d="M131.983,167.697c0.104,0.473,0.209,0.947,0.323,1.435l0.422,0.093L131.983,167.697z" style="fill:#D1D1D1;"/><path d="M131.983,167.697l0.745,1.527l0.181,0.037c4.318-2.094,9.445-4.424,14.858-6.695   c-5.325-0.882-9.626-1.592-12.486-2.056c-1.809,0.826-3.299,1.527-4.318,2.015C131.233,164.123,131.584,165.877,131.983,167.697z" style="fill:#D1D1D1;"/><path d="M265.76,183.858c-3.612-0.785-7.674-1.621-12.057-2.493c-17.182-3.426-39.441-7.433-60.274-11.049   c-2.486-0.432-4.944-0.859-7.377-1.277c-14.759-2.539-28.333-4.819-38.285-6.472c-5.413,2.271-10.541,4.601-14.858,6.695   l178.495,38.356c0,0,6.444,1.346,5.377,8.264c-0.892,5.789-7.112,5.385-7.112,5.385l-43.965-3.454   c0.186,0.056,0.381,0.098,0.566,0.153c20.135,6.263,35.246,17.01,46.564,29.461h20.854v-33.881V202.2   C315.101,196.5,288.601,188.816,265.76,183.858z" style="fill:#D1D1D1;"/><path d="M280.374,308.876c0,1.746-0.116,3.464-0.26,5.172h38.802c-1.406-10.91-6.476-34.387-24.493-52.967   h-36.927C271.442,272.344,280.374,289.559,280.374,308.876z" style="fill:#B6B8B7;"/><path d="M128.963,261.081H96.447c-0.106,0.182-0.229,0.404-0.334,0.576   c-5.701,9.494-36.291,47.465-47.005,52.391h65.735c4.766-9.972,8.319-22.684,10.439-37.985   C126.036,270.612,127.324,265.659,128.963,261.081z" style="fill:#B6B8B7;"/><path d="M332.407,261.081h-9.016c3.565,5.465,6.556,11.045,9.016,16.598V261.081z" style="fill:#B6B8B7;"/><path d="M180.388,261.081h-36.42c-2.352,4.894-4.116,10.469-5.002,16.866   c-1.924,13.956-4.96,25.957-9.009,36.101h27.818c-0.142-1.708-0.26-3.426-0.26-5.172   C157.515,289.559,166.447,272.344,180.388,261.081z" style="fill:#B6B8B7;"/><path d="M6.812,313.277c4.153,0.037,8.136,0.056,11.853,0.056c0.007,0,0.007,0,0.007,0   c22.372,0,27.7-0.664,28.879-1.063c9.12-3.041,39.77-40.979,46.188-51.188H0.516C0.836,278.992,2.993,296.448,6.812,313.277z" style="fill:#B6B8B7;"/><rect height="41.175" style="fill:#251F1F;" width="0" x="298.819" y="298.029"/><path d="M280.114,314.048c-0.251,3.041-0.743,6.008-1.421,8.904l40.251-8.677   c-0.01-0.074-0.019-0.153-0.028-0.228H280.114z" style="fill:#251F1F;"/><path d="M92.545,168.37l-76.441-0.399c-5.703,15.482-9.938,31.676-12.565,48.403l89.006-1.026V168.37z" style="fill:#251F1F;"/><path d="M135.69,247.428h18.053c8.445-8.199,19.183-12.558,29.22-13.886   c-9.482-0.757-18.763-2.164-27.507-4.842C147.942,233.161,141.055,239.303,135.69,247.428z" style="fill:#231F20;"/><path d="M157.774,314.048h-27.818c4.048-10.144,7.084-22.145,9.009-36.101   c0.887-6.397,2.651-11.973,5.002-16.866h-15.004c-1.639,4.578-2.927,9.531-3.682,14.981c-2.12,15.302-5.673,28.014-10.439,37.985   H49.107c-0.27,0.125-0.583,0.325-0.827,0.404c-0.873,0.292-3.535,1.184-29.61,1.184c0,0,0,0-0.007,0   c-3.517,0-7.329-0.019-11.321-0.051c1.639,6.922,3.547,13.736,5.733,20.427h82.377h21.775h19.845l25.696-2.34   C160.077,327.585,158.355,320.983,157.774,314.048z" style="fill:#231F20;"/><path d="M112.854,222.864c0.281-3.9,0.434-19.852,0.52-36.75c-0.863-2.414-1.616-4.795-2.27-7.117   c-0.079,19.294-0.237,39.364-0.55,43.7c-0.385,5.381-4.615,15.414-9.304,24.731h2.593   C108.348,238.356,112.443,228.519,112.854,222.864z" style="fill:#231F20;"/><path d="M96.113,261.657c0.104-0.172,0.228-0.395,0.334-0.576h-2.708   C87.32,271.29,56.67,309.229,47.55,312.27c-1.179,0.399-6.507,1.063-28.879,1.063c0,0,0,0-0.007,0c-3.716,0-7.7-0.019-11.853-0.056   c0.058,0.26,0.119,0.516,0.179,0.771h42.117C59.822,309.122,90.412,271.151,96.113,261.657z" style="fill:#231F20;"/><path d="M7.343,315.585c3.992,0.032,7.804,0.051,11.321,0.051c0.007,0,0.007,0,0.007,0   c26.075,0,28.737-0.892,29.61-1.184c0.244-0.079,0.557-0.279,0.827-0.404H6.99C7.109,314.559,7.223,315.074,7.343,315.585z" style="fill:#231F20;"/><path d="M0.451,254.34c-0.004,0.585-0.044,1.161-0.044,1.746H96.67c0.311-0.552,0.634-1.17,0.949-1.746   H0.451z" style="fill:#231F20;"/><path d="M250.244,256.086h38.867c-0.687-0.589-1.393-1.17-2.117-1.746H247.15   C248.206,254.888,249.228,255.482,250.244,256.086z" style="fill:#231F20;"/><path d="M100.242,254.34c-0.32,0.59-0.636,1.179-0.952,1.746h31.676c0.27-0.594,0.555-1.165,0.84-1.746   H100.242z" style="fill:#231F20;"/><path d="M319.942,256.086h13.746v-1.746h-15.074C319.056,254.921,319.511,255.501,319.942,256.086z" style="fill:#231F20;"/><path d="M147.868,254.34c-0.395,0.571-0.78,1.151-1.154,1.746h40.924c1.019-0.604,2.041-1.198,3.094-1.746   H147.868z" style="fill:#231F20;"/><path d="M323.392,261.081h9.016v-0.552h1.281v-4.443h-13.746C321.154,257.738,322.296,259.4,323.392,261.081   z" style="fill:#231F20;"/><path d="M99.291,256.086c-0.996,1.806-1.955,3.496-2.844,4.995h32.517c0.617-1.722,1.276-3.394,2.003-4.995   H99.291z" style="fill:#231F20;"/><path d="M289.111,256.086h-38.867c2.535,1.504,4.972,3.152,7.252,4.995h36.927   C292.761,259.368,291.001,257.701,289.111,256.086z" style="fill:#231F20;"/><path d="M187.638,256.086h-40.924c-0.994,1.569-1.903,3.245-2.746,4.995h36.42   C182.67,259.238,185.105,257.59,187.638,256.086z" style="fill:#231F20;"/><path d="M96.67,256.086H0.407c0,0.116-0.009,0.228-0.009,0.344c0,1.56,0.09,3.097,0.118,4.651h93.222   c0.121-0.19,0.292-0.44,0.395-0.612C94.94,259.122,95.795,257.646,96.67,256.086z" style="fill:#231F20;"/><path d="M131.807,254.34c-0.286,0.581-0.571,1.151-0.84,1.746h15.748c0.374-0.594,0.759-1.174,1.154-1.746   H131.807z" style="fill:#231F20;"/><path d="M130.966,256.086c-0.727,1.602-1.386,3.273-2.003,4.995h15.004c0.843-1.75,1.752-3.426,2.746-4.995   H130.966z" style="fill:#231F20;"/><path d="M97.619,254.34c-0.315,0.576-0.638,1.193-0.949,1.746h2.621c0.315-0.566,0.631-1.156,0.952-1.746   H97.619z" style="fill:#231F20;"/><path d="M96.67,256.086c-0.875,1.56-1.729,3.036-2.537,4.383c-0.102,0.172-0.274,0.422-0.395,0.612h2.708   c0.889-1.499,1.848-3.189,2.844-4.995H96.67z" style="fill:#231F20;"/><path d="M153.743,247.428H135.69h-31.848h-2.593H0.625c-0.077,2.242-0.151,4.485-0.169,6.741h97.256h2.623   h31.553h16.105h43.097c8.366-4.271,17.811-6.727,27.848-6.727c10.04,0,19.487,2.456,27.855,6.727h39.998   c-3.055-2.414-6.416-4.666-10.064-6.741H153.743z" style="fill:#F6F6F6;"/><path d="M312.835,247.428c2.001,2.201,3.886,4.443,5.646,6.741h15.208v-6.741H312.835z" style="fill:#F6F6F6;"/><path d="M97.619,254.34c0.03-0.056,0.061-0.116,0.093-0.172H0.456   c-0.002,0.056-0.005,0.116-0.005,0.172H97.619z" style="fill:#FAFAFA;fill-opacity:0.78;"/><path d="M131.807,254.34c0.028-0.056,0.053-0.116,0.081-0.172h-31.553   c-0.03,0.061-0.063,0.112-0.093,0.172H131.807z" style="fill:#FAFAFA;fill-opacity:0.78;"/><path d="M286.994,254.34c-0.069-0.056-0.135-0.116-0.204-0.172h-39.998   c0.119,0.061,0.242,0.112,0.357,0.172H286.994z" style="fill:#FAFAFA;fill-opacity:0.78;"/><path d="M190.732,254.34c0.118-0.06,0.239-0.111,0.357-0.172h-43.097   c-0.04,0.061-0.083,0.112-0.125,0.172H190.732z" style="fill:#FAFAFA;fill-opacity:0.78;"/><path d="M318.614,254.34h15.074v-0.172H318.48   C318.526,254.224,318.573,254.285,318.614,254.34z" style="fill:#FAFAFA;fill-opacity:0.78;"/><path d="M147.993,254.168h-16.105c-0.028,0.056-0.053,0.116-0.081,0.172h16.061   C147.909,254.28,147.953,254.229,147.993,254.168z" style="fill:#FAFAFA;fill-opacity:0.78;"/><path d="M100.242,254.34c0.03-0.06,0.063-0.111,0.093-0.172h-2.623   c-0.032,0.056-0.063,0.116-0.093,0.172H100.242z" style="fill:#FAFAFA;fill-opacity:0.78;"/><polygon points="277.314,323.249 278.693,322.952 278.693,322.952  " style="fill:#251F1F;"/><path d="M257.496,261.081h-77.108c-13.941,11.263-22.874,28.478-22.874,47.795   c0,1.746,0.118,3.464,0.26,5.172c0.581,6.936,2.303,13.537,4.996,19.624c9.533,21.564,31.077,36.639,56.167,36.639   c29.078,0,53.382-20.228,59.756-47.358c0.678-2.896,1.17-5.863,1.421-8.904c0.144-1.708,0.26-3.426,0.26-5.172   C280.374,289.559,271.442,272.344,257.496,261.081z M260.969,324.735c-6.421,16.991-22.792,29.094-42.031,29.094   c-16.33,0-30.583-8.741-38.457-21.769c-3.25-5.376-5.425-11.472-6.174-18.013c-0.195-1.699-0.32-3.422-0.32-5.172   c0-24.823,20.127-44.958,44.951-44.958c24.833,0,44.965,20.135,44.965,44.958c0,1.75-0.125,3.473-0.32,5.172   C263.155,317.771,262.25,321.346,260.969,324.735z" style="fill:#251F1F;"/><path d="M247.15,254.34h-56.418c-1.054,0.548-2.075,1.142-3.094,1.746h62.606   C249.228,255.482,248.206,254.888,247.15,254.34z" style="fill:#251F1F;"/><path d="M187.638,256.086c-2.533,1.504-4.968,3.152-7.25,4.995h77.108c-2.28-1.843-4.717-3.491-7.252-4.995   H187.638z" style="fill:#251F1F;"/><path d="M218.938,247.441c-10.037,0-19.482,2.456-27.848,6.727h55.703   C238.424,249.897,228.977,247.441,218.938,247.441z" style="fill:#251F1F;"/><path d="M247.15,254.34c-0.116-0.06-0.239-0.111-0.357-0.172h-55.703c-0.119,0.061-0.239,0.112-0.357,0.172   H247.15z" style="fill:#251F1F;"/><path d="M218.938,339.419c-8.408,0-16.017-3.398-21.537-8.899l-16.92,1.541   c7.874,13.027,22.126,21.769,38.457,21.769c19.239,0,35.61-12.103,42.031-29.094l-16.95,1.546   C238.503,334.215,229.332,339.419,218.938,339.419z" style="fill:#C2C2C2;"/><path d="M188.404,308.876c0-16.866,13.667-30.543,30.534-30.543s30.543,13.677,30.543,30.543   c0,1.765-0.183,3.486-0.471,5.172h14.573c0.195-1.699,0.32-3.422,0.32-5.172c0-24.823-20.132-44.958-44.965-44.958   c-24.824,0-44.951,20.135-44.951,44.958c0,1.75,0.125,3.473,0.32,5.172h14.568C188.587,312.362,188.404,310.641,188.404,308.876z" style="fill:#C2C2C2;"/><path d="M188.875,314.048h-14.568c0.75,6.541,2.925,12.637,6.174,18.013l16.92-1.541   C193.004,326.146,189.963,320.432,188.875,314.048z" style="fill:#C2C2C2;"/><path d="M249.009,314.048c-0.766,4.484-2.493,8.64-4.991,12.233l16.95-1.546   c1.281-3.39,2.187-6.964,2.613-10.688H249.009z" style="fill:#C2C2C2;"/><path d="M218.938,278.333c-16.866,0-30.534,13.677-30.534,30.543c0,1.765,0.183,3.486,0.471,5.172   c1.088,6.384,4.129,12.099,8.526,16.472c5.52,5.501,13.129,8.899,21.537,8.899c10.395,0,19.566-5.204,25.081-13.138   c2.498-3.594,4.225-7.749,4.991-12.233c0.288-1.686,0.471-3.407,0.471-5.172C249.48,292.01,235.804,278.333,218.938,278.333z" style="fill:#EFEFEF;"/><path d="M152.178,202.391v-12.985c0-4.995,3.937-9.034,8.793-9.034c4.863,0,8.797,4.039,8.797,9.034v15.947   c0,1.105-0.221,2.149-0.573,3.129c2.358,0.488,4.786,0.892,7.272,1.226v-15.696c0-4.991,3.944-9.034,8.791-9.034   c4.863,0,8.8,4.043,8.8,9.034v15.947c0,0.409-0.065,0.798-0.116,1.188c2.245,0.093,4.521,0.167,6.817,0.228v-12.753   c0-4.986,3.935-9.039,8.791-9.039c4.861,0,8.797,4.053,8.797,9.039v13.152c16.019,0.418,32.235,1.425,47.356,6.04l43.965,3.454   c0,0,6.221,0.404,7.112-5.385c1.067-6.917-5.377-8.264-5.377-8.264l-178.495-38.356c-0.042,0.023-0.09,0.046-0.132,0.065   l-0.049-0.102l-0.422-0.093C135.031,180.724,140.662,195.186,152.178,202.391z"/><path d="M132.909,169.262l-0.181-0.037l0.049,0.102C132.819,169.308,132.868,169.285,132.909,169.262z"/><path d="M169.769,189.405c0-4.995-3.935-9.034-8.797-9.034c-4.856,0-8.793,4.039-8.793,9.034v12.985   c1.156,0.724,2.337,1.416,3.616,1.982c4.125,1.824,8.638,3.12,13.401,4.108c0.353-0.979,0.573-2.024,0.573-3.129V189.405z" style="fill:#0776C7;"/><path d="M194.058,194.011c0-4.991-3.937-9.034-8.8-9.034c-4.847,0-8.791,4.043-8.791,9.034v15.696   c5.566,0.752,11.409,1.188,17.475,1.439c0.051-0.39,0.116-0.78,0.116-1.188V194.011z" style="fill:#0776C7;"/><path d="M209.55,189.582c-4.856,0-8.791,4.053-8.791,9.039v12.753c2.883,0.079,5.787,0.139,8.728,0.2   c2.941,0.056,5.898,0.125,8.86,0.199v-13.152C218.348,193.635,214.411,189.582,209.55,189.582z" style="fill:#0776C7;"/><path d="M106.786,153.774c-0.021-0.414-0.032-0.71-0.037-0.836l0.107-0.004   c-0.462-0.701-0.798-1.495-0.798-2.4v-0.84H93.262c-2.247,0-4.062,1.834-4.062,4.081c0,2.237,1.815,4.062,4.062,4.062h13.842   c-0.174-1.769-0.267-3.129-0.313-3.969C106.788,153.825,106.788,153.816,106.786,153.774z" style="fill:#949494;"/><path d="M129.885,153.774c0-0.037-0.021-0.07-0.021-0.107c0.005,0.06,0.005,0.098,0.012,0.158   C129.875,153.807,129.885,153.793,129.885,153.774z" style="fill:#949494;"/><path d="M114.302,152.688l15.26-0.506c-0.618-1.458-2.057-2.488-3.74-2.488h-10.887v0.84   C114.936,151.332,114.669,152.042,114.302,152.688z" style="fill:#949494;"/><path d="M110.491,140.353c2.077,0,3.732,1.449,4.227,3.366h5.056c2.781,0,5.035-2.247,5.035-5.033   c0-2.795-2.254-5.037-5.035-5.037h-19.15c-2.781,0-5.037,2.242-5.037,5.037c0,2.786,2.256,5.033,5.037,5.033h5.652   C106.767,141.801,108.422,140.353,110.491,140.353z" style="fill:#949494;"/><path d="M114.936,144.805c0-0.385-0.127-0.729-0.218-1.086h-8.442c-0.093,0.357-0.218,0.701-0.218,1.086   v4.889h8.878V144.805z" style="fill:#949494;"/><path d="M106.855,152.934l7.447-0.246c0.367-0.646,0.633-1.356,0.633-2.154v-0.84h-8.878v0.84   C106.057,151.439,106.394,152.233,106.855,152.934z" style="fill:#949494;"/><path d="M110.491,140.353c-2.068,0-3.724,1.449-4.215,3.366h8.442   C114.223,141.801,112.568,140.353,110.491,140.353z" style="fill:#949494;"/><path d="M357.269,209.684c0,0-0.413-0.139-1.013-0.334l0,0C356.896,209.563,357.269,209.684,357.269,209.684   z" style="fill:#D1D1D1;"/><path d="M333.688,202.2c11.402,3.496,19.801,6.24,22.567,7.149l0,0c-2.697-0.891-11.067-3.63-22.567-7.154   V202.2z" style="fill:#D1D1D1;"/><path d="M357.269,209.684c0,0-0.372-0.121-1.013-0.334c-2.767-0.91-11.165-3.653-22.567-7.149v11.346v33.881   h37.596l1.049,2.813L357.269,209.684z" style="fill:#D1D1D1;"/><path d="M357.269,209.684c0,0-0.372-0.121-1.013-0.334c-2.767-0.91-11.165-3.653-22.567-7.149v11.346v33.881   h37.596l1.049,2.813L357.269,209.684z" style="fill:#D1D1D1;"/><g><path d="M341.154,306.555l30.701-25.58h-37.999C337.537,290.009,339.854,298.783,341.154,306.555z" style="fill:#D1D1D1;"/><polygon points="372.333,250.241 371.284,247.428 333.688,247.428 333.688,253.449 333.688,254.168     333.688,256.086 333.688,256.978 333.688,260.529 378.137,260.529 378.137,256.978 378.137,256.086 378.137,254.168     378.137,253.449 378.137,251.406   " style="fill:#D1D1D1;"/></g><g><g><g><path d="M341.154,306.555l30.701-25.58h-37.999C337.537,290.009,339.854,298.783,341.154,306.555z" style="fill:#D1D1D1;"/></g><g><polygon points="372.333,250.241 371.284,247.428 333.688,247.428 333.688,253.449 333.688,254.168       333.688,256.086 333.688,256.978 333.688,260.529 378.137,260.529 378.137,256.978 378.137,256.086 378.137,254.168       378.137,253.449 378.137,251.406     " style="fill:#D1D1D1;"/></g></g></g><rect height="0.915" style="fill:#D1D1D1;" width="44.448" x="333.688" y="256.36"/><polygon points="378.137,275.742 371.855,280.975 379.32,280.975 379.32,260.529 378.137,260.529    378.137,261.081  " style="fill:#251F1F;"/><path d="M332.407,277.679c0.418,0.951,0.887,1.907,1.281,2.854v-19.452h-1.281V277.679z" style="fill:#251F1F;"/><rect height="0.552" style="fill:#251F1F;" width="1.281" x="332.407" y="260.529"/><path d="M378.137,261.081h-44.448v19.452c0.056,0.148,0.103,0.293,0.168,0.441h37.999l6.281-5.232V261.081z" style="fill:#251F1F;"/><rect height="0.552" style="fill:#251F1F;" width="44.448" x="333.688" y="260.529"/><path d="M107.111,157.836h-0.007c0.051,0.52,0.109,1.072,0.176,1.662h0.016   C107.229,158.914,107.162,158.352,107.111,157.836z" style="fill:#949494;"/><path d="M320.003,314.048h0.009l-1.067,0.232c0.408,3.249,0.501,5.357,0.511,5.784l13.063-0.315   c-0.324-2.559-0.816-5.293-1.393-8.097L320.003,314.048z" style="fill:#949494;"/><path d="M182.962,233.542c8.628,0.687,17.403,0.887,26.068,1.059c17.433,0.343,35.453,0.696,50.396,5.353   c6.444,2.006,12.164,4.559,17.299,7.475h17.391c-9.936-8.449-22.089-15.599-37.055-20.25c-13.284-4.132-27.444-5.432-41.573-5.984   c-1.567,1.476-3.64,2.4-5.938,2.4c-2.475,0-4.701-1.058-6.3-2.744c-0.989-0.023-1.985-0.046-2.971-0.065   c-6.344-0.125-12.554-0.269-18.57-0.58l1.665,13.273C183.239,233.496,183.1,233.523,182.962,233.542z" style="fill:#949494;"/><path d="M111.159,161.699l-3.584,0.116c-0.116-0.845-0.195-1.569-0.279-2.316h-0.016   c0.543,4.795,1.644,11.769,3.823,19.499C111.129,172.98,111.147,167.066,111.159,161.699z" style="fill:#949494;"/><path d="M146.468,225.432c2.911,1.286,5.926,2.331,8.988,3.268c7.707-4.578,16.052-7.344,24.113-8.607   c-12.258-0.734-23.568-2.345-32.985-6.504c-24.257-10.733-26.007-51.787-26.031-52.196l-7.094,0.232   c-0.016,7.502-0.044,16.096-0.086,24.489C118.794,201.258,128.622,217.535,146.468,225.432z" style="fill:#949494;"/><path d="M294.423,261.081c18.018,18.58,23.087,42.057,24.493,52.967h1.087l11.123-2.396   c-3.175-15.492-10.315-34.174-23.616-50.571H294.423z" style="fill:#949494;"/><polygon points="320.003,314.048 318.944,314.275 318.944,314.28 320.012,314.048  " style="fill:#949494;"/><path d="M318.944,314.275l1.059-0.228h-1.087C318.926,314.122,318.935,314.201,318.944,314.275z" style="fill:#949494;"/><path d="M182.962,233.542c0.137-0.019,0.276-0.046,0.411-0.065l-1.665-13.273   c-0.712-0.037-1.435-0.065-2.14-0.111c-8.062,1.263-16.407,4.029-24.113,8.607C164.2,231.378,173.48,232.785,182.962,233.542z" style="fill:#949494;"/><path d="M113.459,161.625l-2.3,0.075c-0.012,5.367-0.03,11.281-0.056,17.298   c0.654,2.321,1.407,4.703,2.27,7.117C113.416,177.72,113.443,169.127,113.459,161.625z" style="fill:#949494;"/><path d="M303.253,256.086c-0.548-0.599-1.17-1.156-1.736-1.746h-14.522c0.725,0.576,1.431,1.156,2.117,1.746   H303.253z" style="fill:#949494;"/><path d="M289.111,256.086c1.89,1.615,3.649,3.282,5.312,4.995h13.087c-1.369-1.685-2.753-3.365-4.257-4.995   H289.111z" style="fill:#949494;"/><path d="M301.345,254.168c-2.261-2.331-4.675-4.573-7.229-6.741h-17.391   c3.648,2.075,7.01,4.327,10.064,6.741H301.345z" style="fill:#949494;"/><path d="M286.994,254.34h14.522c-0.056-0.056-0.116-0.116-0.172-0.172H286.79   C286.859,254.224,286.925,254.285,286.994,254.34z" style="fill:#949494;"/><path d="M209.55,223.593c2.298,0,4.371-0.924,5.938-2.4c-4.093-0.163-8.173-0.26-12.238-0.344   C204.85,222.535,207.076,223.593,209.55,223.593z" style="fill:#949494;"/><path d="M106.786,153.774c-0.021-0.409-0.032-0.715-0.037-0.836   C106.753,153.064,106.765,153.361,106.786,153.774z" style="fill:#949494;"/><path d="M106.791,153.867c0.046,0.84,0.139,2.2,0.313,3.969h0.007   C106.937,156.1,106.837,154.703,106.791,153.867z" style="fill:#949494;"/><polygon points="332.519,319.763 342.491,319.512 332.519,319.749  " style="fill:#F6F6F6;"/><path d="M106.791,153.867c-0.002-0.037-0.002-0.061-0.005-0.093   C106.788,153.816,106.788,153.825,106.791,153.867z" style="fill:#F6F6F6;"/><path d="M129.766,152.177l-0.204,0.004c0.195,0.46,0.288,0.966,0.302,1.486   C129.799,152.804,129.769,152.219,129.766,152.177z" style="fill:#F6F6F6;"/><polygon points="113.464,159.586 112.083,159.568 113.464,159.591  " style="fill:#F6F6F6;"/><path d="M318.614,254.34c-0.041-0.056-0.088-0.116-0.134-0.172h-17.136c0.056,0.056,0.116,0.116,0.172,0.172   H318.614z" style="fill:#F6F6F6;"/><polygon points="336.409,310.51 333.688,311.1 333.688,311.1  " style="fill:#F6F6F6;"/><g><path d="M341.154,306.555c-1.3-7.771-3.617-16.546-7.298-25.58h-0.168h-1.281v-3.296    c-2.46-5.553-5.45-11.133-9.016-16.598c-1.096-1.681-2.237-3.343-3.449-4.995c-0.432-0.585-0.887-1.165-1.328-1.746h-17.098    c0.566,0.59,1.188,1.147,1.736,1.746c1.504,1.63,2.888,3.311,4.257,4.995c13.301,16.397,20.441,35.079,23.616,50.571    c0.576,2.804,1.068,5.538,1.393,8.097l9.973-0.237C342.398,315.715,341.952,311.309,341.154,306.555z" style="fill:#F6F6F6;"/><path d="M312.835,247.428c-11.318-12.451-26.43-23.199-46.564-29.461c-0.186-0.056-0.381-0.098-0.566-0.153    c-15.121-4.615-31.337-5.622-47.356-6.04c-2.962-0.074-5.919-0.144-8.86-0.199c-2.941-0.061-5.845-0.121-8.728-0.2    c-2.296-0.061-4.573-0.135-6.817-0.228c-6.066-0.251-11.908-0.687-17.475-1.439c-2.486-0.334-4.914-0.738-7.272-1.226    c-4.763-0.989-9.276-2.284-13.401-4.108c-1.279-0.566-2.46-1.258-3.616-1.982c-11.516-7.205-17.147-21.667-19.873-33.259    c-0.114-0.488-0.218-0.961-0.323-1.435c-0.399-1.82-0.75-3.575-1.019-5.172c-0.158-0.957-0.288-1.829-0.408-2.679    c-0.369-2.614-0.578-4.694-0.68-6.021c-0.007-0.06-0.007-0.098-0.012-0.158c-0.014-0.52-0.107-1.026-0.302-1.486l-15.26,0.506    l-7.447,0.246l-0.107,0.004c0.005,0.121,0.017,0.427,0.037,0.836c0.002,0.032,0.002,0.056,0.005,0.093    c0.046,0.835,0.146,2.233,0.32,3.969c0.051,0.515,0.118,1.077,0.186,1.662c0.083,0.748,0.163,1.472,0.279,2.316l3.584-0.116    l2.3-0.075l7.094-0.232c0.023,0.409,1.773,41.462,26.031,52.196c9.417,4.16,20.727,5.771,32.985,6.504    c0.706,0.046,1.428,0.074,2.14,0.111c6.017,0.311,12.226,0.455,18.57,0.58c0.986,0.019,1.982,0.042,2.971,0.065    c4.064,0.083,8.145,0.181,12.238,0.344c14.129,0.552,28.289,1.853,41.573,5.984c14.966,4.651,27.119,11.801,37.055,20.25    c2.554,2.168,4.968,4.41,7.229,6.741h17.136C316.721,251.871,314.836,249.628,312.835,247.428z M113.464,159.591l-1.381-0.023    l1.381,0.019V159.591z" style="fill:#F6F6F6;"/></g><path d="M332.407,280.975h1.281v-0.441c-0.395-0.947-0.863-1.903-1.281-2.854V280.975z" style="fill:#F6F6F6;"/><path d="M333.856,280.975c-0.065-0.148-0.112-0.293-0.168-0.441v0.441H333.856z" style="fill:#F6F6F6;"/><g><path d="M496.725,302.817c-7.943-10.524,0.976-25.297,0.976-25.297s-15.864,9.248-18.491,23.352    c-2.628,14.104-1.686,23.334-15.293,30.214c-9.002,4.55-17.321,2.762-22.66-3.937c-5.339-6.699,6.146-15.558,5.841-15.571    c-16.807-0.669-27.289,16.421-36.003,22.382c-9.74,6.666-14.796,3.449-16.467-4.438c-2.345-11.105,17.437-33.515,17.437-33.515    s-23.063,4.735-42.813,30.167c-6.352,8.181-26.546,22.901-26.277,0.976c0.14-10.905,5.984-19.536,5.841-19.467    c-24.095,11.519-30.391,42.104-44.764,45.738c-14.363,3.635-17.414,4.317-22.377,1.945c-4.967-2.372,4.912-17.038,4.731-16.922    c-17.99,11.421-14.917,17.367-24.192,22.762c-9.281,5.396-13.9,5.404-13.9,5.404h239.21c9.169-19.201,15.984-39.735,20.148-61.221    C503.308,306.485,499.246,306.16,496.725,302.817z" style="fill:#FF9100;"/><path d="M494.181,308.468c0,0-11.736,4.345-18.69,37.813c-3.584,17.261-34.132,12.251-36.072,10.432    c-13.473-12.604-4.912-24.769-5.219-24.772c-16.806-0.209-16.949,15.645-33.899,26.509c-8.385,5.376-19.063,1.936-20.863-0.432    c-8.255-10.864,6.086-30.423,6.086-30.423s-11.917,5.747-24.341,26.077c-4.777,7.822-22.451,9.1-25.641,3.041    c-4.345-8.26,4.489-16.537,4.351-16.514c-24.095,3.593-19.731,22.465-34.1,23.598c-14.368,1.138-17.414,1.352-22.382,0.608    c-4.967-0.743-5.079-9.531-5.236-9.429c-6.087,3.908-4.949,9.568-14.225,11.253c-9.278,1.681-13.897,1.686-13.897,1.686h236.85    c3.478-7.178,6.61-14.545,9.415-22.075C491.284,337.316,494.181,308.468,494.181,308.468z" style="fill:#FFFF00;"/></g><path d="M25.333,366.731c41.207,86.165,129.178,145.696,231.063,145.696s189.86-59.531,231.072-145.696   H25.333z" style="fill:#212830;"/><path d="M440.104,378.073c-13.473,12.604-4.911,24.768-5.214,24.772   c-16.811,0.208-16.949-15.646-33.904-26.509c-8.384-5.376-19.057-1.937-20.858,0.432c-8.259,10.863,6.086,30.422,6.086,30.422   s-11.922-5.747-24.34-26.077c-4.777-7.822-22.456-9.099-25.641-3.04c-4.346,8.259,4.489,16.536,4.345,16.513   c-24.095-3.593-19.73-22.465-34.095-23.598c-14.373-1.138-17.418-1.351-22.386-0.608c-4.963,0.743-5.079,9.531-5.237,9.429   c-6.081-3.908-4.944-9.567-14.225-11.253c-0.794-0.144-1.504-0.26-2.232-0.381h-13.401c0,0,4.619,0.01,13.897,5.404   c9.276,5.395,6.207,11.342,24.197,22.762c0.181,0.116-9.703-14.55-4.735-16.922s8.013-1.689,22.382,1.945   c14.368,3.635,20.663,34.22,44.758,45.738c0.148,0.069-5.7-8.561-5.835-19.466c-0.27-21.927,19.921-7.205,26.271,0.975   c19.754,25.432,42.818,30.167,42.818,30.167s-19.782-22.409-17.438-33.515c1.667-7.888,6.727-11.104,16.467-4.438   c8.71,5.961,19.192,23.05,35.998,22.381c0.307-0.014-11.175-8.871-5.836-15.57c5.34-6.699,13.654-8.487,22.66-3.938   c0.298,0.153,0.558,0.312,0.846,0.469c3.719-5.255,7.232-10.659,10.552-16.192C471.869,371.393,442.026,376.271,440.104,378.073z" style="fill:#A1691F;"/><path d="M262.403,368.175h224.34c0.214-0.437,0.441-0.868,0.65-1.305H250.741   C250.741,366.87,254.65,366.889,262.403,368.175z" style="fill:#B56300;"/><path d="M262.403,368.175c0.729,0.121,1.438,0.237,2.232,0.381c9.28,1.686,8.144,7.345,14.225,11.253   c0.158,0.103,0.274-8.686,5.237-9.429c4.968-0.742,8.013-0.529,22.386,0.608c14.364,1.133,10,20.005,34.095,23.598   c0.145,0.023-8.69-8.254-4.345-16.513c3.185-6.059,20.863-4.782,25.641,3.04c12.418,20.33,24.34,26.077,24.34,26.077   s-14.345-19.559-6.086-30.422c1.802-2.368,12.475-5.808,20.858-0.432c16.955,10.863,17.094,26.717,33.904,26.509   c0.303-0.005-8.259-12.169,5.214-24.772c1.922-1.802,31.765-6.681,35.9,9.902c3.858-6.426,7.447-13.027,10.738-19.801H262.403z" style="fill:#B56300;"/><path d="M82.587,444.35c45.645,42.247,106.706,68.078,173.809,68.078   c67.104,0,128.166-25.831,173.812-68.078H82.587z" style="fill:#030A17;"/><path d="M482.18,377.093c1.829-3.412,3.607-6.856,5.288-10.361H25.333c1.676,3.505,3.456,6.949,5.285,10.361   H482.18z" style="fill:#344360;"/></g></svg>
```
横一直線に長いですね。これじゃよくわからないですね。ということで、こういう画像です。あ、元々は[FASTICON](http://fasticon.com/)から頂戴いたしました。

![delorean.png](https://qiita-image-store.s3.amazonaws.com/0/100612/d38fd8fa-16a3-3502-0210-e74e367c71ed.png)

ファイル名からお察しいただけるかと思いますが、映画『バック・トゥ・ザ・フューチャー』シリーズに登場する車型タイムマシンです。カッコいい。

上記のSVGが長いのでわかりにくいと思いますが、縦横が512pxで設定されているので、この画像も512pxです。

```xml:縦横ピクセルサイズの指定しているところだけ抜粋
style="enable-background:new 0 0 512 512;" version="1.1" viewBox="0 0 512 512" width="512px" height="512px"
```
このSVGファイルをPNGファイルに変換するプログラムを書きたいと思います。というか、この画像が、そのプログラムで生成したPNGファイルです。

# 利用するライブラリ

- [The Apache™ Batik Project](https://xmlgraphics.apache.org/batik/)
    - [The Apache™ XML Graphics Project](https://xmlgraphics.apache.org/commons/)
    - [The Apache™ XML Commons projectのExternal Components](http://xerces.apache.org/xml-commons/components/external/index.html)

すべてApacheが提供してくれているライブラリです。 
3つ掲載しましたが、BatikがXML GraphicsとXML Externalを依存しています。

ということで、つまりは、以下のJARファイルを入手しました。

- batik-all-1.9.jar
- xml-apis-ext.jar
- xml-apis.jar
- xmlgraphics-commons-2.2.jar

BatikはJava 1.6以上が必須です（私はJava 1.8で試しました）。
対応しているSVGの仕様はバージョン1.1までのようです。1.2 Specificationには未対応のままのようです。
この投稿を書いているのは2017年6月末なのですが、上記の各Webサイトを見ると、”Web2.0以前のホームページっぽさ”を感じてしまうのは私だけでしょうか？日本アカデミー賞優秀主演男優賞を受賞したこともある俳優の[阿部 寛さんのホームページ](http://abehiroshi.la.coocan.jp/)にも似た雰囲気を感じてしまいます。
ということで、いつ、これらのライブラリがEOLになるか、わかったもんじゃありません。

# いざ、プログラム

上記のBatikのWebサイトでサンプルコードは掲載されてあれども、うまくいきませんでした。困りました。

そこでインターネットという大海に飛び込んでみると、こちらのブログを発見しました。

[霧笛望のもったいない開発保管庫 Apache batikによるSVG→PNG/JPEGへの変換方法](http://d.hatena.ne.jp/kirifue/20090618/1245291317)

2009-06-18の記事となっています。8年前のブログが残っていた（残していただいていた）奇跡に感謝します。kirifue様、ありがとうございます。

```java:ConvSVG4PNG.java
package edu.study;

import java.awt.Rectangle;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;

public class ConvSVG4PNG {
  public static void main(String[] args) {

    try (InputStream istream = new FileInputStream("svg/delorean.svg");
        OutputStream ostream = new FileOutputStream("img/delorean.png");) {

      TranscoderInput input = new TranscoderInput(istream);

      TranscoderOutput output = new TranscoderOutput(ostream);

      PNGTranscoder pngTranscoder = new PNGTranscoder();

      // widthとheight、そしてリージョンを設定
      int width = 512;
      int height = 512;
      Rectangle rect = new Rectangle(0, 0, width, height);
      pngTranscoder.addTranscodingHint(PNGTranscoder.KEY_WIDTH, new Float(rect.width));
      pngTranscoder.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, new Float(rect.height));
      pngTranscoder.addTranscodingHint(PNGTranscoder.KEY_AOI, rect);

      // 変換
      pngTranscoder.transcode(input, output);

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      System.out.println("完了");
    }
  }
}
```

svgと名付けたフォルダにSVGファイルを保存しておきます。imgという名のフォルダを作っておきます。実行しますと、PNG画像ファイルが出力されます。

以上です。