-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.cuh
149 lines (119 loc) · 3.53 KB
/
image.cuh
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
#pragma once
#include <cstdio>
#include <cstdint>
#include <vector_types.h>
class PnmImage {
private:
uint32_t width, height;
uchar3 *pixels;
static float computeError(uchar3 *a1, uchar3 *a2, uint32_t n);
public:
PnmImage() {
width = 0;
height = 0;
pixels = nullptr;
}
PnmImage(uint32_t width, uint32_t height) {
this->width = width;
this->height = height;
this->pixels = (uchar3 *) malloc(width * height * sizeof(uchar3));
}
PnmImage(uint32_t width, uint32_t height, uchar3* pixels) {
this->width = width;
this->height = height;
this->pixels = pixels;
}
PnmImage(const PnmImage &ref) {
this->width = ref.width;
this->height = ref.height;
this->pixels = nullptr;
if (ref.pixels) {
pixels = (uchar3 *) malloc(width * height * sizeof(uchar3));
memcpy(pixels, ref.pixels, width * height * sizeof(uchar3));
}
}
PnmImage &operator=(const PnmImage &ref) {
if (this == &ref)
return *this;
if (pixels)
free(pixels);
this->width = ref.width;
this->height = ref.height;
if (ref.pixels) {
pixels = (uchar3 *) malloc(width * height * sizeof(uchar3));
memcpy(pixels, ref.pixels, width * height * sizeof(uchar3));
}
return *this;
}
~PnmImage() {
if (pixels)
free(pixels);
}
void read(const char *fileName);
void write(const char *fileName);
void compare(const PnmImage &other);
long long sum();
uint32_t getWidth() const;
uint32_t getHeight() const;
uchar3 *getPixels() const;
};
class IntImage {
private:
uint32_t width, height;
int32_t *pixels;
public:
uint32_t getWidth() const;
uint32_t getHeight() const;
int32_t *getPixels() const;
IntImage() {
width = 0;
height = 0;
pixels = nullptr;
}
IntImage(uint32_t width, uint32_t height) {
this->width = width;
this->height = height;
this->pixels = (int32_t *) malloc(width * height * sizeof(int32_t));
}
IntImage(const IntImage &ref) {
this->width = ref.width;
this->height = ref.height;
this->pixels = nullptr;
if (ref.pixels) {
pixels = (int32_t *) malloc(width * height * sizeof(int32_t));
memcpy(pixels, ref.pixels, width * height * sizeof(int32_t));
}
}
IntImage &operator=(const IntImage &ref) {
if (this == &ref)
return *this;
if (pixels)
free(pixels);
this->width = ref.width;
this->height = ref.height;
if (ref.pixels) {
pixels = (int32_t *) malloc(width * height * sizeof(int32_t));
memcpy(pixels, ref.pixels, width * height * sizeof(int32_t));
}
return *this;
}
explicit operator PnmImage() const {
PnmImage outputImage = PnmImage(this->width, this->height);
if (!this->pixels) return outputImage;
for (int i = 0; i < this->height * this->width; ++i) {
int32_t temp = this->pixels[i];
if (temp > 255)
temp = 255;
if (temp < 0)
temp = 0;
outputImage.getPixels()[i] = make_uchar3((char) temp, (char) temp,
(char) temp);
}
return outputImage;
}
long long sum();
~IntImage() {
if (pixels)
free(pixels);
}
};