$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
to_base64.hh
1 // Copyright (C) 2011 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 
27 
28 #ifndef SCRIBO_CONVERT_TO_BASE64_HH
29 # define SCRIBO_CONVERT_TO_BASE64_HH
30 
63 # include <cstdio>
64 # include <cstdlib>
65 
66 # include <mln/border/resize.hh>
67 # include <mln/core/image/image2d.hh>
68 # include <mln/util/array.hh>
69 
70 
71 # define B64_DEF_LINE_SIZE 72
72 
73 namespace scribo
74 {
75 
76  namespace convert
77  {
78 
79  using namespace mln;
80 
81 
82  template <typename I>
83  void
84  to_base64(const Image<I>& input, mln::util::array<unsigned char>& output);
85 
86 
87 # ifndef MLN_INCLUDE_ONLY
88 
89 
90  namespace internal
91  {
92 
93  /*
94  ** Translation Table as described in RFC1113
95  */
96  static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
97 
98  /*
99  ** encodeblock
100  **
101  ** encode 3 8-bit binary bytes as 4 '6-bit' characters
102  */
103  inline
104  void
105  encodeblock(unsigned char in[3], unsigned char out[4], int len)
106  {
107  out[0] = cb64[ in[0] >> 2 ];
108  out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
109  out[2] = (unsigned char) (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=');
110  out[3] = (unsigned char) (len > 2 ? cb64[ in[2] & 0x3f ] : '=');
111  }
112 
113  } // end of namespace scribo::convert::internal
114 
115 
116 
117  template <typename I>
118  void
119  to_base64(const Image<I>& input_, mln::util::array<unsigned char>& output)
120  {
121  mln_trace("scribo::convert::to_base64");
122 
123  mln_precondition(exact(input_).is_valid());
124  using namespace internal;
125 
126  const I& input = exact(input_);
127 
128  unsigned char in[3], out[4];
129  int i, len, blocksout = 0;
130 
131  // FIXME: Take border into account while moving pointer and
132  // remove that call.
133  border::resize(input, 0);
134 
135  const unsigned char
136  *end_ptr = (unsigned char *) (input.buffer() + input.nelements()),
137  *ptr = (unsigned char *)input.buffer();
138 
139  while(ptr != end_ptr)
140  {
141  len = 0;
142  for(i = 0; i < 3; ++i)
143  {
144  if (ptr != end_ptr)
145  {
146  in[i] = (unsigned char) *ptr++;
147  ++len;
148  }
149  else
150  {
151  in[i] = 0;
152  for(++i; i < 3; ++i)
153  in[i] = 0;
154  }
155  }
156  if(len)
157  {
158  encodeblock(in, out, len);
159  for(i = 0; i < 4; ++i)
160  output.append(out[i]);
161  ++blocksout;
162  }
163  if(blocksout >= (B64_DEF_LINE_SIZE/4) || ptr == end_ptr)
164  {
165  if(blocksout)
166  {
167  output.append('\r');
168  output.append('\n');
169  }
170  blocksout = 0;
171  }
172  }
173 
174  }
175 
176 # endif // ! MLN_INCLUDE_ONLY
177 
178 # undef B64_DEF_LINE_SIZE
179 
180  } // end of namespace scribo::convert
181 
182 } // end of namespace scribo
183 
184 #endif // ! SCRIBO_CONVERT_TO_BASE64_HH