aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/irrlicht-1.8.1/source/Irrlicht/CImageWriterPPM.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/irrlicht-1.8.1/source/Irrlicht/CImageWriterPPM.cpp')
-rw-r--r--src/others/irrlicht-1.8.1/source/Irrlicht/CImageWriterPPM.cpp105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/others/irrlicht-1.8.1/source/Irrlicht/CImageWriterPPM.cpp b/src/others/irrlicht-1.8.1/source/Irrlicht/CImageWriterPPM.cpp
new file mode 100644
index 0000000..3bc1737
--- /dev/null
+++ b/src/others/irrlicht-1.8.1/source/Irrlicht/CImageWriterPPM.cpp
@@ -0,0 +1,105 @@
1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine".
3// For conditions of distribution and use, see copyright notice in irrlicht.h
4
5#include "CImageWriterPPM.h"
6
7#ifdef _IRR_COMPILE_WITH_PPM_WRITER_
8
9#include "IWriteFile.h"
10#include "IImage.h"
11#include "dimension2d.h"
12#include "irrString.h"
13
14namespace irr
15{
16namespace video
17{
18
19
20IImageWriter* createImageWriterPPM()
21{
22 return new CImageWriterPPM;
23}
24
25
26CImageWriterPPM::CImageWriterPPM()
27{
28#ifdef _DEBUG
29 setDebugName("CImageWriterPPM");
30#endif
31}
32
33
34bool CImageWriterPPM::isAWriteableFileExtension(const io::path& filename) const
35{
36 return core::hasFileExtension ( filename, "ppm" );
37}
38
39
40bool CImageWriterPPM::writeImage(io::IWriteFile *file, IImage *image, u32 param) const
41{
42 char cache[70];
43 int size;
44
45 const core::dimension2d<u32>& imageSize = image->getDimension();
46
47 const bool binary = false;
48
49 if (binary)
50 size = snprintf(cache, 70, "P6\n");
51 else
52 size = snprintf(cache, 70, "P3\n");
53
54 if (file->write(cache, size) != size)
55 return false;
56
57 size = snprintf(cache, 70, "%d %d\n", imageSize.Width, imageSize.Height);
58 if (file->write(cache, size) != size)
59 return false;
60
61 size = snprintf(cache, 70, "255\n");
62 if (file->write(cache, size) != size)
63 return false;
64
65 if (binary)
66 {
67 for (u32 h = 0; h < imageSize.Height; ++h)
68 {
69 for (u32 c = 0; c < imageSize.Width; ++c)
70 {
71 const video::SColor& pixel = image->getPixel(c, h);
72 const u8 r = (u8)(pixel.getRed() & 0xff);
73 const u8 g = (u8)(pixel.getGreen() & 0xff);
74 const u8 b = (u8)(pixel.getBlue() & 0xff);
75 file->write(&r, 1);
76 file->write(&g, 1);
77 file->write(&b, 1);
78 }
79 }
80 }
81 else
82 {
83 s32 n = 0;
84
85 for (u32 h = 0; h < imageSize.Height; ++h)
86 {
87 for (u32 c = 0; c < imageSize.Width; ++c, ++n)
88 {
89 const video::SColor& pixel = image->getPixel(c, h);
90 size = snprintf(cache, 70, "%.3u %.3u %.3u%s", pixel.getRed(), pixel.getGreen(), pixel.getBlue(), n % 5 == 4 ? "\n" : " ");
91 if (file->write(cache, size) != size)
92 return false;
93 }
94 }
95 }
96
97 return true;
98}
99
100
101} // namespace video
102} // namespace irr
103
104#endif
105