Rev 1144 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1141 | giacomo | 1 | /* |
2 | * Project: S.Ha.R.K. |
||
3 | * |
||
4 | * Coordinators: |
||
5 | * Giorgio Buttazzo <giorgio@sssup.it> |
||
6 | * Paolo Gai <pj@gandalf.sssup.it> |
||
7 | * |
||
8 | * Authors : |
||
9 | * Giacomo Guidi <giacomo@gandalf.sssup.it> |
||
10 | * |
||
11 | * |
||
12 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
13 | * |
||
14 | * http://www.sssup.it |
||
15 | * http://retis.sssup.it |
||
16 | * http://shark.sssup.it |
||
17 | */ |
||
18 | |||
19 | #include <stdio.h> |
||
20 | #include <stdlib.h> |
||
21 | #include <kernel/kern.h> |
||
1144 | pj | 22 | #include <png.h> |
1141 | giacomo | 23 | |
24 | void read_png_file(char *file_name, char **buffer, int *width, int *height, png_byte *color_type) |
||
25 | { |
||
26 | |||
27 | int y; |
||
28 | png_byte bit_depth; |
||
29 | |||
30 | png_structp png_ptr; |
||
31 | png_infop info_ptr; |
||
32 | int number_of_passes; |
||
33 | png_bytep * row_pointers; |
||
34 | |||
35 | char header[8]; // 8 is the maximum size that can be checked |
||
36 | |||
37 | /* open file and test for it being a png */ |
||
38 | FILE *fp = fopen(file_name, "rb"); |
||
39 | if (!fp) { |
||
40 | cprintf("[read_png_file] File %s could not be opened for reading", file_name); |
||
1550 | pj | 41 | exit(1); |
1141 | giacomo | 42 | } |
43 | fread(header, 1, 8, fp); |
||
44 | |||
45 | if (png_sig_cmp(header, 0, 8)) |
||
46 | cprintf("[read_png_file] File %s is not recognized as a PNG file", file_name); |
||
47 | |||
48 | /* initialize stuff */ |
||
49 | |||
50 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
||
51 | |||
52 | if (!png_ptr) |
||
53 | cprintf("[read_png_file] png_create_read_struct failed"); |
||
54 | |||
55 | info_ptr = png_create_info_struct(png_ptr); |
||
56 | if (!info_ptr) |
||
57 | cprintf("[read_png_file] png_create_info_struct failed"); |
||
58 | |||
59 | png_init_io(png_ptr, fp); |
||
60 | png_set_sig_bytes(png_ptr, 8); |
||
61 | |||
62 | png_read_info(png_ptr, info_ptr); |
||
63 | |||
64 | *width = info_ptr->width; |
||
65 | *height = info_ptr->height; |
||
66 | *color_type = info_ptr->color_type; |
||
67 | bit_depth = info_ptr->bit_depth; |
||
68 | |||
69 | number_of_passes = png_set_interlace_handling(png_ptr); |
||
70 | |||
71 | png_read_update_info(png_ptr, info_ptr); |
||
72 | |||
73 | row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * info_ptr->height); |
||
74 | for (y=0; y<info_ptr->height; y++) |
||
75 | row_pointers[y] = (png_byte*) malloc(info_ptr->rowbytes); |
||
76 | |||
77 | png_read_image(png_ptr, row_pointers); |
||
78 | |||
79 | if(info_ptr->color_type == PNG_COLOR_TYPE_RGB) { |
||
80 | *buffer = malloc(info_ptr->height * info_ptr->rowbytes); |
||
81 | for(y=0; y<info_ptr->height; y++) |
||
82 | memcpy(*buffer+y*info_ptr->rowbytes,row_pointers[y],info_ptr->rowbytes); |
||
83 | } else { |
||
84 | cprintf("Image is not RGB\n"); |
||
85 | } |
||
86 | |||
87 | fclose(fp); |
||
88 | |||
89 | } |
||
90 | |||
91 | int main (int argc, char *argv[]) |
||
92 | { |
||
93 | |||
94 | char *Image = NULL; |
||
95 | int ImgWidth = 0; |
||
96 | int ImgHeight = 0; |
||
97 | png_byte ImgColor = 0; |
||
98 | |||
99 | read_png_file("test.png",&Image,&ImgWidth,&ImgHeight,&ImgColor); |
||
100 | |||
101 | cprintf("Image ptr:%8d\n",(DWORD)Image); |
||
102 | cprintf("Image Width:%d\n",ImgWidth); |
||
103 | cprintf("Image Height:%d\n",ImgHeight); |
||
104 | cprintf("Image Type:%d\n",ImgColor); |
||
105 | |||
106 | free(Image); |
||
107 | |||
108 | return 0; |
||
109 | |||
110 | } |