/
image.cpp
168 lines (133 loc) · 3.65 KB
/
image.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
// Copyright 2019 Gene Callahan
/*
* This is the implementation of our image class.
* It is designed to teach class hierarchy and buffer
* allocation.
* */
#include <string>
#include <iostream>
using namespace std;
#include "./image.h"
/*
* "Ordinary" constructor initializing each field:
* */
Image::Image(int width, int height, const string& flnm)
: width(width), height(height) {
image_buf = new unsigned char[image_sz()];
}
/*
* This is the `Image` *copy constructor*. We have to handle
* properly copying the `image_buf` to the target object.
* We use `copy_fields()` so we can share copy code with the
* assignment operator.
* */
Image::Image(const Image& img2) {
cout << "In image copy constructor\n";
copy_fields(img2);
}
/*
* `Image` *destructor* must *free* the image buf so
* that the memory can be used for other purposes.
* */
Image::~Image() {
if (image_buf != nullptr) delete image_buf;
}
/*
* The `Image` assignment operator has to delete the image buf is
* not null, then do the copy like the *copy constructor*.
* */
Image& Image::operator=(const Image& img2) {
if (image_buf != nullptr) delete image_buf;
copy_fields(img2);
return *this;
}
/*
* This is the base class `display()` operator, to illustrate
* *polymorphism*.
* */
void Image::display(const string& s) const {
cout << s << endl;
}
int Image::image_sz() { return width * height; }
/*
* We extract the copy into its own method so that it can be shared
* by the copy constructor and assignment.
* */
void Image::copy_fields(const Image& img2) {
width = img2.width;
height = img2.height;
image_buf = new unsigned char[image_sz()];
for (int i = 0; i < image_sz(); i++)
image_buf[i] = img2.image_buf[i];
}
/*
* Correct way to call base class copy constructor:
Gif::Gif(const Gif& img2) : Image(img2) {
}
* */
/*
* Incorrect way to call it:
Gif::Gif(const Gif& img2) {
Image(img2);
}
* */
/*
* Specialized over-riding display method for Gif.
* In it we call the parent display method with
* `Image::display()`.
* */
void Gif::display(const string& s) const {
Image::display("Gif");
}
/*
* Gif has its own `compress()` with an int parameter.
* It will hide the base class `compress()` unless we
* explicitly prevent it from doing so.
* */
void Gif::compress(int i) {
cout << "compressing at level: " << i << endl;
}
AnimGif::AnimGif(int width, int height, const string& flnm)
: Gif(width, height, flnm) {
image_buf2 = new unsigned char[image_sz()];
}
AnimGif::AnimGif(const AnimGif& img2) : Gif(img2) {
cout << "In AnimGif copy constructor\n";
image_buf2 = new unsigned char[image_sz()];
for (int i = 0; i < image_sz(); i++)
image_buf2[i] = img2.image_buf2[i];
}
AnimGif& AnimGif::operator=(const AnimGif& img2) {
Gif::operator=(img2);
if (image_buf2 != nullptr) delete image_buf2;
for (int i = 0; i < image_sz(); i++)
image_buf2[i] = img2.image_buf2[i];
return *this;
}
AnimGif::~AnimGif() {
if (image_buf2 != nullptr) delete image_buf2;
}
/*
* Specialized over-riding display method for AnimGif.
* In it we call the parent display method with
* `Image::display()`.
void AnimGif::display(const string& s) const {
Image::display("AnimGif");
}
* */
/*
* Specialized over-riding display method for Jpeg.
* In it we call the parent display method with
* `Image::display()`.
* */
void Jpeg::display(const string& s) const {
Image::display("Jpeg");
}
/*
* Specialized over-riding display method for Png.
* In it we call the parent display method with
* `Image::display()`.
* */
void Png::display(const string& s) const {
Image::display("Png");
}