-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathimwrite.cpp
More file actions
executable file
·231 lines (215 loc) · 6.25 KB
/
Copy pathimwrite.cpp
File metadata and controls
executable file
·231 lines (215 loc) · 6.25 KB
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
/*
* This file is part of Filmulator.
*
* Copyright 2013 Omer Mano and Carlo Vaccari
*
* Filmulator is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Filmulator 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Filmulator. If not, see <http://www.gnu.org/licenses/>
*/
#include "header.hpp"
bool ppmb_write_data ( ofstream &output, int xsize, int ysize,
matrix<float> &densityr, matrix<float> &densityg, matrix<float> &densityb,
bool sixteen_bit )
//****************************************************************************80
//
// Purpose:
//
// PPMB_WRITE_DATA writes the data for a binary portable pixel map file.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 11 April 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, ofstream OUTPUT, a pointer to the file to contain the binary
// portable pixel map data.
//
// Input, int XSIZE, YSIZE, the number of rows and columns of data.
//
// Input, unsigned char *R, *G, *B, the arrays of XSIZE by YSIZE
// data values.
//
// Output, bool PPMB_WRITE_DATA, is true if an error occurred.
//
{
int i;
int j;
unsigned short out;
unsigned char out2;
if(sixteen_bit)
{
for ( j = 0; j < ysize; j++ )
{
for ( i = 0; i < xsize; i++ )
{
if(densityr(j,i) > 65535)
out = 65535; //clip to white
else if(densityr(j,i) < 0)
out = 0; //clip to black
else
out = (unsigned short) densityr(j,i); //normal values
out = ((out & 0x00ff)<<8)|((out & 0xff00)>>8);
output.write(reinterpret_cast<char*>(&out), sizeof(unsigned short));
if(densityg(j,i) > 65535)
out = 65535; //clip to white
else if(densityg(j,i) < 0)
out = 0; //clip to black
else
out = (unsigned short) densityg(j,i); //normal values
out = ((out & 0x00ff)<<8)|((out & 0xff00)>>8);
output.write(reinterpret_cast<char*>(&out), sizeof(unsigned short));
if(densityb(j,i) > 65535)
out = 65535; //clip to white
else if(densityb(j,i) < 0)
out = 0; //clip to black
else
out = (unsigned short) densityb(j,i); //normal values
out = ((out & 0x00ff)<<8)|((out & 0xff00)>>8);
output.write(reinterpret_cast<char*>(&out), sizeof(unsigned short));
}
}
}
else //8 bit
{
for ( j = 0; j < ysize; j++ )
{
for ( i = 0; i < xsize; i++ )
{
if(densityr(j,i) > 255)
out2 = 255; //clip to white
else if(densityr(j,i) < 0)
out2 = 0; //clip to black
else
out2 = (unsigned char) densityr(j,i); //normal values
output.write(reinterpret_cast<char*>(&out2), sizeof(unsigned char));
if(densityg(j,i) > 255)
out2 = 255; //clip to white
else if(densityg(j,i) < 0)
out2 = 0; //clip to black
else
out2 = (unsigned char) densityg(j,i); //normal values
output.write(reinterpret_cast<char*>(&out2), sizeof(unsigned char));
if(densityb(j,i) > 255)
out2 = 255; //clip to white
else if(densityb(j,i) < 0)
out2 = 0; //clip to black
else
out2 = (unsigned char) densityb(j,i); //normal values
output.write(reinterpret_cast<char*>(&out2), sizeof(unsigned char));
}
}
}
return false;
}
//****************************************************************************80
bool ppmb_write_header ( ofstream &output, int xsize, int ysize, int maxrgb )
//****************************************************************************80
//
// Purpose:
//
// PPMB_WRITE_HEADER writes the header of a binary portable pixel map file.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 11 April 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, ofstream &OUTPUT, a pointer to the file to contain the binary
// portable pixel map data.
//
// Input, int XSIZE, YSIZE, the number of rows and columns of data.
//
// Input, int MAXRGB, the maximum RGB value.
//
// Output, bool PPMB_WRITE_HEADER, is true if an error occurred.
//
{
output << "P6" << " "
<< xsize << " "
<< ysize << " "
<< maxrgb << "\n";
return false;
}
void imwrite(matrix<float> &densityr, matrix<float> &densityg,
matrix<float> &densityb, string output_name, bool sixteen_bit )
{
bool error;
ofstream output;
int maxrgb;
int xsize = densityr.nc();
int ysize = densityr.nr();
//
// Open the output file.
//
output.open ( output_name.c_str( ), ios::binary );
if ( !output )
{
cout << "\n";
cout << "PPMB_WRITE: Fatal error!\n";
cout << " Cannot open the output file " << output_name << ".\n";
return;
}
//
// Compute the maximum.
//
if ( sixteen_bit)
maxrgb = 65535;
else
maxrgb = 255;
//
// Write the header.
//
error = ppmb_write_header ( output, xsize, ysize, maxrgb );
if ( error )
{
cout << "\n";
cout << "PPMB_WRITE: Fatal error!\n";
cout << " PPMB_WRITE_HEADER failed.\n";
return;
}
//
// Write the data.
//
error = ppmb_write_data ( output, xsize, ysize, densityr, densityg, densityb,
sixteen_bit);
if ( error )
{
cout << "\n";
cout << "PPMB_WRITE: Fatal error!\n";
cout << " PPMB_WRITE_DATA failed.\n";
return;
}
//
// Close the file.
//
output.close ( );
return;
}