/* Camicro - Microscope camera viewer. * * Copyright (C) 2019 Nicolas Schodet * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Contact : * Web: http://ni.fr.eu.org/ * Email: */ #define _GNU_SOURCE #include #include #include #include #include "options.h" #include "device.h" #include "live.h" #include "utils.h" void run(struct device *device, int count, const char *out) { 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++; } } } int main(int argc, char **argv) { struct options options; options_parse(argc, argv, &options); libusb_context *usb; int r = libusb_init(&usb); if (r) utils_fatal("unable to initialize libusb: %s", libusb_strerror(r)); 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.count, options.out); else live_run(device, &options); device_close(device); libusb_exit(usb); return EXIT_SUCCESS; }