|
1 #include "FloatPlane.h" |
|
2 #include <cstring> |
|
3 |
|
4 extern "C" { |
|
5 #include <stdio.h> |
|
6 #include <pam.h> |
|
7 } |
|
8 |
|
9 FloatPlane::FloatPlane() |
|
10 { |
|
11 setMaxMinValue(); |
|
12 ready = false; |
|
13 } |
|
14 |
|
15 void FloatPlane::setMaxMinValue(unsigned int max, unsigned int min) |
|
16 { |
|
17 MAXVALUE=max; |
|
18 MINVALUE=min; |
|
19 } |
|
20 |
|
21 FloatPlane::FloatPlane(float *_ptr) |
|
22 { |
|
23 ptr = _ptr; |
|
24 setMaxMinValue(); |
|
25 ready = true; |
|
26 } |
|
27 |
|
28 FloatPlane::FloatPlane(const unsigned int _width, const unsigned int _height) |
|
29 { |
|
30 allocate(_width, _height); |
|
31 |
|
32 setMaxMinValue(); |
|
33 ready = true; |
|
34 } |
|
35 |
|
36 void FloatPlane::allocate(const unsigned int _width, const unsigned int _height) |
|
37 { |
|
38 width = _width; |
|
39 height = _height; |
|
40 ptr = new float[width*height]; |
|
41 ready = true; |
|
42 } |
|
43 |
|
44 void FloatPlane::writePGM(const char * filename) |
|
45 { |
|
46 struct pam outpam; |
|
47 unsigned int row; |
|
48 tuple *tuplerow; |
|
49 |
|
50 FILE* file; |
|
51 |
|
52 if (!ready) |
|
53 return; |
|
54 |
|
55 file = fopen(filename, "wb"); |
|
56 if (file == NULL) |
|
57 return; |
|
58 |
|
59 outpam.size = sizeof(outpam); |
|
60 outpam.len = outpam.size; |
|
61 outpam.file = file; |
|
62 outpam.format = PGM_FORMAT; |
|
63 outpam.plainformat = 0; /* false */ |
|
64 outpam.width = width; |
|
65 outpam.height = height; |
|
66 outpam.depth = 1; /* Grayscale - 1 sample per tuple */ |
|
67 outpam.allocation_depth = 0; /* Same as depth. */ |
|
68 outpam.maxval = MAXVALUE; |
|
69 std::strcpy(outpam.tuple_type, "GRAYSCALE"); /* PGM non transparent */ |
|
70 |
|
71 /* needs size,len,file,format,height,width,depth,maxval, tuple_type */ |
|
72 pnm_writepaminit(&outpam); |
|
73 |
|
74 tuplerow = pnm_allocpamrow(&outpam); |
|
75 |
|
76 for (row = 0; row < height; row++) { |
|
77 unsigned int column; |
|
78 for (column = 0; column < width; ++column) { |
|
79 /* Only one plane */ |
|
80 /* float to int! */ |
|
81 tuplerow[column][0] = ptr[column+width*row]; |
|
82 } |
|
83 pnm_writepamrow(&outpam, tuplerow); |
|
84 } |
|
85 |
|
86 pnm_freepamrow(tuplerow); |
|
87 fclose(file); |
|
88 } |
|
89 |
|
90 unsigned int FloatPlane::getMaxValue() |
|
91 { |
|
92 return MAXVALUE; |
|
93 } |
|
94 |
|
95 unsigned int FloatPlane::getWidth() |
|
96 { |
|
97 return width; |
|
98 } |
|
99 |
|
100 unsigned int FloatPlane::getHeight() |
|
101 { |
|
102 return height; |
|
103 } |
|
104 |
|
105 float * FloatPlane::getPtr() |
|
106 { |
|
107 return ptr; |
|
108 } |
|
109 |
|
110 void FloatPlane::free() |
|
111 { |
|
112 delete[] ptr; |
|
113 } |