From 76389b999910e3aa3900d5a611588f4a5983e224 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Sat, 16 Nov 2019 18:01:12 +0100 Subject: Add back raw output --- cli.c | 18 ++++-------------- image.c | 25 +++++++++++++++++++++++++ image.h | 4 ++++ moticam.c | 9 ++++++++- options.c | 11 +++++++++-- options.h | 2 ++ 6 files changed, 52 insertions(+), 17 deletions(-) diff --git a/cli.c b/cli.c index b0d3a1b..79225c1 100644 --- a/cli.c +++ b/cli.c @@ -22,7 +22,6 @@ */ #define _GNU_SOURCE #include -#include #include "device.h" #include "utils.h" @@ -30,13 +29,13 @@ void cli_read_images(libusb_context *usb, struct device *device, int count, - const char *out) + bool raw, const char *out) { device_start(device); for (int i = 0; i < count;) { struct image *image = device_read(device); if (image) { - if (image->format != IMAGE_FORMAT_XBGR32) { + if (!raw && image->format != IMAGE_FORMAT_XBGR32) { struct image *cimage = image_new(image->width, image->height, image->width * 4, IMAGE_FORMAT_XBGR32); image_convert(cimage, image); @@ -47,16 +46,7 @@ cli_read_images(libusb_context *usb, struct device *device, int count, if (asprintf(&name, out, i) < 0) utils_fatal("can not prepare file name"); utils_info("write %s", name); - png_image pimage; - memset(&pimage, 0, sizeof(pimage)); - pimage.version = PNG_IMAGE_VERSION; - pimage.width = image->width; - pimage.height = image->height; - pimage.format = PNG_FORMAT_BGRA; - int r = png_image_write_to_file(&pimage, name, 0, - image->pixels, image->stride, NULL); - if (r == 0) - utils_fatal("can not write image: %s", pimage.message); + image_save(image, name); free(name); image_unref(image); i++; @@ -94,7 +84,7 @@ cli_run(struct options *options) if (width == -1) utils_fatal("no matching resolution"); device_set_resolution(device, width, height, 0); - cli_read_images(usb, device, options->count, options->out); + cli_read_images(usb, device, options->count, options->raw, options->out); device_close(device); libusb_exit(usb); return EXIT_SUCCESS; diff --git a/image.c b/image.c index 73ac957..9ab12c9 100644 --- a/image.c +++ b/image.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "image.h" #include "utils.h" @@ -179,3 +180,27 @@ image_convert(struct image *dst, const struct image *src) } } } + +void +image_save(const struct image *image, const char *name) +{ + png_image pimage; + memset(&pimage, 0, sizeof(pimage)); + pimage.version = PNG_IMAGE_VERSION; + pimage.width = image->width; + pimage.height = image->height; + switch(image->format) { + case IMAGE_FORMAT_SGRBG8: + pimage.format = PNG_FORMAT_GRAY; + break; + case IMAGE_FORMAT_XBGR32: + pimage.format = PNG_FORMAT_BGRA; + break; + default: + assert(0); + } + int r = png_image_write_to_file(&pimage, name, 0, + image->pixels, image->stride, NULL); + if (r == 0) + utils_fatal("can not write image: %s", pimage.message); +} diff --git a/image.h b/image.h index 9e20859..ae33ca7 100644 --- a/image.h +++ b/image.h @@ -79,4 +79,8 @@ image_unref(struct image *image); void image_convert(struct image *dst, const struct image *src); +/* Save image to PNG. */ +void +image_save(const struct image *image, const char *name); + #endif /* image_h */ diff --git a/moticam.c b/moticam.c index 4f46478..4e83225 100644 --- a/moticam.c +++ b/moticam.c @@ -51,6 +51,7 @@ struct moticam_device { struct image *other_image; struct libusb_transfer *usb_transfer; bool usb_transfer_done; + int drop; }; static char * @@ -263,6 +264,8 @@ moticam_start(struct device *device) } mdev->current_image = &mdev->image[0]; mdev->other_image = &mdev->image[1]; + /* Drop first frame. */ + mdev->drop = 1; /* Prepare and submit transfer. */ mdev->usb_transfer = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(mdev->usb_transfer, mdev->handle, 0x83, @@ -283,12 +286,15 @@ moticam_read(struct device *device) const int image_size = mdev->width * mdev->height; struct image *image = NULL; /* Image received? Return it, else drop. */ - if (mdev->usb_transfer->actual_length == image_size) { + if (!mdev->drop + && mdev->usb_transfer->actual_length == image_size) { image = mdev->current_image; image->refs = 1; mdev->current_image = mdev->other_image; mdev->other_image = image; } + if (mdev->drop) + mdev->drop--; /* Pending updates? */ if (mdev->exposure_pending) { moticam_control_exposure(device, mdev->exposure_ms); @@ -394,6 +400,7 @@ moticam_usb_open(libusb_context *usb, libusb_device *usb_device) mdev->other_image = NULL; mdev->usb_transfer = NULL; mdev->usb_transfer_done = false; + mdev->drop = 1; /* Open USB device. */ int r = libusb_open(usb_device, &mdev->handle); if (r) diff --git a/options.c b/options.c index bd9f82b..c8baaa2 100644 --- a/options.c +++ b/options.c @@ -40,11 +40,13 @@ options_add(GApplication *app) G_OPTION_ARG_DOUBLE, "exposure value", "MS"); g_application_add_main_option(app, "gain", 'g', 0, G_OPTION_ARG_DOUBLE, "gain value", "VALUE"); + g_application_add_main_option(app, "count", 'n', 0, + G_OPTION_ARG_INT, "number of image to take", "N"); + g_application_add_main_option(app, "raw", 'r', 0, + G_OPTION_ARG_NONE, "do not convert image", NULL); g_application_add_main_option(app, "output", 'o', 0, G_OPTION_ARG_FILENAME, "output file pattern (default: out%02d.png)", "PATTERN"); - g_application_add_main_option(app, "count", 'n', 0, - G_OPTION_ARG_INT, "number of image to take", "N"); } gint @@ -56,6 +58,7 @@ options_handle(GApplication *app, GVariantDict *options_dict) options.exposure_ms = -1.0; options.gain = -1.0; options.count = 1; + options.raw = false; options.out = NULL; bool option_set = false; if (g_variant_dict_lookup(options_dict, "width", "i", &options.width)) { @@ -79,6 +82,10 @@ options_handle(GApplication *app, GVariantDict *options_dict) if (options.count <= 0) utils_fatal("invalid count"); } + if (g_variant_dict_contains(options_dict, "raw")) { + option_set = true; + options.raw = true; + } if (g_variant_dict_lookup(options_dict, "out", "^&ay", &options.out)) { option_set = true; int argtypes[1]; diff --git a/options.h b/options.h index 86741cf..7a08619 100644 --- a/options.h +++ b/options.h @@ -37,6 +37,8 @@ struct options { double gain; /* Number of frame to dump. */ int count; + /* Do not convert image. */ + bool raw; /* Output file name or pattern. This should include a printf like pattern * (%d) used to name the files. */ const char *out; -- cgit v1.2.3