-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageFile.cpp
More file actions
92 lines (80 loc) · 1.94 KB
/
ImageFile.cpp
File metadata and controls
92 lines (80 loc) · 1.94 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
/*
Author: Jon Shidal
Purpose: CSE 332 lab 5, defines the ImageFile class
*/
#include "ImageFile.h"
#include "AbstractFileVisitor.h"
#include<iostream>
using namespace std;
ImageFile::ImageFile(string n) : name(n), dimensions(0), parent(nullptr) {}
unsigned int ImageFile::getSize() {
return contents.size();
}
string ImageFile::getName() {
return name;
}
vector<char> ImageFile::read() {
return contents;
}
void ImageFile::accept(AbstractFileVisitor* fv) {
if (fv) { fv->visit(this); }
}
int ImageFile::write(vector<char> data) {
if (data.size() == 0) { // JS add to starter code
dimensions = 0;
contents.clear();
}
char dim = data[data.size() - 1] - '0'; // substract ascii value of '0' to convert char to int.
if (dim >= 0 && dim * dim == data.size() - 1) {
contents.clear();
for (size_t i = 0; i < data.size() - 1; ++i) {
if (data[i] == 'X') {
contents.push_back('X');
}
else if (data[i] == ' ') {
contents.push_back(' ');
}
else {
dimensions = 0;
contents.clear();
return invalidpixel;
}
}
dimensions = dim;
return success;
}
else {
dimensions = 0;
contents.clear();
return sizemismatch;
}
}
int ImageFile::append(vector<char> data) {
return unabletoappend;
}
size_t ImageFile::coordsToIndex(size_t x, size_t y) {
return dimensions * y + x;
}
int ImageFile::addChild(AbstractFile*) {
return notacomposite;
}
int ImageFile::removeChild(string name) {
return notacomposite;
}
AbstractFile* ImageFile::getChild(std::string name) {
return nullptr;
}
AbstractFile* ImageFile::getParent() {
return parent;
}
void ImageFile::setParent(AbstractFile* p) {
parent = p;
}
ImageFile* ImageFile::clone() {
ImageFile* temp = new ImageFile(name);
vector<char> temp_contents = this->read();
temp_contents.push_back((char)(dimensions + '0'));
temp->write(temp_contents);
temp->setParent(nullptr);
return temp;
}