Created
September 29, 2010 07:45
-
-
Save nagos/602423 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@mini2440:/dev/shm# time pngread small.png | |
begin | |
Open | |
read | |
png_ptr | |
Init | |
info | |
jmpbuf | |
sig bytes | |
info2 | |
read_image | |
Read done | |
real 0m 0.02s | |
user 0m 0.01s | |
sys 0m 0.01s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@mini2440:/dev/shm# ls -lh test.png | |
-rwxr-xr-x 1 root root 79.7K Apr 5 14:01 test.png | |
root@mini2440:/dev/shm# time ./pngread test.png | |
begin | |
Open | |
read | |
png_ptr | |
Init | |
info | |
jmpbuf | |
sig bytes | |
info2 | |
read_image | |
Read done | |
Pixel at position [ 1679 - 1049 ] has the following RGBA values: 212 - 208 - 200 - 255 | |
real 0m 1.16s | |
user 0m 1.06s | |
sys 0m 0.10s | |
root@mini2440:/dev/shm# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdarg.h> | |
#define PNG_DEBUG 3 | |
#include <png.h> | |
int x, y; | |
int width, height; | |
png_byte color_type; | |
png_byte bit_depth; | |
png_structp png_ptr; | |
png_infop info_ptr; | |
int number_of_passes; | |
png_bytep * row_pointers; | |
void abort_(const char * s, ...) | |
{ | |
va_list args; | |
va_start(args, s); | |
vfprintf(stderr, s, args); | |
fprintf(stderr, "\n"); | |
va_end(args); | |
abort(); | |
} | |
void read_png_file(char* file_name) | |
{ | |
char header[8]; // 8 is the maximum size that can be checked | |
printf("begin\n"); | |
/* open file and test for it being a png */ | |
FILE *fp = fopen(file_name, "rb"); | |
if (!fp) | |
abort_("[read_png_file] File %s could not be opened for reading", file_name); | |
else printf("Open\n"); | |
fread(header, 1, 8, fp); | |
if (png_sig_cmp(header, 0, 8)) | |
abort_("[read_png_file] File %s is not recognized as a PNG file", file_name); | |
else printf("read\n"); | |
/* initialize stuff */ | |
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); | |
printf("png_ptr\n"); | |
if (!png_ptr) | |
abort_("[read_png_file] png_create_read_struct failed"); | |
else printf("Init\n"); | |
info_ptr = png_create_info_struct(png_ptr); | |
if (!info_ptr) | |
abort_("[read_png_file] png_create_info_struct failed"); | |
else printf("info\n"); | |
if (setjmp(png_jmpbuf(png_ptr))) | |
abort_("[read_png_file] Error during init_io"); | |
else printf("jmpbuf\n"); | |
png_init_io(png_ptr, fp); | |
png_set_sig_bytes(png_ptr, 8); | |
printf("sig bytes\n"); | |
png_read_info(png_ptr, info_ptr); | |
printf("info2\n"); | |
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); | |
/* read file */ | |
if (setjmp(png_jmpbuf(png_ptr))) | |
abort_("[read_png_file] Error during read_image"); | |
else printf("read_image\n"); | |
row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height); | |
for (y=0; y<height; y++) | |
row_pointers[y] = (png_byte*) malloc(info_ptr->rowbytes); | |
png_read_image(png_ptr, row_pointers); | |
fclose(fp); | |
} | |
void process_file(void) | |
{ | |
if (info_ptr->color_type != PNG_COLOR_TYPE_RGBA) | |
abort_("[process_file] color_type of input file must be PNG_COLOR_TYPE_RGBA (is %d)", | |
info_ptr->color_type); | |
x=1679; | |
y=1049; | |
png_byte* row = row_pointers[y]; | |
png_byte* ptr = &(row[x*4]); | |
printf("Pixel at position [ %d - %d ] has the following RGBA values: %d - %d - %d - %d\n", | |
x, y, ptr[0], ptr[1], ptr[2], ptr[3]); | |
} | |
int main(int argc, char **argv) | |
{ | |
if (argc != 2) | |
abort_("Usage: program_name <file_in>"); | |
read_png_file(argv[1]); | |
printf("Read done\n"); | |
process_file(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment