INFO804 Introduction à l'informatique graphique
Loading...
Searching...
No Matches
Image2DWriter.h
1#ifndef _IMAGE2DWRITER_HPP_
2#define _IMAGE2DWRITER_HPP_
3
4#include <iostream>
5#include <string>
6#include "Color.h"
7#include "Image2D.h"
8
9namespace rt {
10
11template <typename TValue>
13public:
14 typedef TValue Value;
15 typedef Image2D<Value> Image;
16
17 static bool write( Image & img, std::ostream & output, bool ascii );
18};
19
20template <typename TValue>
21bool
22Image2DWriter<TValue>::write( Image & /* img */, std::ostream & /* output */,
23 bool /* ascii */ )
24{
25 return false;
26}
27
28/// Specialization for gray-level images.
29template <>
30class Image2DWriter<unsigned char> {
31public:
32 typedef unsigned char Value;
33 typedef Image2D<Value> Image;
34
35 static bool write( Image & img, std::ostream & output, bool ascii );
36};
37
38/// Specialization for color images.
39template <>
41public:
42 typedef Color Value;
43 typedef Image2D<Value> Image;
44
45 static bool write( Image & img, std::ostream & output, bool ascii );
46};
47
48
49
50bool
51Image2DWriter<unsigned char>::write( Image & img, std::ostream & output, bool ascii )
52{
53 typedef unsigned char GrayLevel;
54 output << ( ascii ? "P2" : "P5" ) << std::endl;
55 output << "# Generated by You !" << std::endl;
56 output << img.w() << " " << img.h() << std::endl;
57 output << "255" << std::endl;
58 if ( ascii )
59 {
60 for ( Image::Iterator it = img.begin(), itE = img.end(); it != itE; ++it )
61 output << (int) *it << " ";
62 }
63 else
64 {
65 for ( Image::Iterator it = img.begin(), itE = img.end(); it != itE; ++it )
66 output << (GrayLevel) *it;
67 }
68 return true;
69}
70
71
72bool
73Image2DWriter<Color>::write( Image & img, std::ostream & output, bool ascii )
74{
75 output << ( ascii ? "P3" : "P6" ) << std::endl;
76 output << "# Generated by You !" << std::endl;
77 output << img.w() << " " << img.h() << std::endl;
78 output << "255" << std::endl;
79 if ( ascii )
80 {
81 for ( Image::Iterator it = img.begin(), itE = img.end(); it != itE; ++it )
82 {
83 Color c = *it;
84 output << (int) (c.r()*255.0f) << " " << (int) (c.g()*255.0f) << " " << (int) (c.b()*255.0f) << " ";
85 }
86 }
87 else
88 {
89 for ( Image::Iterator it = img.begin(), itE = img.end(); it != itE; ++it )
90 {
91 Color c = *it;
92 unsigned char red = (unsigned char) (c.r()*255.0f);
93 unsigned char green = (unsigned char) (c.g()*255.0f);
94 unsigned char blue = (unsigned char) (c.b()*255.0f);
95 output << red << green << blue;
96 }
97 }
98 return true;
99}
100
101} // namespace rt
102
103#endif // _IMAGE2DWRITER_HPP_
Classe générique pour représenter des images 2D.
Definition Image2D.h:10
Namespace RayTracer.
Definition Color.h:11