Repository navigation
Expand file tree
/
Copy pathPixel.cpp
More file actions
188 lines (162 loc) · 5.45 KB
/
Copy pathPixel.cpp
File metadata and controls
188 lines (162 loc) · 5.45 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
#include "Pixel.hpp"
// empty constructor, all 0 (coordinates and RGB color components), again
Pixel::Pixel()
{
coordX=0; // X coordinate
coordY=0; // Y coordinate
colorR=0; // red component of the pixel color
colorG=0; // green component
colorB=0; // blue component
}
// Constructor with X and Y coordinates as parameters. Initializes the color components to 0.
Pixel::Pixel(unsigned int paramX, unsigned int paramY)
{
coordX=paramX;
coordY=paramY;
colorR=0;
colorG=0;
colorB=0;
}
Pixel::Pixel(unsigned int paramX, unsigned int paramY, unsigned char paramR, unsigned char paramG, unsigned char paramB)
{
coordX=paramX;
coordY=paramY;
colorR=paramR;
colorG=paramG;
colorB=paramB;
}
Pixel::~Pixel()
{
}
Pixel* Pixel::clone()
{
return new Pixel(this->getX(), this->getY(), this->getR(), this->getG(), this->getB());
}
unsigned int Pixel::getX()
{
return coordX;
}
unsigned int Pixel::getY()
{
return coordY;
}
unsigned char Pixel::getR()
{
return colorR;
}
unsigned char Pixel::getG()
{
return colorG;
}
unsigned char Pixel::getB()
{
return colorB;
}
// Sets the RGB color components of the pixel
void Pixel::setComponents(unsigned char paramR, unsigned char paramG, unsigned char paramB)
{
colorR=paramR;
colorG=paramG;
colorB=paramB;
}
// Normalizes the pixel color to either full Red, full Green, full Blue, or Black based on thresholds
void Pixel::normalizeToRedGreenOrBlue()
{
if (colorR>190 && colorG<100 && colorB<100)
{
// if red is dominant, set the pixel to full red
colorR=255;
colorG=0;
colorB=0;
}
else
{
if (colorR<100 && colorG>190 && colorB<100)
{
// if green is dominant, set the pixel to full green
colorR=0;
colorG=255;
colorB=0;
}
else
{
if (colorR<100 && colorG<100 && colorB>190)
{
// if blue is dominant, set the pixel to full blue
colorR=0;
colorG=0;
colorB=255;
}
else
{
// Otherwise, set the pixel to Black
colorR=0;
colorG=0;
colorB=0;
}
}
}
}
// Checks if the pixel is black (i.e., if the sum of the RGB components is 0)
bool Pixel::isBlack()
{
return 0==(colorR+colorG+colorB); // si la suma de los tres componentes es 0, el pixel es de color negro
}
bool Pixel::isNotBlack()
{
return 0!=(colorR+colorG+colorB); // si la suma de los tres componentes no es 0, el pixel no es de color negro
}
// Calculates the distance of the pixel from the center of an image with given width and height
unsigned int Pixel::getDistACentro(unsigned int width, unsigned int height)
{
//std::cout << "dist: X " << coordX << " Y " << coordY << "W: " << width << "H: " << height << std::endl;
// calculates the difference between the pixel's X coordinate and the image center
long difX = std::abs((long)coordX - (long)width/2);
// calculates the difference between the pixel's Y coordinate and the image center
long difY = std::abs((long)coordY - (long)height/2);
//std::cout << "dist: X " << difX << " Y " << difY << "dist: " << std::sqrt((difX*difX)+(difY*difY)) << std::endl;
// Return the Euclidean distance from the pixel to the center of the image
return std::sqrt((difX*difX)+(difY*difY));
}
// Extracts RGB values of a pixel from a raw image data array
bool Pixel::getFromRaw(const unsigned char *image, unsigned int width, unsigned int height, unsigned int p_x, unsigned int p_y)
{
bool result = false; // default to false, indicating no success
//check if the coordinates are valid within the image boundaries
if (height < 1 || width < 1 || p_x < 0 || p_y < 0 || p_x >= width || p_y >= height)
{
result = false;
}
else
{
int rowPix = width*3; // Calculate the number of bytes per row (width * 3, because 3 bytes per pixel)
// extracts the Blue, Green, and Red components from the raw image data
colorB = *(image+((p_y*rowPix)+(p_x*3)+0)); // blue
colorG = *(image+((p_y*rowPix)+(p_x*3)+1)); // green
colorR = *(image+((p_y*rowPix)+(p_x*3)+2)); // red
result = true;// successfully extracted the pixel data
}
return result;
}
// Sets the RGB values of a pixel into a raw image data array
bool Pixel::setIntoRaw(unsigned char *image, unsigned int width, unsigned int height, unsigned int p_x, unsigned int p_y)
{/*allows you to forget about details of image internal format of the raw (malloc) image.
* Every Pixel Objects knows how to write itself in an image raw if you provide it the image pointer,
* the coordinates and the size of the image in width and heigth.
*/
bool result = false; // no success
// Check if the coordinates are valid within the image boundaries
if (height < 1 || width < 1 || p_x < 0 || p_y < 0 || p_x >= width || p_y >= height)
{
result = false;
}
else
{
int rowPix = width*3; // Calculate the number of bytes per row
*(image+((p_y*rowPix)+(p_x*3)+0)) = colorB; // blue
*(image+((p_y*rowPix)+(p_x*3)+1)) = colorG; // green
*(image+((p_y*rowPix)+(p_x*3)+2)) = colorR; // red
result = true;// Successfully set the pixel data
}
return result;
}