-
Notifications
You must be signed in to change notification settings - Fork 0
/
Frame.cpp
207 lines (162 loc) · 4.08 KB
/
Frame.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
//*****************************************************************************
//
// Frame.cpp : Defines the class operations on images
//
// Author - Aditya Dhulipala - based on starter code by Prof. Parag Havaldar
// Code used by students as starter code to display and modify images
//
//*****************************************************************************
#include "Frame.h"
// Constructor and Desctructors
Frame::Frame()
{
Data = NULL;
Width = -1;
Height = -1;
ImagePath[0] = 0;
}
Frame::~Frame()
{
if ( Data )
delete Data;
}
// Copy constructor
Frame::Frame( Frame *otherImage)
{
Height = otherImage->Height;
Width = otherImage->Width;
Data = new char[Width*Height*3];
strcpy(otherImage->ImagePath, ImagePath );
for ( int i=0; i<(Height*Width*3); i++ )
{
Data[i] = otherImage->Data[i];
}
}
// = operator overload
Frame &Frame::operator= (const Frame &otherImage)
{
Height = otherImage.Height;
Width = otherImage.Width;
Data = new char[Width*Height*3];
strcpy( (char *)otherImage.ImagePath, ImagePath );
for ( int i=0; i<(Height*Width*3); i++ )
{
Data[i] = otherImage.Data[i];
}
return *this;
}
// Frame::ReadImage
// Function to read the image given a path
bool Frame::ReadImage()
{
// Verify ImagePath
if (ImagePath[0] == 0 || Width < 0 || Height < 0 )
{
fprintf(stderr, "Frame or Frame properties not defined");
fprintf(stderr, "Usage is `Frame.exe Imagefile w h`");
return false;
}
// Create a valid output file pointer
FILE *IN_FILE;
IN_FILE = fopen(ImagePath, "rb");
if ( IN_FILE == NULL )
{
fprintf(stderr, "Error Opening File for Reading");
return false;
}
// Create and populate RGB buffers
int i;
char *Rbuf = new char[Height*Width];
char *Gbuf = new char[Height*Width];
char *Bbuf = new char[Height*Width];
for (i = 0; i < Width*Height; i ++)
{
Rbuf[i] = fgetc(IN_FILE);
}
for (i = 0; i < Width*Height; i ++)
{
Gbuf[i] = fgetc(IN_FILE);
}
for (i = 0; i < Width*Height; i ++)
{
Bbuf[i] = fgetc(IN_FILE);
}
// Allocate Data structure and copy
Data = new char[Width*Height*3];
for (i = 0; i < Height*Width; i++)
{
Data[3*i] = Bbuf[i];
Data[3*i+1] = Gbuf[i];
Data[3*i+2] = Rbuf[i];
}
// Add image data to Mat for OpenCV
MatData = Mat(Height, Width, CV_8UC3, Data);
// Clean up and return
delete Rbuf;
delete Gbuf;
delete Bbuf;
fclose(IN_FILE);
return true;
}
// Frame functions defined here
bool Frame::WriteImage()
{
// Verify ImagePath
// Verify ImagePath
if (ImagePath[0] == 0 || Width < 0 || Height < 0 )
{
fprintf(stderr, "Frame or Frame properties not defined");
return false;
}
// Create a valid output file pointer
FILE *OUT_FILE;
OUT_FILE = fopen(ImagePath, "wb");
if ( OUT_FILE == NULL )
{
fprintf(stderr, "Error Opening File for Writing");
return false;
}
// Create and populate RGB buffers
int i;
char *Rbuf = new char[Height*Width];
char *Gbuf = new char[Height*Width];
char *Bbuf = new char[Height*Width];
for (i = 0; i < Height*Width; i++)
{
Bbuf[i] = Data[3*i];
Gbuf[i] = Data[3*i+1];
Rbuf[i] = Data[3*i+2];
}
// Write data to file
for (i = 0; i < Width*Height; i ++)
{
fputc(Rbuf[i], OUT_FILE);
}
for (i = 0; i < Width*Height; i ++)
{
fputc(Gbuf[i], OUT_FILE);
}
for (i = 0; i < Width*Height; i ++)
{
fputc(Bbuf[i], OUT_FILE);
}
// Clean up and return
delete Rbuf;
delete Gbuf;
delete Bbuf;
fclose(OUT_FILE);
return true;
}
// Here is where you would place your code to modify an image
// eg Filtering, Transformation, Cropping, etc.
bool Frame::Modify()
{
// TO DO by student
// sample operation
for ( int i=0; i<Width*Height; i++ )
{
Data[3*i] = 0;
Data[3*i+1] = 0;
}
return false;
}