From 1bdf71f47e5f9f75308c5fef664814c2c125f305 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Sun, 10 Nov 2019 13:53:08 +0100 Subject: Rework device API, drop raw output --- main.c | 85 +++++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 45 insertions(+), 40 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index ad3d1f0..7fd84ef 100644 --- a/main.c +++ b/main.c @@ -32,45 +32,27 @@ #include "utils.h" void -run(struct device *device, struct options *options) +run(struct device *device, int count, const char *out) { - if (options->raw) { - FILE *out = fopen(options->out, "wb"); - if (!out) - utils_fatal("can not open output file `%s': %m", options->out); - for (int i = 0; i < options->count;) { - size_t data_size; - const uint8_t *data = device_read_raw(device, &data_size); - if (data) { - utils_info("write %d (%zu)", i, data_size); - int r = fwrite(data, data_size, 1, out); - if (r < 0) - utils_fatal("can not write: %m"); - i++; - } - } - fclose(out); - } else { - for (int i = 0; i < options->count;) { - const uint32_t *pixels = device_read(device); - if (pixels) { - char *name = NULL; - if (asprintf(&name, options->out, i) < 0) - utils_fatal("can not prepare file name"); - utils_info("write %s", name); - png_image image; - memset(&image, 0, sizeof(image)); - image.version = PNG_IMAGE_VERSION; - image.width = options->width; - image.height = options->height; - image.format = PNG_FORMAT_BGRA; - int r = png_image_write_to_file(&image, name, 0, pixels, 0, - NULL); - if (r == 0) - utils_fatal("can not write image: %s", image.message); - free(name); - i++; - } + for (int i = 0; i < count;) { + const struct device_image *device_image = device_read(device); + if (device_image) { + char *name = NULL; + if (asprintf(&name, out, i) < 0) + utils_fatal("can not prepare file name"); + utils_info("write %s", name); + png_image image; + memset(&image, 0, sizeof(image)); + image.version = PNG_IMAGE_VERSION; + image.width = device_image->width; + image.height = device_image->height; + image.format = PNG_FORMAT_BGRA; + int r = png_image_write_to_file(&image, name, 0, + device_image->pixels, device_image->stride, NULL); + if (r == 0) + utils_fatal("can not write image: %s", image.message); + free(name); + i++; } } } @@ -84,11 +66,34 @@ main(int argc, char **argv) int r = libusb_init(&usb); if (r) utils_fatal("unable to initialize libusb: %s", libusb_strerror(r)); - struct device *device = device_open(usb, &options); + struct device *device = device_open(usb); if (!device) utils_fatal("unable to find device"); + const struct device_info *info = device_get_info(device); + if (options.exposure_ms > 0.0) + device_set_exposure(device, options.exposure_ms); + else + options.exposure_ms = info->exposure_default_ms; + if (options.gain > 0.0) + device_set_gain(device, options.gain); + else + options.gain = info->gain_default; + int width = -1, height = -1; + for (int i = 0; width == -1 && i < info->resolutions; i++) { + const struct device_info_resolution *ir = &info->resolution[i]; + if ((options.width == -1 || options.width == ir->width) + && (options.height == -1 || options.height == ir->height)) { + width = ir->width; + height = ir->height; + } + } + if (width == -1) + utils_fatal("no matching resolution"); + device_set_resolution(device, width, height, 0); + options.width = width; + options.height = height; if (options.count) - run(device, &options); + run(device, options.count, options.out); else live_run(device, &options); device_close(device); -- cgit v1.2.3