pixel function C++

I have been trying to get the function pixelData() in class background below to work but to no avail. Could someone let me know how to go about implementing it? I am not allowed to touch the main(). Just the class Background. Thanks!

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
 class Object
{
	private:
	float d;
	
	public:
	Object(float n) : d(n){}
	 
     float depth() const
	{
		return d;
	}
};
class Background :public Object
{
	private:
	char d;
	int w;
	int h;
	int v[];
	
	public:
	Background(char* data, int width, int height, float depth) : d(*data), w(width), h(height), Object(depth) {}
	
	char* pixelData()// Need to get this to work 
	{
	return &d;
        }
};

float frand()
{
	return 10.0f * float(rand()) / float(RAND_MAX);
}

int main()
{
	srand(unsigned(time(0)));
	int count = 0;
	int max_count = 0;

	int width = 100 + rand() % 200;
	int height = 100 + rand() % 200;
	float depth = frand();

	char* data = new char[3 * width * height];
	for (int i = 0; i < width * height; ++i)
	{
		data[3 * i] = 1;
		data[3 * i + 1] = 2;
		data[3 * i + 2] = 3;
		
	}
 	Background background(data, width, height, depth);
	
	delete[] data;
	
	int index = 0;
	while ((index < width*height) &&
		(background.pixelData()[3 * index] == 1) && //Calls pixelData
		(background.pixelData()[3 * index + 1] == 2) &&
		(background.pixelData()[3 * index + 2] == 3))
	{
		++index;
	}
	if (index == width * height)
	{
		++count;
	}
	else
	{
		std::cout << "  - Background::pixelData test failed" << std::endl;
	}
 	++max_count;
}
 
Last edited on
it looks like 'd' should be a vector/array of bytes, not just 1 single byte. you take its address, its a single character, and then you try to treat it like an array, which probably crashes.
Ok, if this is the case, I should treat 'd' as a vector/array of bytes? Meaning that I would need to implement the pixel function with a vector/array? And the data member 'd' should then become 'd[]' in my class background?
yes, as far as I can tell.

what this in main says:
background.pixelData()[3 * index + 2]

is, going from what you tried to write, this:
d[3*index+2]

so at a guess d should be width*height*3 in size.
Last edited on
Hi, I'm not sure if I am attempting it correctly, but is this how its done. Should not be since I am getting a compilation error from this. Would appreciate if anyone could correct me on this. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Background: public Object
{
	private:
	char *d;
	int w;
	int h;
 
	public:
	Background( char* data, int width, int height, float depth) : d(data), w(width), h(height), Object(depth) {}
	
	
	  char* pixelData()
	{
		  int size=3*w*h;
		 for(int i = 0; i < size; i += 3)
    {
            unsigned char tmp = d[i];
            d[i] = d[i+2];
            d[i+2] = tmp;
 
    }
	return d;
}
what error?
I see nothing wrong there
Last edited on
Topic archived. No new replies allowed.