-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.cpp
178 lines (134 loc) · 3.33 KB
/
Image.cpp
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
// Copyright (C) Kevin Suffern 2000-2007.
// Copyright (C) Stefan Brumme 2005.
// Copyright (C) Sverre Kvaale 2007.
// This C++ code is for non-commercial purposes only.
// This C++ code is licensed under the GNU General Public License Version 2.
// See the file COPYING.txt for the full license.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "Image.h"
// ---------------------------------------------------- default constructor
Image::Image(void)
: hres(100),
vres(100)
{
}
// ---------------------------------------------------- copy constructor
Image::Image(const Image &image)
: hres(image.hres),
vres(image.vres),
pixels(image.pixels)
{
}
// ---------------------------------------------------- assignment operator
Image &
Image::operator=(const Image &rhs)
{
if (this == &rhs)
return (*this);
hres = rhs.hres;
vres = rhs.vres;
pixels = rhs.pixels;
return (*this);
}
// ---------------------------------------------------- destructor
Image::~Image(void) {}
// ---------------------------------------------------- read_ppm_file
void Image::read_ppm_file(const char *file_name)
{
// read-only binary sequential access
FILE *file = fopen(file_name, "rb");
if (file == 0)
{
cout << "could not open file " << file_name << endl;
}
// PPM header
unsigned char ppm_type;
if (fscanf(file, "P%c\n", &ppm_type) != 1)
{
cout << "Invalid PPM signature" << endl;
}
// only binary PPM supported
if (ppm_type != '6')
{
cout << "Only binary PPM supported" << endl;
}
// skip comments
unsigned char dummy;
while (fscanf(file, "#%c", &dummy))
while (fgetc(file) != '\n')
;
// read image size
if (fscanf(file, "%d %d\n", &hres, &vres) != 2)
{
cout << "Invalid image size" << endl;
}
if (hres <= 0)
cout << "Invalid image width" << endl;
if (vres <= 0)
cout << "Invalid image height" << endl;
// maximum value to be found in the PPM file (usually 255)
unsigned int max_value;
if (fscanf(file, "%d\n", &max_value) != 1)
{
cout << "Invalid max value" << endl;
}
float inv_max_value = 1.0 / (float)max_value;
// allocate memory
pixels.reserve(hres * vres);
// read pixel data
for (unsigned int y = 0; y < vres; y++)
{
for (unsigned int x = 0; x < hres; x++)
{
unsigned char red;
unsigned char green;
unsigned char blue;
if (fscanf(file, "%c%c%c", &red, &green, &blue) != 3)
{
cout << "Invalid image " << file_name << endl;
}
float r = red * inv_max_value;
float g = green * inv_max_value;
float b = blue * inv_max_value;
ColorRGB aux;
aux.red = r;
aux.green = g;
aux.blue = b;
pixels.push_back(aux);
}
}
// close file
fclose(file);
}
// --------------------------------------------------------------------------------------------- get_color
ColorRGB
Image::get_color(int &index)
{
int pixels_size = pixels.size();
ColorRGB red;
red.red = 1;
red.green = 0;
red.blue = 0;
if (index < pixels_size && index >= 0)
{
return (pixels[index]);
}
else
return (red); // useful for debugging
}
ColorRGB
Image::get_color(const int row, const int column) const
{
ColorRGB black;
black.red = 0;
black.green = 0;
black.blue = 0;
int index = column + hres * (vres - row - 1);
int pixels_size = pixels.size();
if (index < pixels_size && index >= 0)
return (pixels[index]);
return black;
std::cout << "Se salio del index\n";
}