Thanks for the pointers, I’ve managed to write a simple program to acquire a test image. Needs more work to refine, but positing here, just in case it helps others. Uses Aravis Save PNG
#include <png.h> // Requires libpng1.2
#include <assert.h>
#include <arv.h>
#include <stdlib.h>
#include <stdio.h>
void arv_save_png(ArvBuffer * buffer, const char * filename);
int main (int argc, char **argv)
{
ArvCamera *camera;
ArvBuffer *buffer;
camera = arv_camera_new (argc > 1 ? argv[1] : NULL);
buffer = arv_camera_acquisition (camera, 0);
if (ARV_IS_BUFFER (buffer)){
printf ("Image successfully acquired\n");
FILE * fp;
fp=fopen("/home/pi/test.png", "r");
arv_save_png(buffer, "test.png");
}else{
printf ("Failed to acquire a single image\n");
}
g_clear_object (&camera);
g_clear_object (&buffer);
return EXIT_SUCCESS;
}
void arv_save_png(ArvBuffer * buffer, const char * filename)
{
// TODO: This only works on image buffers
assert(arv_buffer_get_payload_type(buffer) == ARV_BUFFER_PAYLOAD_TYPE_IMAGE);
size_t buffer_size;
char * buffer_data = (char*)arv_buffer_get_data(buffer, &buffer_size); // raw data
int width;
int height;
arv_buffer_get_image_region(buffer, NULL, NULL, &width, &height); // get width/height
int bit_depth = ARV_PIXEL_FORMAT_BIT_PER_PIXEL(arv_buffer_get_image_pixel_format(buffer)); // bit(s) per pixel
//TODO: Deal with non-png compliant pixel formats?
// EG: ARV_PIXEL_FORMAT_MONO_14 is 14 bits per pixel, so conversion to PNG loses data
int arv_row_stride = width * bit_depth/8; // bytes per row, for constructing row pointers
int color_type = PNG_COLOR_TYPE_GRAY; //TODO: Check for other types?
// boilerplate libpng stuff without error checking (setjmp? Seriously? How many kittens have to die?)
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
png_infop info_ptr = png_create_info_struct(png_ptr);
FILE * f = fopen(filename, "wb");
png_init_io(png_ptr, f);
png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
// Need to create pointers to each row of pixels for libpng
png_bytepp rows = (png_bytepp)(png_malloc(png_ptr, height*sizeof(png_bytep)));
int i =0;
for (i = 0; i < height; ++i)
rows[i] = (png_bytep)(buffer_data + (height - i)*arv_row_stride);
// Actually write image
png_write_image(png_ptr, rows);
png_write_end(png_ptr, NULL); // cleanup
png_free(png_ptr, rows);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(f);
}
Compile with (this bit confused me) on RPi 4
sudo gcc -I/home/pi/aravis-0.6.4/src -I/usr/include/glib-2.0/ -I/usr/lib/arm-linux-gnueabihf/glib-2.0/include/ -L/home/pi/aravis-0.6.4/src/.libs/ -laravis-0.6 -lglib-2.0 -lgobject-2.0 -lpng save-png.c -o save-png3