-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage.cc
More file actions
167 lines (140 loc) · 4.17 KB
/
image.cc
File metadata and controls
167 lines (140 loc) · 4.17 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
// Created by Ioannis Stamos
// Modified by Wei Shi
#include <iostream>
#include <string.h>
#include "image.h"
using namespace std;
namespace image {
Image::Image(const Image &an_image){
AllocateSpaceAndSetSize(an_image.num_rows(), an_image.num_columns());
SetNumberGrayLevels(an_image.num_gray_levels());
for (size_t i = 0; i < num_rows(); ++i)
for (size_t j = 0; j < num_columns(); ++j){
SetPixel(i,j, an_image.GetPixel(i,j));
}
}
Image::Image(Image &&an_image) {
AllocateSpaceAndSetSize(std::move(an_image.num_rows()), std::move(an_image.num_columns()));
SetNumberGrayLevels(std::move(an_image.num_gray_levels()));
for (size_t i = 0; i < num_rows(); ++i)
for (size_t j = 0; j < num_columns(); ++j){
SetPixel(i,j, std::move(an_image.GetPixel(i,j)));
}
}
Image::~Image(){
DeallocateSpace();
}
Image& Image::operator=(const Image& rhs) {
AllocateSpaceAndSetSize(rhs.num_rows(), rhs.num_columns());
SetNumberGrayLevels(rhs.num_gray_levels());
for (size_t i = 0; i < num_rows(); ++i) {
for (size_t j = 0; j < num_columns(); ++j){
SetPixel(i,j,rhs.GetPixel(i,j));
}
}
return *this;
}
Image& Image::operator=(Image&& rhs) {
AllocateSpaceAndSetSize(std::move(rhs.num_rows()), std::move(rhs.num_columns()));
SetNumberGrayLevels(std::move(rhs.num_gray_levels()));
for (size_t i = 0; i < num_rows(); ++i) {
for (size_t j = 0; j < num_columns(); ++j){
SetPixel(i,j,std::move(rhs.GetPixel(i,j)));
}
}
return *this;
}
void Image::AllocateSpaceAndSetSize(size_t num_rows, size_t num_columns) {
if (pixels_ != nullptr){
DeallocateSpace();
}
pixels_ = new int*[num_rows];
for (size_t i = 0; i < num_rows; ++i)
pixels_[i] = new int[num_columns];
num_rows_ = num_rows;
num_columns_ = num_columns;
}
void Image::DeallocateSpace() {
for (size_t i = 0; i < num_rows_; i++)
delete pixels_[i];
delete pixels_;
pixels_ = nullptr;
num_rows_ = 0;
num_columns_ = 0;
}
void Image::Fill(unsigned short value) {
for(size_t i = 0; i < num_rows_; ++i) {
for(size_t j = 0; j < num_columns_; ++j) {
SetPixel(i,j,value);
}
}
}
bool ReadImage(const string &filename, Image *an_image) {
if (an_image == nullptr) abort();
FILE *input = fopen(filename.c_str(),"rb");
if (input == 0) {
cout << "ReadImage: Cannot open file" << endl;
return false;
}
// Check for the right "magic number".
char line[1024];
if (fread(line, 1, 3, input) != 3 || strncmp(line,"P5\n",3)) {
fclose(input);
cout << "ReadImage: Expected .pgm file" << endl;
return false;
}
// Skip comments.
do
fgets(line, sizeof line, input);
while(*line == '#');
// Read the width and height.
int num_columns,num_rows;
sscanf(line,"%d %d\n", &num_columns, &num_rows);
an_image->AllocateSpaceAndSetSize(num_rows, num_columns);
// Read # of gray levels.
fgets(line, sizeof line, input);
int levels;
sscanf(line,"%d\n", &levels);
an_image->SetNumberGrayLevels(levels);
// read pixel row by row.
for (int i = 0; i < num_rows; ++i) {
for (int j = 0;j < num_columns; ++j) {
const int byte=fgetc(input);
if (byte == EOF) {
fclose(input);
cout << "ReadImage: short file" << endl;
return false;
}
an_image->SetPixel(i, j, byte);
}
}
fclose(input);
return true;
}
bool WriteImage(const string &filename, const Image &an_image) {
FILE *output = fopen(filename.c_str(), "w");
if (output == 0) {
cout << "WriteImage: cannot open file" << endl;
return false;
}
const int num_rows = an_image.num_rows();
const int num_columns = an_image.num_columns();
const int colors = an_image.num_gray_levels();
// Write the header.
fprintf(output, "P5\n"); // Magic number.
fprintf(output, "#\n"); // Empty comment.
fprintf(output, "%d %d\n%03d\n", num_columns, num_rows, colors);
for (int i = 0; i < num_rows; ++i) {
for (int j = 0; j < num_columns; ++j) {
const int byte = an_image.GetPixel(i , j);
if (fputc(byte,output) == EOF) {
fclose(output);
cout << "WriteImage: could not write" << endl;
return false;
}
}
}
fclose(output);
return true;
}
} // namespace ComputerVisionProjects