-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPNG.cpp
211 lines (171 loc) · 5.66 KB
/
PNG.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
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
/**
* @file PNG.cpp
* Implementation of a simple PNG class using HSLAPixels and the lodepng PNG library.
*
* @author CS 225: Data Structures
* @version 2018r1
*/
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <cassert>
#include "lodepng.h"
#include "PNG.h"
#include "RGB_HSL.h"
void PNG::_copy(PNG const & other) {
// Clear self
delete[] imageData_;
// Copy `other` to self
width_ = other.width_;
height_ = other.height_;
imageData_ = new HSLAPixel[width_ * height_];
for (unsigned i = 0; i < width_ * height_; i++) {
imageData_[i] = other.imageData_[i];
}
}
PNG::PNG() {
width_ = 0;
height_ = 0;
imageData_ = NULL;
}
PNG::PNG(unsigned int width, unsigned int height) {
width_ = width;
height_ = height;
imageData_ = new HSLAPixel[width * height];
}
PNG::PNG(PNG const & other) {
imageData_ = NULL;
_copy(other);
}
PNG::~PNG() {
delete[] imageData_;
}
PNG const & PNG::operator=(PNG const & other) {
if (this != &other) { _copy(other); }
return *this;
}
bool PNG::operator==(PNG const & other) const {
if (width_ != other.width_) { return false; }
if (height_ != other.height_) { return false; }
for (unsigned i = 0; i < width_ * height_; i++) {
HSLAPixel & p1 = imageData_[i];
HSLAPixel & p2 = other.imageData_[i];
if (p1 != p2) { return false; }
}
return true;
}
bool PNG::operator!=(PNG const & other) const {
return !(*this == other);
}
HSLAPixel & PNG::getPixel(unsigned int x, unsigned int y) const {
if (width_ == 0 || height_ == 0) {
cerr << "ERROR: Call to cs225::PNG::getPixel() made on an image with no pixels." << endl;
assert(width_ > 0);
assert(height_ > 0);
}
if (x >= width_) {
cerr << "WARNING: Call to cs225::PNG::getPixel(" << x << "," << y << ") tries to access x=" << x
<< ", which is outside of the image (image width: " << width_ << ")." << endl;
cerr << " : Truncating x to " << (width_ - 1) << endl;
x = width_ - 1;
}
if (y >= height_) {
cerr << "WARNING: Call to cs225::PNG::getPixel(" << x << "," << y << ") tries to access y=" << y
<< ", which is outside of the image (image height: " << height_ << ")." << endl;
cerr << " : Truncating y to " << (height_ - 1) << endl;
y = height_ - 1;
}
unsigned index = x + (y * width_);
return imageData_[index];
}
bool PNG::readFromFile(string const & fileName) {
vector<unsigned char> byteData;
unsigned error = lodepng::decode(byteData, width_, height_, fileName);
if (error) {
cerr << "PNG decoder error " << error << ": " << lodepng_error_text(error) << endl;
return false;
}
delete[] imageData_;
imageData_ = new HSLAPixel[width_ * height_];
for (unsigned i = 0; i < byteData.size(); i += 4) {
rgbaColor rgb;
rgb.r = byteData[i];
rgb.g = byteData[i + 1];
rgb.b = byteData[i + 2];
rgb.a = byteData[i + 3];
hslaColor hsl = rgb2hsl(rgb);
HSLAPixel & pixel = imageData_[i/4];
pixel.h = hsl.h;
pixel.s = hsl.s;
pixel.l = hsl.l;
pixel.a = hsl.a;
}
return true;
}
bool PNG::writeToFile(string const & fileName) {
unsigned char *byteData = new unsigned char[width_ * height_ * 4];
for (unsigned i = 0; i < width_ * height_; i++) {
hslaColor hsl;
hsl.h = imageData_[i].h;
hsl.s = imageData_[i].s;
hsl.l = imageData_[i].l;
hsl.a = imageData_[i].a;
rgbaColor rgb = hsl2rgb(hsl);
byteData[(i * 4)] = rgb.r;
byteData[(i * 4) + 1] = rgb.g;
byteData[(i * 4) + 2] = rgb.b;
byteData[(i * 4) + 3] = rgb.a;
}
unsigned error = lodepng::encode(fileName, byteData, width_, height_);
if (error) {
cerr << "PNG encoding error " << error << ": " << lodepng_error_text(error) << endl;
}
delete[] byteData;
return (error == 0);
}
unsigned int PNG::width() const {
return width_;
}
unsigned int PNG::height() const {
return height_;
}
void PNG::resize(unsigned int newWidth, unsigned int newHeight) {
// Create a new vector to store the image data for the new (resized) image
HSLAPixel * newImageData = new HSLAPixel[newWidth * newHeight];
// Copy the current data to the new image data, using the existing pixel
// for coordinates within the bounds of the old image size
for (unsigned x = 0; x < newWidth; x++) {
for (unsigned y = 0; y < newHeight; y++) {
if (x < width_ && y < height_) {
HSLAPixel & oldPixel = this->getPixel(x, y);
HSLAPixel & newPixel = newImageData[ (x + (y * newWidth)) ];
newPixel = oldPixel;
}
}
}
// Clear the existing image
delete[] imageData_;
// Update the image to reflect the new image size and data
width_ = newWidth;
height_ = newHeight;
imageData_ = newImageData;
}
std::size_t PNG::computeHash() const {
std::hash<float> hashFunction;
std::size_t hash = 0;
for (unsigned x = 0; x < this->width(); x++) {
for (unsigned y = 0; y < this->height(); y++) {
HSLAPixel & pixel = this->getPixel(x, y);
hash = (hash << 1) + hash + hashFunction(pixel.h);
hash = (hash << 1) + hash + hashFunction(pixel.s);
hash = (hash << 1) + hash + hashFunction(pixel.l);
hash = (hash << 1) + hash + hashFunction(pixel.a);
}
}
return hash;
}
std::ostream & operator << ( std::ostream& os, PNG const& png ) {
os << "PNG(w=" << png.width() << ", h=" << png.height() << ", hash=" << std::hex << png.computeHash() << std::dec << ")";
return os;
}