/usr/include/claw/targa.hpp is in libclaw-graphic-dev 1.7.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | /*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
/**
* \file targa.hpp
* \brief A class for targa pictures.
* \author Julien Jorge
*/
#ifndef __CLAW_TARGA_HPP__
#define __CLAW_TARGA_HPP__
#include <iostream>
#include <claw/image.hpp>
#include <claw/rle_decoder.hpp>
#include <claw/rle_encoder.hpp>
#include <claw/color_palette.hpp>
#include <claw/buffered_istream.hpp>
namespace claw
{
namespace graphic
{
/**
* \brief A class for targa pictures.
* \author Julien Jorge
*/
class targa : public image
{
private:
/*----------------------------------------------------------------------*/
/**
* \brief Tool class used for defining the structures of the datas stored
* in a targa file.
* \author Julien Jorge
*/
class file_structure
{
public:
enum image_coding
{
color_mapped = 1,
true_color = 2,
black_and_white = 3,
rle_color_mapped = 9,
rle_true_color = 10,
rle_black_and_white = 11
}; // enum image_coding
# pragma pack (push,1)
/*--------------------------------------------------------------------*/
/**
* \brief Header of a targa file.
*/
class header
{
public:
header();
header( unsigned int w, unsigned int h );
public:
/** \brief Image identifier length. */
char id_length;
/** \brief 1 if there is a color map, 0 otherwise. */
char color_map;
/** \brief Image type. */
char image_type;
/** \brief Color map specification. */
struct
{
/** \brief Index of the first color map entry. */
unsigned short first_entry_index;
/** \brief Total number of color map entries included. */
unsigned short length;
/** \brief Number of bits per enrty. */
unsigned char entry_size;
} color_map_specification;
/** \brief Image specification. */
struct specification
{
/** \brief Lower left corner X-origin. */
unsigned short x_origin;
/** \brief Lower left corner Y-origin. */
unsigned short y_origin;
/** \brief Image width. */
unsigned short width;
/** \brief Image height. */
unsigned short height;
/** \brief Bits per pixel. */
unsigned char bpp;
/** \brief descriptor. */
unsigned char descriptor;
bool up_down_oriented() const ;
bool left_right_oriented() const ;
unsigned char alpha() const;
}; // struct specification
/** \brief The specification of the image. */
specification image_specification;
}; // struct header
/*--------------------------------------------------------------------*/
/**
* \brief Item in the developper directory.
*/
struct developer_item
{
/** \brief Item identifier. */
unsigned short tag;
/** \brief Offset in the file. */
unsigned int offset;
/** \brief Fielf size. */
unsigned int size;
}; // struct developer_item
/*--------------------------------------------------------------------*/
/**
* \brief Extension area.
* \todo Describe this the day it seems useful.
*/
struct extension
{
}; // struct extension
/*--------------------------------------------------------------------*/
/**
* \brief Footer of a targa file.
*/
class footer
{
public:
footer();
bool is_valid() const;
public:
/** \brief Offset of the extension area. */
unsigned int extension_offset;
/** \brief Offset of the developer directory. */
unsigned int developer_offset;
/** \brief Footer identier. Must be as long as
std::string("TRUEVISION-XFILE.") + 1 (for the last '\\0').*/
char signature[18];
private:
/** \brief The string content in the signature. */
static const std::string s_signature;
}; // struct footer
# pragma pack (pop)
}; // class file_structure
/*----------------------------------------------------------------------*/
/**
* \brief A pixel color in 16 bits in targa files.
*
* \remark We need a type for template specialisation. No content is
* needed.
*/
struct pixel16
{
}; // struct pixel16
/*----------------------------------------------------------------------*/
/**
* \brief A pixel color in 8 bits in targa files.
*
* \remark We need a type for template specialisation. No content is
* needed.
*/
struct pixel8
{
}; // struct pixel8
/*----------------------------------------------------------------------*/
/**
* \brief A color palette of RGBA colors.
*/
typedef color_palette<rgba_pixel_8> color_palette32;
public:
/*----------------------------------------------------------------------*/
/**
* \brief This class read data from a targa file and store it in an
* image.
* \author Julien Jorge
*/
class reader : private file_structure
{
private:
/*--------------------------------------------------------------------*/
/**
* \brief The type of the input buffer associated with the file when
* decoding RLE files.
*
* \b Template \b parameters
* - \a Pixel, the type of the pixels in the input buffer.
*/
template<typename Pixel>
class file_input_buffer : public buffered_istream<std::istream>
{
private:
/** \brief The type of the pixels in the input buffer. */
typedef Pixel pixel_type;
public:
file_input_buffer( std::istream& f );
rgba_pixel_8 get_pixel();
}; // class file_input_buffer
/*--------------------------------------------------------------------*/
/**
* \brief The type of the input buffer associated with the file when
* decoding RLE files using a color palette.
*
* \b Template \b parameters
* - \a Pixel, the type of the pixels in the input buffer.
*/
template<typename Pixel>
class mapped_file_input_buffer:
public buffered_istream<std::istream>
{
private:
/** \brief The type of the pixels in the input buffer. */
typedef Pixel pixel_type;
public:
mapped_file_input_buffer( std::istream& f, const color_palette32& p );
rgba_pixel_8 get_pixel();
private:
/** \brief The color map of the image. */
const color_palette32& m_palette;
}; // class mapped_file_input_buffer
/*--------------------------------------------------------------------*/
/**
* \brief The output buffer for the RLE decoder.
*
* \b Template \b parameters
* - \a InputBuffer, the type of the input buffer.
*
* \author Julien Jorge
*/
template< typename InputBuffer >
class rle_targa_output_buffer
{
private:
/** \brief The type of he pixels in the input buffer. */
typedef rgba_pixel_8 pixel_type;
/** \brief The type of the input buffer. */
typedef InputBuffer input_buffer_type;
public:
rle_targa_output_buffer( image& img, bool up_down, bool left_right );
void fill( unsigned int n, rgba_pixel_8 pattern );
void copy( unsigned int n, input_buffer_type& buffer );
bool completed() const;
private:
void adjust_position(int x);
private:
/** \brief The targa image to fill. */
image& m_image;
/** \brief Current column index in the image. */
unsigned int m_x;
/** \brief Current row index in the image. */
unsigned int m_y;
/** \brief Horizontal increment. */
const int m_x_inc;
/** \brief Vertical increment. */
const int m_y_inc;
}; // class rle_targa_output_buffer
/*--------------------------------------------------------------------*/
/**
* \brief RLE decoder for targa RLE format
*
* \b Template \b parameters :
* - \a Pixel, the type of the pixels in the file.
* - \a OutputBuffer, the type of the output buffer.
*
* The \a OutputBuffer type must match the type requirements of the
* template parameter OutputBuffer of the rle_decoder class.
*
* \author Julien Jorge
*/
template< typename InputBuffer,
typename OutputBuffer = rle_targa_output_buffer<InputBuffer> >
class rle_targa_decoder
: public rle_decoder< rgba_pixel_8, InputBuffer, OutputBuffer >
{
public:
/** \brief Type of the input buffer. */
typedef InputBuffer input_buffer_type;
/** \brief Type of the output buffer. */
typedef OutputBuffer output_buffer_type;
private:
virtual void
read_mode( input_buffer_type& input, output_buffer_type& output );
}; // class rle_targa_decoder
/*--------------------------------------------------------------------*/
/** \brief RLE decoder for 32 bpp targa images. */
typedef
rle_targa_decoder< file_input_buffer<rgba_pixel_8> > rle32_decoder;
/*--------------------------------------------------------------------*/
/** \brief RLE decoder for 24 bpp targa images. */
typedef
rle_targa_decoder< file_input_buffer<rgb_pixel_8> > rle24_decoder;
/*--------------------------------------------------------------------*/
/** \brief RLE decoder for 16 bpp targa images. */
typedef rle_targa_decoder< file_input_buffer<pixel16> > rle16_decoder;
/*--------------------------------------------------------------------*/
/** \brief RLE decoder for color mapped 8 bpp targa images. */
typedef rle_targa_decoder< mapped_file_input_buffer<pixel8> >
rle8_decoder;
public:
reader( image& img );
reader( image& img, std::istream& f );
void load( std::istream& f );
private:
void check_if_targa( std::istream& f ) const;
void load_palette
( const header& h, std::istream& f, color_palette32& palette ) const;
void load_color_mapped( const header& h, std::istream& f );
void load_rle_color_mapped( const header& h, std::istream& f );
void load_true_color( const header& h, std::istream& f );
void load_rle_true_color( const header& h, std::istream& f );
template<typename Pixel>
void load_color_mapped_raw
( const header& h, std::istream& f, const color_palette32& palette );
template<typename Decoder>
void decompress_rle_color_mapped
( const header& h, std::istream& f, const color_palette32& palette );
template<typename Pixel>
void load_true_color_raw( const header& h, std::istream& f );
template<typename Decoder>
void decompress_rle_true_color( const header& h, std::istream& f );
template<typename Pixel>
void
load_palette_content( std::istream& f, color_palette32& palette ) const;
private:
/** \brief The image in which we store the data we read. */
image& m_image;
}; // class reader
/*----------------------------------------------------------------------*/
/**
* \brief This class write an image in a targa file.
* \author Julien Jorge
*/
class writer : private file_structure
{
public:
/*--------------------------------------------------------------------*/
/**
* \brief The type of the output buffer associated with the file when
* encoding RLE data.
*
* \b Template \b parameters
* - \a Pixel, the type of the pixels in the output buffer.
*/
template<typename Pixel>
class file_output_buffer
{
public:
/** \brief The type of the pixels in the input buffer. */
typedef Pixel pixel_type;
/** \brief The type of the patterns to encode. */
typedef pixel_type pattern_type;
public:
file_output_buffer( std::ostream& os );
void encode( unsigned int n, pattern_type pattern );
template<typename Iterator>
void raw( Iterator first, Iterator last );
unsigned int min_interesting() const;
unsigned int max_encodable() const;
/**
* \brief Write a pixel in the stream and set its value in the good
* order.
* \param p The pixel to write.
*/
void order_pixel_bytes( const pixel_type& p );
private:
/** \brief The stream in which we write. */
std::ostream& m_stream;
}; // class file_output_buffer
/*--------------------------------------------------------------------*/
/**
* \brief RLE encoder for targa format
*
* \b Template \b parameters :
* - \a Pixel, the type of the pixels in the file.
*
* \author Julien Jorge
*/
template<typename Pixel>
class rle_targa_encoder
: public rle_encoder< file_output_buffer<Pixel> >
{
public:
/** \brief Type of the output buffer. */
typedef file_output_buffer<Pixel> output_buffer_type;
}; // class rle_targa_encoder
/*--------------------------------------------------------------------*/
/** \brief RLE encoder for 32 bpp targa images. */
typedef rle_targa_encoder<rgba_pixel_8> rle32_encoder;
public:
writer( const image& img );
writer( const image& img, std::ostream& f, bool rle );
void save( std::ostream& f, bool rle ) const;
private:
void save_true_color( std::ostream& os ) const;
void save_rle_true_color( std::ostream& os ) const;
private:
/** \brief The image from which we read the data. */
const image& m_image;
}; // class writer
public:
targa( unsigned int w, unsigned int h );
targa( const image& that );
targa( std::istream& f );
void save( std::ostream& os, bool rle ) const;
}; // class targa
} // namespace graphic
} // namespace claw
#include <claw/impl/targa_writer.tpp>
#include <claw/impl/targa_reader.tpp>
#endif // __CLAW_TARGA_HPP__
|