-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixel.cpp
More file actions
65 lines (55 loc) · 1.38 KB
/
pixel.cpp
File metadata and controls
65 lines (55 loc) · 1.38 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
#include <iostream>
#include <string.h>
#include <stdio.h>
#include "pixel.hpp"
Pixels::Pixels() {
arrayValues = nullptr;
length = 0;
}
Pixels::Pixels(const char* values) {
this->arrayValues = nullptr;
length = 0;
if (values) {
this->length = strlen(values);
if (!(this->arrayValues = new char[length]))
throw std::bad_alloc();
memcpy(this->arrayValues, values, length*sizeof(char));
}
}
int Pixels::readArray(FILE *stream, const long &bytesToRead) {
if (!stream)
throw "Invalid stream";
this->length = bytesToRead;
std::cout << this->length << std::endl;
this->arrayValues = new char[this->length];
long ret;
ret = fread(this->arrayValues, sizeof(char), bytesToRead, stream);
if (ret == 0)
throw strerror(errno);
if (ret < bytesToRead)
throw std::out_of_range("Didn't read all bytes");
}
int Pixels::writeArray(FILE *stream) const {
if (this->arrayValues) {
errno = 0;
int ret = fwrite(this->arrayValues, sizeof(char), this->length, stream);
if (ret > 0)
return 1;
}
return -2;
}
char& Pixels::operator[](const int &index) {
if (index < length && index >= 0)
return this->arrayValues[index];
}
const char& Pixels::operator[](const int &index) const {
if (index < length && index >= 0)
return this->arrayValues[index];
}
char* Pixels::getArray() const {
return this->arrayValues;
}
Pixels::~Pixels() {
if (this->arrayValues)
delete[] arrayValues;
}