Claw  1.7.3
targa_writer.tpp
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
25 /**
26  * \file targa_writer.tpp
27  * \brief Implementation of the template methods of the targa::writer class and
28  * subclasses.
29  * \author Julien Jorge
30  */
31 #include <limits>
32 #include <iterator>
33 
34 
35 //********************* targa::writer::file_output_buffer **********************
36 
37 
38 
39 
40  /*----------------------------------------------------------------------------*/
41 /**
42  * \brief Constructor.
43  * \param os The
44  */
45 template<typename Pixel>
46 claw::graphic::targa::writer::file_output_buffer<Pixel>::file_output_buffer
47 ( std::ostream& os )
48  : m_stream(os)
49 {
50 
51 } // targa::writer::file_output_buffer::file_output_buffer()
52 
53 /*----------------------------------------------------------------------------*/
54 /**
55  * \brief Code a pixel.
56  * \param n The number of time the pixel appears.
57  * \param pattern The value of the pixel.
58  */
59 template<typename Pixel>
60 void claw::graphic::targa::writer::file_output_buffer<Pixel>::encode
61 ( unsigned int n, pattern_type pattern )
62 {
63  assert( n <= max_encodable() );
64  assert( n >= min_interesting() );
65 
66  unsigned char key = (n-1) | 0x80;
67 
68  m_stream << key;
69  order_pixel_bytes( pattern );
70 } // targa::writer::file_output_buffer::encode()
71 
72 /*----------------------------------------------------------------------------*/
73 /**
74  * \brief Write raw data int the stream.
75  * \param first Iterator on the first data.
76  * \param last Iterator past the last data.
77  */
78 template<typename Pixel>
79 template<typename Iterator>
80 void claw::graphic::targa::writer::file_output_buffer<Pixel>::raw
81 ( Iterator first, Iterator last )
82 {
83  unsigned int n = std::distance(first, last);
84 
85  unsigned int full = n / max_encodable();
86  unsigned int remaining = n % max_encodable();
87 
88  unsigned char key = max_encodable() - 1;
89 
90  for (unsigned int i=0; i!=full; ++i)
91  {
92  m_stream << key;
93 
94  for (unsigned int j=0; j!=max_encodable(); ++j, ++first)
95  order_pixel_bytes( *first );
96  }
97 
98  if (remaining)
99  {
100  key = remaining - 1;
101  m_stream << key;
102 
103  for (unsigned int j=0; j!=remaining; ++j, ++first)
104  order_pixel_bytes( *first );
105  }
106 
107 } // targa::writer::file_output_buffer::raw()
108 
109 /*----------------------------------------------------------------------------*/
110 /**
111  * \brief Get the minimum number of pixels needed for encoding.
112  */
113 template<typename Pixel>
114 unsigned int
115 claw::graphic::targa::writer::file_output_buffer<Pixel>::min_interesting() const
116 {
117  return 2;
118 } // targa::writer::file_output_buffer::min_interesting()
119 
120 /*----------------------------------------------------------------------------*/
121 /**
122  * \brief Get the maximum number of pixel a code can encode.
123  */
124 template<typename Pixel>
125 unsigned int
126 claw::graphic::targa::writer::file_output_buffer<Pixel>::max_encodable() const
127 {
128  return 0x80;
129 } // targa::writer::file_output_buffer::max_encodable()