-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWriteImage.cpp
More file actions
53 lines (37 loc) · 913 Bytes
/
WriteImage.cpp
File metadata and controls
53 lines (37 loc) · 913 Bytes
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
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
#include "image.h"
void writeImage(char fname[], ImageType& image)
{
int i, j;
int N, M, Q;
unsigned char *charImage;
ofstream ofp;
image.getImageInfo(N, M, Q);
charImage = (unsigned char *) new unsigned char [M*N];
// convert the integer values to unsigned char
int val;
for(i=0; i<N; i++)
for(j=0; j<M; j++) {
image.getPixelVal(i, j, val);
charImage[i*M+j]=(unsigned char)val;
}
ofp.open(fname, ios::out | ios::binary);
if (!ofp) {
cout << "Can't open file: " << fname << endl;
exit(1);
}
ofp << "P5" << endl;
ofp << M << " " << N << endl;
ofp << Q << endl;
ofp.write( reinterpret_cast<char *>(charImage), (M*N)*sizeof(unsigned char));
if (ofp.fail()) {
cout << "Can't write image " << fname << endl;
exit(0);
}
ofp.close();
delete [] charImage;
}