-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathDIP_MyHeaderFile.h
243 lines (223 loc) · 8.49 KB
/
DIP_MyHeaderFile.h
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
// DIP_MyHeaderFile.h
// hw_1_prob_1_a1
//
// Created by Abinaya Manimaran on 1/19/18.
// Copyright © 2018 Abinaya Manimaran. All rights reserved.
//
#ifndef DIP_MyHeaderFile_h
#define DIP_MyHeaderFile_h
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
// Define all the functions written below ------
void fileRead(char *fileName, unsigned char *imageFromFile, long int imageLen );
unsigned char ** allocMemory2D(unsigned char ** image2D, int row, int col);
unsigned char *** allocMemory3D(unsigned char *** image3D, int row, int col, int bytesPerPixel);
unsigned char ** image1Dto2D(unsigned char ** image2D, unsigned char *image1D, int row, int col);
unsigned char *** image1Dto3D(unsigned char *** image3D, unsigned char *image1D, int row, int col, int bytesPerPixel);
void image2Dto1D(unsigned char ** image2D, unsigned char *image1D, int row, int col);
void image3Dto1D(unsigned char *** image3D, unsigned char *image1D, int row, int col, int bytesPerPixel);
unsigned char ** seperateChannels(unsigned char *** image3D, unsigned char **image2DChannel, int row, int col, int channelNumber);
unsigned char *** combineChannels(unsigned char *** image3D, unsigned char ** image2DChannel1, unsigned char ** image2DChannel2, unsigned char **image2DChannel3, int row, int col, int bytesPerPixel);
void fileWrite(char *fileName, unsigned char *imageToFile, long int imageLen );
void fileWriteHist(char* fileName, unsigned int histData[256]);
void freeMemory2D(unsigned char ** image2D, int row, int col);
void freeMemory3D(unsigned char *** image3D, int row, int col, int bytesPerPixel);
void histogram2DImage(unsigned char ** image2D, unsigned int *histCalculated, int row, int col);
long double ** convert1DtoMatrix(long double ** matrix, long double *array, int ROW, int COL);
// Read Raw file and store it in 1D array --------
void fileRead(char *fileName, unsigned char *imageFromFile, long int imageLen ) {
FILE *file;
if (!(file=fopen(fileName,"rb"))) {
cout << "Cannot open file: " << fileName << endl;
exit(1);
}
fread(imageFromFile, sizeof(unsigned char), imageLen, file);
fclose(file);
}
// Allocate memory for 2D array and initialize to zero --------
unsigned char ** allocMemory2D(unsigned char ** image2D, int row, int col){
image2D = new unsigned char *[row]();
long int temp_count = 0;
for(int i=0; i < row; i++) {
image2D[i] = new unsigned char [col]();
for(int j=0; j < col; j++) {
image2D[i][j] = 0;
temp_count = temp_count + 1;
}
}
return image2D;
}
// Allocate memory for 3D array and initialize to zero --------
unsigned char *** allocMemory3D(unsigned char *** image3D, int row, int col, int bytesPerPixel){
long int temp_count = 0;
image3D = new unsigned char **[row]();
for(int i=0; i < row; i++) {
image3D[i] = new unsigned char *[col]();
for(int j=0; j < col; j++) {
image3D[i][j] = new unsigned char [bytesPerPixel]();
for(int k=0; k < bytesPerPixel; k++) {
image3D[i][j][k] = 0;
temp_count = temp_count + 1;
}
}
}
return image3D;
}
// Convert 1D array to 2D array --------
unsigned char ** image1Dto2D(unsigned char ** image2D, unsigned char *image1D, int row, int col) {
image2D = new unsigned char *[row]();
long int temp_count = 0;
for(int i=0; i < row; i++) {
image2D[i] = new unsigned char [col]();
for(int j=0; j < col; j++) {
image2D[i][j] = image1D[temp_count];
temp_count = temp_count + 1;
}
}
return image2D;
}
// Convert 1D array to 3D array --------
unsigned char *** image1Dto3D(unsigned char *** image3D, unsigned char *image1D, int row, int col, int bytesPerPixel) {
long int temp_count = 0;
image3D = new unsigned char **[row]();
for(int i=0; i < row; i++) {
image3D[i] = new unsigned char *[col]();
for(int j=0; j < col; j++) {
image3D[i][j] = new unsigned char [bytesPerPixel]();
for(int k=0; k < bytesPerPixel; k++) {
image3D[i][j][k] = image1D[temp_count];
temp_count = temp_count + 1;
}
}
}
return image3D;
}
// Convert 2D array to 1D array --------
void image2Dto1D(unsigned char ** image2D, unsigned char * image1D, int row, int col) {
long int temp_count = 0;
for(int i=0; i < row; i++) {
for(int j=0; j < col; j++) {
image1D[temp_count] = image2D[i][j];
temp_count = temp_count + 1;
}
}
}
// Convert 3D array to 1D array --------
void image3Dto1D(unsigned char *** image3D, unsigned char *image1D, int row, int col, int bytesPerPixel) {
long int temp_count = 0;
for(int i=0; i < row; i++) {
for(int j=0; j < col; j++) {
for(int k=0; k < bytesPerPixel; k++) {
image1D[temp_count] = image3D[i][j][k];
temp_count = temp_count + 1;
}
}
}
}
// Seperate channels from 3D image --------
unsigned char ** seperateChannels(unsigned char *** image3D, unsigned char **image2DChannel, int row, int col, int channelNumber) {
image2DChannel = new unsigned char *[row]();
for(int i=0; i < row; i++){
image2DChannel[i] = new unsigned char [col]();
for(int j=0; j < col; j++) {
image2DChannel[i][j] = image3D[i][j][channelNumber];
}
}
return image2DChannel;
}
// Combine channels to form 3D image --------
unsigned char *** combineChannels(unsigned char *** image3D, unsigned char ** image2DChannel1, unsigned char ** image2DChannel2, unsigned char **image2DChannel3, int row, int col, int bytesPerPixel) {
image3D = new unsigned char **[row]();
for(int i=0; i < row; i++) {
image3D[i] = new unsigned char *[col]();
for(int j=0; j < col; j++) {
image3D[i][j] = new unsigned char [bytesPerPixel]();
image3D[i][j][0] = image2DChannel1[i][j];
image3D[i][j][1] = image2DChannel2[i][j];
image3D[i][j][2] = image2DChannel3[i][j];
}
}
return image3D;
}
// Write 1D array to a raw file --------
void fileWrite(char *fileName, unsigned char *imageToFile, long int imageLen ) {
FILE *file;
if (!(file=fopen(fileName,"wb"))) {
cout << "Cannot open file: " << fileName << endl;
exit(1);
}
fwrite(imageToFile, sizeof(unsigned char), imageLen, file);
fclose(file);
}
void fileWriteHist(char* fileName, unsigned int histData[256]) {
FILE *file;
file = fopen(fileName, "w");
if (file != NULL)
{
for (int i = 0; i < 256; i++)
{
fprintf(file, "%d\t%u\n", i, histData[i]);
}
fclose(file);
//cout << "File " << fileName << " written successfully !!!" << endl;
}
else
{
cout << "Cannot open file " << fileName << endl;
}
}
// Deallocate memory from 2D array --------
void freeMemory2D(unsigned char ** image2D, int row, int col) {
for (int i=0; i<row; i++) {
delete[] image2D[i];
}
delete[] image2D;
}
// Deallocate memory from 3D array
void freeMemory3D(unsigned char *** image3D, int row, int col, int bytesPerPixel) {
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
delete[] image3D[i][j];
}
delete[] image3D[i];
}
delete[] image3D;
}
// Histogram given a 2D image
void histogram2DImage(unsigned char ** image2D, unsigned int *histCalculated, int row, int col) {
int indexToIncrement;
for(int i=0; i < row; i++) {
for(int j=0; j < col; j++) {
indexToIncrement = image2D[i][j];
histCalculated[indexToIncrement] = histCalculated[indexToIncrement] + 1;
}
}
}
unsigned char ** convert1DtoMatrix(unsigned char ** matrix, unsigned char *array, int ROW, int COL) {
unsigned int tempCount = 0;
matrix = new unsigned char *[ROW]();
for(int i=0; i < ROW; i++){
matrix[i] = new unsigned char [COL]();
for(int j=0; j < COL; j++){
matrix[i][j] = array[tempCount];
tempCount ++;
}
}
return matrix;
}
long double ** convert1DtoMatrixLongDouble(long double ** matrix, long double *array, int ROW, int COL) {
unsigned int tempCount = 0;
matrix = new long double *[ROW]();
for(int i=0; i < ROW; i++){
matrix[i] = new long double [COL]();
for(int j=0; j < COL; j++){
matrix[i][j] = array[tempCount];
tempCount ++;
}
}
return matrix;
}
#endif /* DIP_MyHeaderFile_h */