2d array to and from external file

Hello all! After many hours...I think I finally understand a hw problem. However I am having two issues..one is minor.

Assignment: treat an image file as a quad (2d array) and then break up the array into quads, determine the average color of the quad and the variance, then depending on whether we are at a single pixel or the variance is below a threshold, write the entire quad or pixel with the average.
Write the final compressed array to a new pgma file.

1st minor problem:
When I am first creating a 2d array from my pgma file, I am somehow missing the 1st important element that I need-- I am assuming that it never actually gets written to the array?

2nd huge problem... when I uncomment my compressImage function, I end up in an infinite loop and I start outputting data to the console, and never actually write a compressed image file...can anyone please help?

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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

int imageMatrix [512][512];
int outputMatrix [512][512];

using namespace std;



void compressImage(int x, int y, int width, int height, int threshold)
{
	int sum;                             //when i do make it to this stage it seems like the outputMatrix is getting output to the console and gets stuck in an infinite loop? 
	int average;
	double temp;
	double variance;
	int i, j, m, n;
	
	for (i = 0; i < width; i++)						
	{
		for (int j = 0; j < height; j++)
		{						
			sum += imageMatrix[i][j];	
		}
	}		
	average = sum/(i*j);											//calculate average
	
	
	for (m = 0; m < width; i++)
	{
		for (int n = 0; n < height; j++)
		{
			temp = (imageMatrix[m][n] - average) * (imageMatrix[m][n] - average);		
		}
	}	
	variance = temp / ((m * n)-1);									//calculate variance
	
	
	
	if(width == 1 || height == 1)
	{
		outputMatrix[x][y] == imageMatrix[x][y];					//if at single pixel, write pixel to output array
	}
	
	else if(variance <= threshold)
	{
		for(i = 0; i < width; i++)								// if quad variance is < threshold, write whole quad to output array
		{
			for (j = 0; j < height; j++) {
				outputMatrix[i][j] = average;
			}
		}
	}
	else
	{
		compressImage(x, y, width/2, height/2, threshold);
		compressImage(x + (width/2), y, width - (width/2), height/2, threshold);
		compressImage( x + (width/2), y + height/2, width - width/2 , height - height/2, threshold); 
		compressImage(x, y + height/2, width/2, height - height/2, threshold);
	}
	
	//const char* const OutputFileName = "outputBaboon.pgma";
	ofstream dataOut("outputBaboon.pgma");
	//dataOut.write(outputMatrix);
	//dataOut.close();
	
	
	
	
	for (i = 0; i < width; i++) 
	{
		for (j = 0; j < height; j++) 
		{
			dataOut << outputMatrix[i][j];
		}
	}
	dataOut.close();
	 
	
}


int main (int argc, char * const argv[]) {
	
	const char* const FileName = ("/Users/seanmacbook/Desktop/ClaudiaSchool/Multimedia/Alvarado_Multimedia_Proj1/build/Debug/baboon.pgma");
	std::ifstream dataIn;
	dataIn.open(FileName);
	
	if(!dataIn)
		std::cout << "Can't open file " << FileName;
	else {
		string line;
		string line2;
		string line3;
		string line4;
		int width, height;
		getline(dataIn, line);
		getline(dataIn, line2);
		cout << line;
		cout << line2;
		dataIn >> width;
		cout << width << " ";
		dataIn >> height;
		cout << height << " ";
		getline(dataIn, line3);
		cout << line3 << " ";
		getline(dataIn, line4);
		cout << line4;
		//cout << "\n";
		int imageMatrix[width][height];
		

		for (int i = 0; i < width; i++) {             //i think this is one issue
			for (int j = 0; j < height; j++) {
				
				dataIn >> imageMatrix[i][j];
			}
		}	

		for (int i = 0; i < width; i++) {
			for (int j = 1; j < height; j++) {
				
				cout << imageMatrix[i][j];
				cout << " ";
			}			
		}
	
		
		//compressImage(0, 0, width, height, 126);
	}
	
	
    // insert code here...
    cout << "Hello, World!\n";
    return 0;
}

Last edited on
QuadTree(int imageData[][], int width, int height)
is what you're looking for.

std::cout << "/endl";
I think what you mean is "\n".

QuadTree(imageData[rows][cols], 0, 0, rows, cols, threshold);
You can't call other constructors from a constructor.
More generally, you can't call constructors, period.
Topic archived. No new replies allowed.