alvieboy

Integrating Linux libraries

Recommended Posts

Hi guys,

 

I spent a few days implementing some sort of POSIX interface for ZPUino (and a smaller stdio), so we can use some of Linux libraries out of the box. Since we have plenty of RAM on the PPro, this can be feasible to do.

 

So I started with zlib and libpng. PNG requires ZLIB for decompression, so the two had to go side by side.

 

After a few hiccups with libpng requiring odd stuff like "jmpbuf", I finally was able to have them working. Zlib actually worked right out of the box, I would not expect otherwise.

 

So, a few code snippets from the sketch:

int readpng(const char*name){	unsigned char header[8];	int y;	FILE *f = fopen(name, "rb");	if (!f) {		perror("fopen");		return -1;	}	fread(header, 1, 8, f);	if (png_sig_cmp(header, 0, 8)) {		fprintf(stderr,"Invalid PNG file");		fclose(f);		return -1;	}	png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);	if (!png_ptr) {        fprintf(stderr,"Cannot read struct\n");		return -1;	}	info_ptr = png_create_info_struct(png_ptr);	if (!info_ptr) {        fprintf(stderr,"Cannot create struct\n");		return -1;	}	png_init_io(png_ptr, f);	png_set_sig_bytes(png_ptr, 8);	png_read_info(png_ptr, info_ptr);	width = png_get_image_width(png_ptr, info_ptr);	height = png_get_image_height(png_ptr, info_ptr);	color_type = png_get_color_type(png_ptr, info_ptr);	bit_depth = png_get_bit_depth(png_ptr, info_ptr);	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) * height);	for (y=0; y<height; y++)		row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));        png_read_image(png_ptr, row_pointers);        printf("All read\r\n");        printf("Image information:\r\n\r\n");        printf("Width:     %d\r\n", width);        printf("Height:    %d\r\n", height);        printf("ColorType: %d\r\n", color_type);        printf("Bit Depth: %d\r\n", bit_depth);        fclose(f);}void loop(){    SmallFS.begin();    readpng("smallfs:test.png");}

Notice how the multi-device is handled in the system ("smallfs:"). I devised something simple to handle the multiplicity of devices we have, while maintaining the libraries without much modifications.

 

So we can have:

FILE *c = fopen("dev:serial2", "rw"); // Open serial device 2FILE *sf = fopen("smallfs:myfile.txt","r"); // Open a file in SmallFSFILE *s = fopen("sdcard:/documents/doc.txt","rw"); // Open a file in sdcard

Note: you can use normal "POSIX" open/close, besides the stdio ones (fopen/fclose). The latter will use POSIX (the former) interface to open the files.

 

Oh, and we have printf/fprintf now. :) Only the following are supported as of now: %"duxXs", with proper fill and padding (so you can use "0x%08x" to print proper 32-bit hexs).

 

Outputs from real board:

Image information:Width:     784Height:    504ColorType: 2Bit Depth: 8

File information, as seen on Linux:

$ file smallfs/test.png smallfs/test.png: PNG image data, 784 x 504, 8-bit/color RGB, non-interlaced

Best for all,

Alvie

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now