Blame |
Last modification |
View Log
| RSS feed
/*
* Project: S.Ha.R.K.
*
* Coordinators:
* Giorgio Buttazzo <giorgio@sssup.it>
* Paolo Gai <pj@gandalf.sssup.it>
*
* Authors :
* Giacomo Guidi <giacomo@gandalf.sssup.it>
*
*
* ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
*
* http://www.sssup.it
* http://retis.sssup.it
* http://shark.sssup.it
*/
#include <stdio.h>
#include <stdlib.h>
#include <kernel/kern.h>
#include <png.h>
void read_png_file
(char *file_name
, char **buffer
, int *width
, int *height
, png_byte
*color_type
)
{
int y
;
png_byte bit_depth
;
png_structp png_ptr
;
png_infop info_ptr
;
int number_of_passes
;
png_bytep
* row_pointers
;
char header
[8]; // 8 is the maximum size that can be checked
/* open file and test for it being a png */
FILE
*fp
= fopen(file_name
, "rb");
if (!fp
) {
cprintf
("[read_png_file] File %s could not be opened for reading", file_name
);
exit(1);
}
fread(header
, 1, 8, fp
);
if (png_sig_cmp
(header
, 0, 8))
cprintf
("[read_png_file] File %s is not recognized as a PNG file", file_name
);
/* initialize stuff */
png_ptr
= png_create_read_struct
(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
if (!png_ptr
)
cprintf
("[read_png_file] png_create_read_struct failed");
info_ptr
= png_create_info_struct
(png_ptr
);
if (!info_ptr
)
cprintf
("[read_png_file] png_create_info_struct failed");
png_init_io
(png_ptr
, fp
);
png_set_sig_bytes
(png_ptr
, 8);
png_read_info
(png_ptr
, info_ptr
);
*width
= info_ptr
->width
;
*height
= info_ptr
->height
;
*color_type
= info_ptr
->color_type
;
bit_depth
= info_ptr
->bit_depth
;
number_of_passes
= png_set_interlace_handling
(png_ptr
);
png_read_update_info
(png_ptr
, info_ptr
);
row_pointers
= (png_bytep
*) malloc(sizeof(png_bytep
) * info_ptr
->height
);
for (y
=0; y
<info_ptr
->height
; y
++)
row_pointers
[y
] = (png_byte
*) malloc(info_ptr
->rowbytes
);
png_read_image
(png_ptr
, row_pointers
);
if(info_ptr
->color_type
== PNG_COLOR_TYPE_RGB
) {
*buffer
= malloc(info_ptr
->height
* info_ptr
->rowbytes
);
for(y
=0; y
<info_ptr
->height
; y
++)
memcpy(*buffer
+y
*info_ptr
->rowbytes
,row_pointers
[y
],info_ptr
->rowbytes
);
} else {
cprintf
("Image is not RGB\n");
}
fclose(fp
);
}
int main
(int argc
, char *argv
[])
{
char *Image
= NULL
;
int ImgWidth
= 0;
int ImgHeight
= 0;
png_byte ImgColor
= 0;
read_png_file
("test.png",&Image
,&ImgWidth
,&ImgHeight
,&ImgColor
);
cprintf
("Image ptr:%8d\n",(DWORD
)Image
);
cprintf
("Image Width:%d\n",ImgWidth
);
cprintf
("Image Height:%d\n",ImgHeight
);
cprintf
("Image Type:%d\n",ImgColor
);
free(Image
);
return 0;
}