Create png file. I couldn't correct the error.

this error appears when i try to compile the code:

E:\Users\HOME_2\Desktop\DESKTOP ITEMS\a\main.cpp||In function 'int main(int, char**)':|


E:\Users\HOME_2\Desktop\DESKTOP ITEMS\a\main.cpp|52|error: invalid conversion from 'void*' to 'Pixel*' [-fpermissive]|


, and I couldn't figure out the solution to this. Please give suggestions.

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
#include <stdio.h>
#include <stdlib.h>

#define PNG_SETJMP_NOT_SUPPORTED
#include <png.h>  

#define WIDTH 256
#define HEIGHT 32
#define COLOR_DEPTH 8

struct Pixel {
	png_byte r, g, b, a;
};

int main(int argc, char *argv[]) {
	srand(time(NULL));

	/* open PNG file for writing */
	FILE *f = fopen("out.png", "wb");
	if (!f) {
		fprintf(stderr, "could not open out.png\n");
		return 1;
	}

	/* initialize png data structures */
	png_structp png_ptr;
	png_infop info_ptr;

	png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
	if (!png_ptr) {
		fprintf(stderr, "could not initialize png struct\n");
		return 1;
	}

	info_ptr = png_create_info_struct(png_ptr);
	if (!info_ptr) {
		png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
		fclose(f);
		return 1;
	}

	/* begin writing PNG File */
	png_init_io(png_ptr, f);
	png_set_IHDR(png_ptr, info_ptr, WIDTH, HEIGHT, COLOR_DEPTH,
	             PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
	             PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
	png_write_info(png_ptr, info_ptr);

	/* allocate image data */
	struct Pixel *row_pointers[HEIGHT];
	for (int row = 0; row < HEIGHT; row++) {
		row_pointers[row] = calloc(WIDTH*2, sizeof(Pixel));
	}

	/* draw a bunch of vertical lines */
	for (int col = 0; col < WIDTH; col++) {
		int bar_height = rand() % HEIGHT;
		for (int row = 0; row < HEIGHT; row++) {
			if (HEIGHT - row <= bar_height) {
				row_pointers[row][col].r = 255; // red
				row_pointers[row][col].g = 0; // green
				row_pointers[row][col].b = 0; // blue
				row_pointers[row][col].a = 255; // alpha (opacity)
			} else {
				row_pointers[row][col].r = 0; // red
				row_pointers[row][col].g = 0; // green
				row_pointers[row][col].b = 0; // blue
				row_pointers[row][col].a = 0; // alpha (opacity)
			}
		}
	}

	/* write image data to disk */
	png_write_image(png_ptr, (png_byte **)row_pointers);

	/* finish writing PNG file */
	png_write_end(png_ptr, NULL);

	/* clean up PNG-related data structures */
	png_destroy_write_struct(&png_ptr, &info_ptr);

	/* close the file */
	fclose(f);
	f = NULL;

	return 0;
}


Thanks!
Last edited on
calloc returns a void* so you need to cast it to a Pixel* before you can assign it. This would not have been necessary in C but in C++ it is necessary (for good reasons).
 
row_pointers[row] = static_cast<Pixel*>(calloc(WIDTH*2, sizeof(Pixel)));
thanks, that error has been rectified but now these errors have come up:


obj\Debug\main.o||In function `main':|


E:\Users\HOME_2\Desktop\DESKTOP ITEMS\a\main.cpp|29|undefined reference to `png_create_write_struct'|


E:\Users\HOME_2\Desktop\DESKTOP ITEMS\a\main.cpp|35|undefined reference to `png_create_info_struct'|


E:\Users\HOME_2\Desktop\DESKTOP ITEMS\a\main.cpp|37|undefined reference to `png_destroy_write_struct'|


E:\Users\HOME_2\Desktop\DESKTOP ITEMS\a\main.cpp|43|undefined reference to `png_init_io'|


E:\Users\HOME_2\Desktop\DESKTOP ITEMS\a\main.cpp|46|undefined reference to `png_set_IHDR'|


E:\Users\HOME_2\Desktop\DESKTOP ITEMS\a\main.cpp|47|undefined reference to `png_write_info'|


E:\Users\HOME_2\Desktop\DESKTOP ITEMS\a\main.cpp|74|undefined reference to `png_write_image'|


E:\Users\HOME_2\Desktop\DESKTOP ITEMS\a\main.cpp|77|undefined reference to `png_write_end'|


E:\Users\HOME_2\Desktop\DESKTOP ITEMS\a\main.cpp|80|undefined reference to `png_destroy_write_struct'|


||=== Build finished: 9 errors, 0 warnings (0 minutes, 0 seconds) ===|

[/quote]
It's a linker error. You probably forgot to link the libpng library.
oh yes, i haven't . :P
[edit]
and funny thing is that, i don't remember how to. :P
so, Please help......
[/edit]

thanks!
Last edited on
Try: -lpng
doesn't work,
error :cannot find -l-lpng
Topic archived. No new replies allowed.