PageRenderTime 94ms CodeModel.GetById 18ms app.highlight 56ms RepoModel.GetById 7ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/gil/doc/html/g_i_l_0253.html

http://hadesmem.googlecode.com/
HTML | 401 lines | 382 code | 3 blank | 16 comment | 0 complexity | 050913735214f6dbd8c755667b768e0e MD5 | raw file
  1<!-- Copyright 2008 Lubomir Bourdev and Hailin Jin
  2
  3     Distributed under the Boost Software License, Version 1.0.
  4     (See accompanying file LICENSE_1_0.txt or copy at
  5     http://www.boost.org/LICENSE_1_0.txt)
  6  -->
  7
  8<!--
  9    Copyright 2005-2007 Adobe Systems Incorporated
 10    Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
 11    or a copy at http://stlab.adobe.com/licenses.html)
 12
 13    Some files are held under additional license.
 14    Please see "http://stlab.adobe.com/licenses.html" for more information.
 15-->
 16
 17<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 18        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 19<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
 20
 21<head>
 22    <TITLE>Generic Image Library: png_io_private.hpp Source File</TITLE>
 23    <META HTTP-EQUIV="content-type" CONTENT="text/html;charset=ISO-8859-1"/>
 24    <LINK TYPE="text/css" REL="stylesheet" HREF="adobe_source.css"/>
 25</head>
 26<body>
 27<table border="0" cellspacing="0" cellpadding="0" style='width: 100%; margin: 0; padding: 0'><tr>
 28<td width="100%" valign="top" style='padding-left: 10px; padding-right: 10px; padding-bottom: 10px'>
 29<div class="qindex"><a class="qindex" href="index.html">Modules</a> 
 30                  | <a class="qindex" href="classes.html">Alphabetical List</a> 
 31                  | <a class="qindex" href="annotated.html">Class List</a> 
 32                  | <a class="qindex" href="dirs.html">Directories</a> 
 33                  | <a class="qindex" href="files.html">File List</a> 
 34                  | <a class="qindex" href="../index.html">GIL Home Page</a> 
 35</div>
 36<!-- End Header -->
 37<!-- Generated by Doxygen 1.5.6 -->
 38  <div class="navpath"><a class="el" href="g_i_l_0076.html">boost</a>&nbsp;&raquo&nbsp;<a class="el" href="g_i_l_0079.html">gil</a>&nbsp;&raquo&nbsp;<a class="el" href="g_i_l_0078.html">extension</a>&nbsp;&raquo&nbsp;<a class="el" href="g_i_l_0077.html">io</a>
 39  </div>
 40<div class="contents">
 41<h1>png_io_private.hpp</h1><a href="g_i_l_0254.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
 42<a name="l00002"></a>00002 <span class="comment">    Copyright 2005-2007 Adobe Systems Incorporated</span>
 43<a name="l00003"></a>00003 <span class="comment">   </span>
 44<a name="l00004"></a>00004 <span class="comment">    Use, modification and distribution are subject to the Boost Software License,</span>
 45<a name="l00005"></a>00005 <span class="comment">    Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at</span>
 46<a name="l00006"></a>00006 <span class="comment">    http://www.boost.org/LICENSE_1_0.txt).</span>
 47<a name="l00007"></a>00007 <span class="comment"></span>
 48<a name="l00008"></a>00008 <span class="comment">    See http://stlab.adobe.com/gil for most recent version including documentation.</span>
 49<a name="l00009"></a>00009 <span class="comment">*/</span>
 50<a name="l00010"></a>00010 <span class="comment">/*************************************************************************************************/</span>
 51<a name="l00011"></a>00011 
 52<a name="l00012"></a>00012 <span class="preprocessor">#ifndef GIL_PNG_IO_PRIVATE_H</span>
 53<a name="l00013"></a>00013 <span class="preprocessor"></span><span class="preprocessor">#define GIL_PNG_IO_PRIVATE_H</span>
 54<a name="l00014"></a>00014 <span class="preprocessor"></span>
 55<a name="l00020"></a>00020 
 56<a name="l00021"></a>00021 <span class="preprocessor">#include &lt;algorithm&gt;</span>
 57<a name="l00022"></a>00022 <span class="preprocessor">#include &lt;vector&gt;</span>
 58<a name="l00023"></a>00023 <span class="preprocessor">#include &lt;boost/static_assert.hpp&gt;</span>
 59<a name="l00024"></a>00024 <span class="preprocessor">#include "../../gil_all.hpp"</span>
 60<a name="l00025"></a>00025 <span class="preprocessor">#include "<a class="code" href="io__error_8hpp.html" title="Handle input-output errors.">io_error.hpp</a>"</span>
 61<a name="l00026"></a>00026 <span class="preprocessor">#include &lt;png.h&gt;</span>
 62<a name="l00027"></a>00027 
 63<a name="l00028"></a>00028 <span class="keyword">namespace </span>boost { <span class="keyword">namespace </span>gil {
 64<a name="l00029"></a>00029 
 65<a name="l00030"></a>00030 <span class="keyword">namespace </span>detail {
 66<a name="l00031"></a>00031 
 67<a name="l00032"></a>00032 <span class="keyword">static</span> <span class="keyword">const</span> std::size_t PNG_BYTES_TO_CHECK = 4;
 68<a name="l00033"></a>00033 
 69<a name="l00034"></a>00034 <span class="comment">// lbourdev: These can be greatly simplified, for example:</span>
 70<a name="l00035"></a>00035 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Cs&gt; <span class="keyword">struct </span>png_color_type {BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=0);};
 71<a name="l00036"></a>00036 <span class="keyword">template</span>&lt;&gt; <span class="keyword">struct </span>png_color_type&lt;gray_t&gt; { BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=PNG_COLOR_TYPE_GRAY); };
 72<a name="l00037"></a>00037 <span class="keyword">template</span>&lt;&gt; <span class="keyword">struct </span>png_color_type&lt;rgb_t&gt;  { BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=PNG_COLOR_TYPE_RGB); };
 73<a name="l00038"></a>00038 <span class="keyword">template</span>&lt;&gt; <span class="keyword">struct </span>png_color_type&lt;rgba_t&gt; { BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=PNG_COLOR_TYPE_RGBA); };
 74<a name="l00039"></a>00039 
 75<a name="l00040"></a>00040 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Channel,<span class="keyword">typename</span> ColorSpace&gt; <span class="keyword">struct </span>png_is_supported {BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,value=<span class="keyword">false</span>);};
 76<a name="l00041"></a>00041 <span class="keyword">template</span> &lt;&gt; <span class="keyword">struct </span>png_is_supported&lt;bits8,gray_t&gt;  {BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,value=<span class="keyword">true</span>);};
 77<a name="l00042"></a>00042 <span class="keyword">template</span> &lt;&gt; <span class="keyword">struct </span>png_is_supported&lt;bits8,rgb_t&gt;   {BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,value=<span class="keyword">true</span>);};
 78<a name="l00043"></a>00043 <span class="keyword">template</span> &lt;&gt; <span class="keyword">struct </span>png_is_supported&lt;bits8,rgba_t&gt;  {BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,value=<span class="keyword">true</span>);};
 79<a name="l00044"></a>00044 <span class="keyword">template</span> &lt;&gt; <span class="keyword">struct </span>png_is_supported&lt;bits16,gray_t&gt; {BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,value=<span class="keyword">true</span>);};
 80<a name="l00045"></a>00045 <span class="keyword">template</span> &lt;&gt; <span class="keyword">struct </span>png_is_supported&lt;bits16,rgb_t&gt;  {BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,value=<span class="keyword">true</span>);};
 81<a name="l00046"></a>00046 <span class="keyword">template</span> &lt;&gt; <span class="keyword">struct </span>png_is_supported&lt;bits16,rgba_t&gt; {BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,value=<span class="keyword">true</span>);};
 82<a name="l00047"></a>00047 
 83<a name="l00048"></a>00048 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Channel&gt; <span class="keyword">struct </span>png_bit_depth {BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,bit_depth=<span class="keyword">sizeof</span>(Channel)*8);};
 84<a name="l00049"></a>00049 
 85<a name="l00050"></a>00050 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Channel,<span class="keyword">typename</span> ColorSpace&gt;
 86<a name="l00051"></a>00051 <span class="keyword">struct </span>png_read_support_private {
 87<a name="l00052"></a>00052     BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,is_supported=<span class="keyword">false</span>);
 88<a name="l00053"></a>00053     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,bit_depth=0);
 89<a name="l00054"></a>00054     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=0);
 90<a name="l00055"></a>00055 };
 91<a name="l00056"></a>00056 <span class="keyword">template</span> &lt;&gt;
 92<a name="l00057"></a>00057 <span class="keyword">struct </span>png_read_support_private&lt;bits8,gray_t&gt; {
 93<a name="l00058"></a>00058     BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,is_supported=<span class="keyword">true</span>);
 94<a name="l00059"></a>00059     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,bit_depth=8);
 95<a name="l00060"></a>00060     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=PNG_COLOR_TYPE_GRAY);
 96<a name="l00061"></a>00061 };
 97<a name="l00062"></a>00062 <span class="keyword">template</span> &lt;&gt;
 98<a name="l00063"></a>00063 <span class="keyword">struct </span>png_read_support_private&lt;bits8,rgb_t&gt; {
 99<a name="l00064"></a>00064     BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,is_supported=<span class="keyword">true</span>);
100<a name="l00065"></a>00065     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,bit_depth=8);
101<a name="l00066"></a>00066     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=PNG_COLOR_TYPE_RGB);
102<a name="l00067"></a>00067 };
103<a name="l00068"></a>00068 <span class="keyword">template</span> &lt;&gt;
104<a name="l00069"></a>00069 <span class="keyword">struct </span>png_read_support_private&lt;bits8,rgba_t&gt; {
105<a name="l00070"></a>00070     BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,is_supported=<span class="keyword">true</span>);
106<a name="l00071"></a>00071     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,bit_depth=8);
107<a name="l00072"></a>00072     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=PNG_COLOR_TYPE_RGBA);
108<a name="l00073"></a>00073 };
109<a name="l00074"></a>00074 <span class="keyword">template</span> &lt;&gt;
110<a name="l00075"></a>00075 <span class="keyword">struct </span>png_read_support_private&lt;bits16,gray_t&gt; {
111<a name="l00076"></a>00076     BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,is_supported=<span class="keyword">true</span>);
112<a name="l00077"></a>00077     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,bit_depth=16);
113<a name="l00078"></a>00078     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=PNG_COLOR_TYPE_GRAY);
114<a name="l00079"></a>00079 };
115<a name="l00080"></a>00080 <span class="keyword">template</span> &lt;&gt;
116<a name="l00081"></a>00081 <span class="keyword">struct </span>png_read_support_private&lt;bits16,rgb_t&gt; {
117<a name="l00082"></a>00082     BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,is_supported=<span class="keyword">true</span>);
118<a name="l00083"></a>00083     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,bit_depth=16);
119<a name="l00084"></a>00084     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=PNG_COLOR_TYPE_RGB);
120<a name="l00085"></a>00085 };
121<a name="l00086"></a>00086 <span class="keyword">template</span> &lt;&gt;
122<a name="l00087"></a>00087 <span class="keyword">struct </span>png_read_support_private&lt;bits16,rgba_t&gt; {
123<a name="l00088"></a>00088     BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,is_supported=<span class="keyword">true</span>);
124<a name="l00089"></a>00089     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,bit_depth=16);
125<a name="l00090"></a>00090     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=PNG_COLOR_TYPE_RGBA);
126<a name="l00091"></a>00091 };
127<a name="l00092"></a>00092 
128<a name="l00093"></a>00093 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Channel,<span class="keyword">typename</span> ColorSpace&gt;
129<a name="l00094"></a>00094 <span class="keyword">struct </span>png_write_support_private {
130<a name="l00095"></a>00095     BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,is_supported=<span class="keyword">false</span>);
131<a name="l00096"></a>00096     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,bit_depth=0);
132<a name="l00097"></a>00097     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=0);
133<a name="l00098"></a>00098 };
134<a name="l00099"></a>00099 <span class="keyword">template</span> &lt;&gt;
135<a name="l00100"></a>00100 <span class="keyword">struct </span>png_write_support_private&lt;bits8,gray_t&gt; {
136<a name="l00101"></a>00101     BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,is_supported=<span class="keyword">true</span>);
137<a name="l00102"></a>00102     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,bit_depth=8);
138<a name="l00103"></a>00103     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=PNG_COLOR_TYPE_GRAY);
139<a name="l00104"></a>00104 };
140<a name="l00105"></a>00105 <span class="keyword">template</span> &lt;&gt;
141<a name="l00106"></a>00106 <span class="keyword">struct </span>png_write_support_private&lt;bits8,rgb_t&gt; {
142<a name="l00107"></a>00107     BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,is_supported=<span class="keyword">true</span>);
143<a name="l00108"></a>00108     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,bit_depth=8);
144<a name="l00109"></a>00109     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=PNG_COLOR_TYPE_RGB);
145<a name="l00110"></a>00110 };
146<a name="l00111"></a>00111 <span class="keyword">template</span> &lt;&gt;
147<a name="l00112"></a>00112 <span class="keyword">struct </span>png_write_support_private&lt;bits8,rgba_t&gt; {
148<a name="l00113"></a>00113     BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,is_supported=<span class="keyword">true</span>);
149<a name="l00114"></a>00114     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,bit_depth=8);
150<a name="l00115"></a>00115     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=PNG_COLOR_TYPE_RGBA);
151<a name="l00116"></a>00116 };
152<a name="l00117"></a>00117 <span class="keyword">template</span> &lt;&gt;
153<a name="l00118"></a>00118 <span class="keyword">struct </span>png_write_support_private&lt;bits16,gray_t&gt; {
154<a name="l00119"></a>00119     BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,is_supported=<span class="keyword">true</span>);
155<a name="l00120"></a>00120     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,bit_depth=16);
156<a name="l00121"></a>00121     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=PNG_COLOR_TYPE_GRAY);
157<a name="l00122"></a>00122 };
158<a name="l00123"></a>00123 <span class="keyword">template</span> &lt;&gt;
159<a name="l00124"></a>00124 <span class="keyword">struct </span>png_write_support_private&lt;bits16,rgb_t&gt; {
160<a name="l00125"></a>00125     BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,is_supported=<span class="keyword">true</span>);
161<a name="l00126"></a>00126     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,bit_depth=16);
162<a name="l00127"></a>00127     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=PNG_COLOR_TYPE_RGB);
163<a name="l00128"></a>00128 };
164<a name="l00129"></a>00129 <span class="keyword">template</span> &lt;&gt;
165<a name="l00130"></a>00130 <span class="keyword">struct </span>png_write_support_private&lt;bits16,rgba_t&gt; {
166<a name="l00131"></a>00131     BOOST_STATIC_CONSTANT(<span class="keywordtype">bool</span>,is_supported=<span class="keyword">true</span>);
167<a name="l00132"></a>00132     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,bit_depth=16);
168<a name="l00133"></a>00133     BOOST_STATIC_CONSTANT(<span class="keywordtype">int</span>,color_type=PNG_COLOR_TYPE_RGBA);
169<a name="l00134"></a>00134 };
170<a name="l00135"></a>00135 
171<a name="l00136"></a>00136 <span class="keyword">class </span>png_reader : <span class="keyword">public</span> file_mgr {
172<a name="l00137"></a>00137 <span class="keyword">protected</span>:
173<a name="l00138"></a>00138     png_structp _png_ptr;
174<a name="l00139"></a>00139     png_infop _info_ptr;
175<a name="l00140"></a>00140 
176<a name="l00141"></a>00141     <span class="keywordtype">void</span> init() {
177<a name="l00142"></a>00142         <span class="keywordtype">char</span> buf[PNG_BYTES_TO_CHECK];
178<a name="l00143"></a>00143         <span class="comment">// read in some of the signature bytes</span>
179<a name="l00144"></a>00144         io_error_if(fread(buf, 1, PNG_BYTES_TO_CHECK, <span class="keyword">get</span>()) != detail::PNG_BYTES_TO_CHECK,
180<a name="l00145"></a>00145                     <span class="stringliteral">"png_check_validity: fail to read file"</span>);
181<a name="l00146"></a>00146         <span class="comment">// compare the first PNG_BYTES_TO_CHECK bytes of the signature.</span>
182<a name="l00147"></a>00147         io_error_if(png_sig_cmp((png_bytep)buf, (png_size_t)0, detail::PNG_BYTES_TO_CHECK)!=0,
183<a name="l00148"></a>00148                     <span class="stringliteral">"png_check_validity: invalid png file"</span>);
184<a name="l00149"></a>00149 
185<a name="l00150"></a>00150         _png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
186<a name="l00151"></a>00151         io_error_if(_png_ptr==NULL,<span class="stringliteral">"png_get_file_size: fail to call png_create_write_struct()"</span>);
187<a name="l00152"></a>00152         <span class="comment">// allocate/initialize the image information data</span>
188<a name="l00153"></a>00153         _info_ptr = png_create_info_struct(_png_ptr);
189<a name="l00154"></a>00154         <span class="keywordflow">if</span> (_info_ptr == NULL) {
190<a name="l00155"></a>00155             png_destroy_read_struct(&amp;_png_ptr,png_infopp_NULL,png_infopp_NULL);
191<a name="l00156"></a>00156             io_error(<span class="stringliteral">"png_get_file_size: fail to call png_create_info_struct()"</span>);
192<a name="l00157"></a>00157         }
193<a name="l00158"></a>00158         <span class="keywordflow">if</span> (setjmp(png_jmpbuf(_png_ptr))) {
194<a name="l00159"></a>00159             <span class="comment">//free all of the memory associated with the png_ptr and info_ptr</span>
195<a name="l00160"></a>00160             png_destroy_read_struct(&amp;_png_ptr, &amp;_info_ptr, png_infopp_NULL);
196<a name="l00161"></a>00161             io_error(<span class="stringliteral">"png_get_file_size: fail to call setjmp()"</span>);
197<a name="l00162"></a>00162         }
198<a name="l00163"></a>00163         png_init_io(_png_ptr, <span class="keyword">get</span>());
199<a name="l00164"></a>00164         png_set_sig_bytes(_png_ptr,PNG_BYTES_TO_CHECK);
200<a name="l00165"></a>00165         png_read_info(_png_ptr, _info_ptr);
201<a name="l00166"></a>00166         <span class="keywordflow">if</span> (little_endian() &amp;&amp; png_get_bit_depth(_png_ptr,_info_ptr)&gt;8)
202<a name="l00167"></a>00167             png_set_swap(_png_ptr);
203<a name="l00168"></a>00168     }
204<a name="l00169"></a>00169 <span class="keyword">public</span>:
205<a name="l00170"></a>00170     png_reader(FILE* file          ) : file_mgr(file)           { init(); }
206<a name="l00171"></a>00171     png_reader(<span class="keyword">const</span> <span class="keywordtype">char</span>* filename) : file_mgr(filename, <span class="stringliteral">"rb"</span>) { init(); }
207<a name="l00172"></a>00172 
208<a name="l00173"></a>00173     ~png_reader() {
209<a name="l00174"></a>00174         png_destroy_read_struct(&amp;_png_ptr,&amp;_info_ptr,png_infopp_NULL);
210<a name="l00175"></a>00175     }
211<a name="l00176"></a>00176     point2&lt;std::ptrdiff_t&gt; get_dimensions() {
212<a name="l00177"></a>00177         <span class="keywordflow">return</span> point2&lt;std::ptrdiff_t&gt;(png_get_image_width(_png_ptr,_info_ptr),
213<a name="l00178"></a>00178                                       png_get_image_height(_png_ptr,_info_ptr));
214<a name="l00179"></a>00179     }
215<a name="l00180"></a>00180     <span class="keyword">template</span> &lt;<span class="keyword">typename</span> View&gt;
216<a name="l00181"></a>00181     <span class="keywordtype">void</span> apply(<span class="keyword">const</span> View&amp; <a class="code" href="g_i_l_0135.html#g55e45a15dddfc0d870c2f7fb9739cb2e" title="Returns the non-constant-pixel view of any image. The returned view is any view.">view</a>) {
217<a name="l00182"></a>00182         png_uint_32 width, height;
218<a name="l00183"></a>00183         <span class="keywordtype">int</span> bit_depth, color_type, interlace_type;
219<a name="l00184"></a>00184         png_get_IHDR(_png_ptr, _info_ptr,
220<a name="l00185"></a>00185                      &amp;width, &amp;height,&amp;bit_depth,&amp;color_type,&amp;interlace_type,
221<a name="l00186"></a>00186                      int_p_NULL, int_p_NULL);
222<a name="l00187"></a>00187         io_error_if(((png_uint_32)view.width()!=width || (png_uint_32)view.height()!= height),
223<a name="l00188"></a>00188                     <span class="stringliteral">"png_read_view: input view size does not match PNG file size"</span>);
224<a name="l00189"></a>00189         
225<a name="l00190"></a>00190         <span class="keywordflow">if</span>(png_read_support_private&lt;<span class="keyword">typename</span> channel_type&lt;View&gt;::type,
226<a name="l00191"></a>00191                                     <span class="keyword">typename</span> color_space_type&lt;View&gt;::type&gt;::bit_depth!=bit_depth ||
227<a name="l00192"></a>00192            png_read_support_private&lt;<span class="keyword">typename</span> channel_type&lt;View&gt;::type,
228<a name="l00193"></a>00193                                     <span class="keyword">typename</span> color_space_type&lt;View&gt;::type&gt;::color_type!=color_type)
229<a name="l00194"></a>00194             io_error(<span class="stringliteral">"png_read_view: input view type is incompatible with the image type"</span>);
230<a name="l00195"></a>00195         
231<a name="l00196"></a>00196         std::vector&lt;pixel&lt;typename channel_type&lt;View&gt;::type,
232<a name="l00197"></a>00197                           layout&lt;typename color_space_type&lt;View&gt;::type&gt; &gt; &gt; row(width);
233<a name="l00198"></a>00198         <span class="keywordflow">for</span>(png_uint_32 y=0;y&lt;height;++y) {
234<a name="l00199"></a>00199             png_read_row(_png_ptr,(png_bytep)&amp;row.front(),NULL);
235<a name="l00200"></a>00200             <a class="code" href="g_i_l_0214.html#g4e8a9e1bb08e76c93dc56a4626231ee1" title="Copy when both src and dst are planar pointers is copy for each channel.">std::copy</a>(row.begin(),row.end(),view.row_begin(y));
236<a name="l00201"></a>00201         }
237<a name="l00202"></a>00202         png_read_end(_png_ptr,NULL);
238<a name="l00203"></a>00203     }
239<a name="l00204"></a>00204 
240<a name="l00205"></a>00205     <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Image&gt;
241<a name="l00206"></a>00206     <span class="keywordtype">void</span> read_image(Image&amp; im) {
242<a name="l00207"></a>00207         im.recreate(get_dimensions());
243<a name="l00208"></a>00208         apply(<a class="code" href="g_i_l_0135.html#g55e45a15dddfc0d870c2f7fb9739cb2e" title="Returns the non-constant-pixel view of any image. The returned view is any view.">view</a>(im));
244<a name="l00209"></a>00209     }
245<a name="l00210"></a>00210 };
246<a name="l00211"></a>00211 
247<a name="l00212"></a>00212 <span class="comment">// This code will be simplified...</span>
248<a name="l00213"></a>00213 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> CC&gt;
249<a name="l00214"></a>00214 <span class="keyword">class </span>png_reader_color_convert : <span class="keyword">public</span> png_reader {
250<a name="l00215"></a>00215 <span class="keyword">private</span>:
251<a name="l00216"></a>00216     CC _cc;
252<a name="l00217"></a>00217 <span class="keyword">public</span>:
253<a name="l00218"></a>00218     png_reader_color_convert(FILE* file          ,CC cc_in) : png_reader(file),_cc(cc_in) {}
254<a name="l00219"></a>00219     png_reader_color_convert(FILE* file          ) : png_reader(file) {}
255<a name="l00220"></a>00220     png_reader_color_convert(<span class="keyword">const</span> <span class="keywordtype">char</span>* filename,CC cc_in) : png_reader(filename),_cc(cc_in) {}
256<a name="l00221"></a>00221     png_reader_color_convert(<span class="keyword">const</span> <span class="keywordtype">char</span>* filename) : png_reader(filename) {}
257<a name="l00222"></a>00222     <span class="keyword">template</span> &lt;<span class="keyword">typename</span> View&gt;
258<a name="l00223"></a>00223     <span class="keywordtype">void</span> apply(<span class="keyword">const</span> View&amp; view) {
259<a name="l00224"></a>00224         png_uint_32 width, height;
260<a name="l00225"></a>00225         <span class="keywordtype">int</span> bit_depth, color_type, interlace_type;
261<a name="l00226"></a>00226         png_get_IHDR(_png_ptr, _info_ptr,
262<a name="l00227"></a>00227                      &amp;width, &amp;height,&amp;bit_depth,&amp;color_type,&amp;interlace_type,
263<a name="l00228"></a>00228                      int_p_NULL, int_p_NULL);
264<a name="l00229"></a>00229         io_error_if(((png_uint_32)view.width()!=width || (png_uint_32)view.height()!= height),
265<a name="l00230"></a>00230                     <span class="stringliteral">"png_reader_color_convert::apply(): input view size does not match PNG file size"</span>);
266<a name="l00231"></a>00231         <span class="keywordflow">switch</span> (color_type) {
267<a name="l00232"></a>00232         <span class="keywordflow">case</span> PNG_COLOR_TYPE_GRAY:
268<a name="l00233"></a>00233             <span class="keywordflow">switch</span> (bit_depth) {
269<a name="l00234"></a>00234             <span class="keywordflow">case</span> 8: {
270<a name="l00235"></a>00235                 std::vector&lt;gray8_pixel_t&gt; row(width);
271<a name="l00236"></a>00236                 <span class="keywordflow">for</span>(png_uint_32 y=0;y&lt;height;++y) {
272<a name="l00237"></a>00237                     png_read_row(_png_ptr,(png_bytep)&amp;row.front(),NULL);
273<a name="l00238"></a>00238                     std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn&lt;gray8_ref_t,typename View::value_type,CC&gt;(_cc));
274<a name="l00239"></a>00239                 }
275<a name="l00240"></a>00240                 <span class="keywordflow">break</span>;
276<a name="l00241"></a>00241             }
277<a name="l00242"></a>00242             <span class="keywordflow">case</span> 16: {
278<a name="l00243"></a>00243                 std::vector&lt;gray16_pixel_t&gt; row(width);
279<a name="l00244"></a>00244                 <span class="keywordflow">for</span>(png_uint_32 y=0;y&lt;height;++y) {
280<a name="l00245"></a>00245                     png_read_row(_png_ptr,(png_bytep)&amp;row.front(),NULL);
281<a name="l00246"></a>00246                     std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn&lt;gray16_ref_t,typename View::value_type,CC&gt;(_cc));
282<a name="l00247"></a>00247                 }
283<a name="l00248"></a>00248                 <span class="keywordflow">break</span>;
284<a name="l00249"></a>00249             }
285<a name="l00250"></a>00250             <span class="keywordflow">default</span>: io_error(<span class="stringliteral">"png_reader_color_convert::apply(): unknown combination of color type and bit depth"</span>);
286<a name="l00251"></a>00251             }
287<a name="l00252"></a>00252             <span class="keywordflow">break</span>;
288<a name="l00253"></a>00253         <span class="keywordflow">case</span> PNG_COLOR_TYPE_RGB:
289<a name="l00254"></a>00254             <span class="keywordflow">switch</span> (bit_depth) {
290<a name="l00255"></a>00255             <span class="keywordflow">case</span> 8: {
291<a name="l00256"></a>00256                 std::vector&lt;rgb8_pixel_t&gt; row(width);
292<a name="l00257"></a>00257                 <span class="keywordflow">for</span>(png_uint_32 y=0;y&lt;height;++y) {
293<a name="l00258"></a>00258                     png_read_row(_png_ptr,(png_bytep)&amp;row.front(),NULL);
294<a name="l00259"></a>00259                     std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn&lt;rgb8_ref_t,typename View::value_type,CC&gt;(_cc));
295<a name="l00260"></a>00260                 }
296<a name="l00261"></a>00261                 <span class="keywordflow">break</span>;
297<a name="l00262"></a>00262             }
298<a name="l00263"></a>00263             <span class="keywordflow">case</span> 16: {
299<a name="l00264"></a>00264                 std::vector&lt;rgb16_pixel_t&gt; row(width);
300<a name="l00265"></a>00265                 <span class="keywordflow">for</span>(png_uint_32 y=0;y&lt;height;++y) {
301<a name="l00266"></a>00266                     png_read_row(_png_ptr,(png_bytep)&amp;row.front(),NULL);
302<a name="l00267"></a>00267                     std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn&lt;rgb16_ref_t,typename View::value_type,CC&gt;(_cc));
303<a name="l00268"></a>00268                 }
304<a name="l00269"></a>00269                 <span class="keywordflow">break</span>;
305<a name="l00270"></a>00270             }
306<a name="l00271"></a>00271             <span class="keywordflow">default</span>: io_error(<span class="stringliteral">"png_reader_color_convert::apply(): unknown combination of color type and bit depth"</span>);
307<a name="l00272"></a>00272             }
308<a name="l00273"></a>00273             <span class="keywordflow">break</span>;
309<a name="l00274"></a>00274         <span class="keywordflow">case</span> PNG_COLOR_TYPE_RGBA:
310<a name="l00275"></a>00275             <span class="keywordflow">switch</span> (bit_depth) {
311<a name="l00276"></a>00276             <span class="keywordflow">case</span> 8: {
312<a name="l00277"></a>00277                 std::vector&lt;rgba8_pixel_t&gt; row(width);
313<a name="l00278"></a>00278                 <span class="keywordflow">for</span>(png_uint_32 y=0;y&lt;height;++y) {
314<a name="l00279"></a>00279                     png_read_row(_png_ptr,(png_bytep)&amp;row.front(),NULL);
315<a name="l00280"></a>00280                     std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn&lt;rgba8_ref_t,typename View::value_type,CC&gt;(_cc));
316<a name="l00281"></a>00281                 }
317<a name="l00282"></a>00282                 <span class="keywordflow">break</span>;
318<a name="l00283"></a>00283             }
319<a name="l00284"></a>00284             <span class="keywordflow">case</span> 16: {
320<a name="l00285"></a>00285                 std::vector&lt;rgba16_pixel_t&gt; row(width);
321<a name="l00286"></a>00286                 <span class="keywordflow">for</span>(png_uint_32 y=0;y&lt;height;++y) {
322<a name="l00287"></a>00287                     png_read_row(_png_ptr,(png_bytep)&amp;row.front(),NULL);
323<a name="l00288"></a>00288                     std::transform(row.begin(),row.end(),view.row_begin(y),color_convert_deref_fn&lt;rgba16_ref_t,typename View::value_type,CC&gt;(_cc));
324<a name="l00289"></a>00289                 }
325<a name="l00290"></a>00290                 <span class="keywordflow">break</span>;
326<a name="l00291"></a>00291             }
327<a name="l00292"></a>00292             <span class="keywordflow">default</span>: io_error(<span class="stringliteral">"png_reader_color_convert::apply(): unknown combination of color type and bit depth"</span>);
328<a name="l00293"></a>00293             }
329<a name="l00294"></a>00294             <span class="keywordflow">break</span>;
330<a name="l00295"></a>00295         <span class="keywordflow">default</span>: io_error(<span class="stringliteral">"png_reader_color_convert::apply(): unknown color type"</span>);
331<a name="l00296"></a>00296         }
332<a name="l00297"></a>00297         png_read_end(_png_ptr,NULL);
333<a name="l00298"></a>00298     }
334<a name="l00299"></a>00299     <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Image&gt;
335<a name="l00300"></a>00300     <span class="keywordtype">void</span> read_image(Image&amp; im) {
336<a name="l00301"></a>00301         im.recreate(get_dimensions());
337<a name="l00302"></a>00302         apply(<a class="code" href="g_i_l_0135.html#g55e45a15dddfc0d870c2f7fb9739cb2e" title="Returns the non-constant-pixel view of any image. The returned view is any view.">view</a>(im));
338<a name="l00303"></a>00303     }
339<a name="l00304"></a>00304 };
340<a name="l00305"></a>00305 
341<a name="l00306"></a>00306 
342<a name="l00307"></a>00307 <span class="keyword">class </span>png_writer : <span class="keyword">public</span> file_mgr {
343<a name="l00308"></a>00308 <span class="keyword">protected</span>:
344<a name="l00309"></a>00309     png_structp _png_ptr;
345<a name="l00310"></a>00310     png_infop _info_ptr;
346<a name="l00311"></a>00311 
347<a name="l00312"></a>00312     <span class="keywordtype">void</span> init() {
348<a name="l00313"></a>00313         _png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
349<a name="l00314"></a>00314         io_error_if(!_png_ptr,<span class="stringliteral">"png_write_initialize: fail to call png_create_write_struct()"</span>);
350<a name="l00315"></a>00315         _info_ptr = png_create_info_struct(_png_ptr);
351<a name="l00316"></a>00316         <span class="keywordflow">if</span> (!_info_ptr) {
352<a name="l00317"></a>00317             png_destroy_write_struct(&amp;_png_ptr,png_infopp_NULL);
353<a name="l00318"></a>00318             io_error(<span class="stringliteral">"png_write_initialize: fail to call png_create_info_struct()"</span>);
354<a name="l00319"></a>00319         }
355<a name="l00320"></a>00320         <span class="keywordflow">if</span> (setjmp(png_jmpbuf(_png_ptr))) {
356<a name="l00321"></a>00321             png_destroy_write_struct(&amp;_png_ptr, &amp;_info_ptr);
357<a name="l00322"></a>00322             io_error(<span class="stringliteral">"png_write_initialize: fail to call setjmp(png_jmpbuf())"</span>);
358<a name="l00323"></a>00323         }
359<a name="l00324"></a>00324         png_init_io(_png_ptr,<span class="keyword">get</span>());
360<a name="l00325"></a>00325     }
361<a name="l00326"></a>00326 <span class="keyword">public</span>:
362<a name="l00327"></a>00327     png_writer(FILE* file          ) : file_mgr(file)           { init(); }
363<a name="l00328"></a>00328     png_writer(<span class="keyword">const</span> <span class="keywordtype">char</span>* filename) : file_mgr(filename, <span class="stringliteral">"wb"</span>) { init(); }
364<a name="l00329"></a>00329 
365<a name="l00330"></a>00330     ~png_writer() {
366<a name="l00331"></a>00331         png_destroy_write_struct(&amp;_png_ptr,&amp;_info_ptr);
367<a name="l00332"></a>00332     }
368<a name="l00333"></a>00333     <span class="keyword">template</span> &lt;<span class="keyword">typename</span> View&gt;
369<a name="l00334"></a>00334     <span class="keywordtype">void</span> apply(<span class="keyword">const</span> View&amp; view) {
370<a name="l00335"></a>00335         png_set_IHDR(_png_ptr, _info_ptr, view.width(), view.height(),
371<a name="l00336"></a>00336                      png_write_support_private&lt;typename channel_type&lt;View&gt;::type,
372<a name="l00337"></a>00337                                                <span class="keyword">typename</span> color_space_type&lt;View&gt;::type&gt;::bit_depth,
373<a name="l00338"></a>00338                      png_write_support_private&lt;typename channel_type&lt;View&gt;::type,
374<a name="l00339"></a>00339                                                <span class="keyword">typename</span> color_space_type&lt;View&gt;::type&gt;::color_type,
375<a name="l00340"></a>00340                      PNG_INTERLACE_NONE,
376<a name="l00341"></a>00341                      PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
377<a name="l00342"></a>00342         png_write_info(_png_ptr,_info_ptr);
378<a name="l00343"></a>00343         <span class="keywordflow">if</span> (little_endian() &amp;&amp;
379<a name="l00344"></a>00344             png_write_support_private&lt;<span class="keyword">typename</span> channel_type&lt;View&gt;::type,
380<a name="l00345"></a>00345                                       <span class="keyword">typename</span> color_space_type&lt;View&gt;::type&gt;::bit_depth&gt;8)
381<a name="l00346"></a>00346             png_set_swap(_png_ptr);
382<a name="l00347"></a>00347         std::vector&lt;pixel&lt;typename channel_type&lt;View&gt;::type,
383<a name="l00348"></a>00348                           layout&lt;typename color_space_type&lt;View&gt;::type&gt; &gt; &gt; row(view.width());
384<a name="l00349"></a>00349         <span class="keywordflow">for</span>(<span class="keywordtype">int</span> y=0;y&lt;view.height();++y) {
385<a name="l00350"></a>00350             <a class="code" href="g_i_l_0214.html#g4e8a9e1bb08e76c93dc56a4626231ee1" title="Copy when both src and dst are planar pointers is copy for each channel.">std::copy</a>(view.row_begin(y),view.row_end(y),row.begin());
386<a name="l00351"></a>00351             png_write_row(_png_ptr,(png_bytep)&amp;row.front());
387<a name="l00352"></a>00352         }
388<a name="l00353"></a>00353         png_write_end(_png_ptr,_info_ptr);
389<a name="l00354"></a>00354     }
390<a name="l00355"></a>00355 };
391<a name="l00356"></a>00356 
392<a name="l00357"></a>00357 } <span class="comment">// namespace detail</span>
393<a name="l00358"></a>00358 } }  <span class="comment">// namespace boost::gil</span>
394<a name="l00359"></a>00359 
395<a name="l00360"></a>00360 <span class="preprocessor">#endif</span>
396</pre></div></div>
397<hr size="1"><address style="text-align: right;"><small>Generated on Sat May 2 13:50:15 2009 for Generic Image Library by&nbsp;
398<a href="http://www.doxygen.org/index.html">
399<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
400</body>
401</html>