summaryrefslogtreecommitdiff
path: root/snapshot.c
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2012-03-19 12:58:24 +0100
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2012-05-01 12:13:56 +0200
commit97036161f78b336d9778460335d8fedb42259e5f (patch)
tree85f295d940ddb8d1a205c4fd3fc84ecb62fbddc9 /snapshot.c
parentcecf2f690a16ad8a99d739f42b3bdb6745fafc07 (diff)
snapshot: Add JPEG compression support
Specifying the -j flag results in captured images being saved in JPEG format instead of raw YUYV. JPEG compression implies YUYV capture, software demosaicing isn't supported. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Diffstat (limited to 'snapshot.c')
-rw-r--r--snapshot.c69
1 files changed, 60 insertions, 9 deletions
diff --git a/snapshot.c b/snapshot.c
index 958307e..5d29653 100644
--- a/snapshot.c
+++ b/snapshot.c
@@ -42,6 +42,11 @@
#include <linux/spi/spidev.h>
+#if USE_LIBJPEG
+#include <jpeglib.h>
+#include <setjmp.h>
+#endif
+
#include "isp/list.h"
#include "isp/omap3isp.h"
#include "isp/tools.h"
@@ -50,6 +55,7 @@
#include "events.h"
#include "iq.h"
+#include "jpeg.h"
#define MEDIA_DEVICE "/dev/media0"
@@ -86,6 +92,9 @@ struct snapshot {
struct timespec time;
enum snapshot_trigger_type trigger;
bool save;
+
+ bool jpeg_enable;
+ struct jpeg *jpeg;
};
static struct snapshot snap = {
@@ -151,8 +160,8 @@ static bool snapshot_process(struct omap3_isp_device *isp,
{
static struct timespec ts;
char name[33];
+ FILE *file;
int ret;
- int fd;
if (buffer->error) {
printf("warning: error in dequeued buffer, skipping\n");
@@ -172,14 +181,21 @@ static bool snapshot_process(struct omap3_isp_device *isp,
printf("frame captured in %lu.%06lus.\n", ts.tv_sec, ts.tv_nsec / 1000);
if (snap.save && snap.frame >= snap.skip) {
- sprintf(name, "snapshot-%06u-%02u.bin", snap.cycle, snap.frame);
- fd = open(name, O_WRONLY | O_CREAT,
- S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
- if (fd == -1)
+ sprintf(name, "snapshot-%06u-%02u.%s", snap.cycle, snap.frame,
+#if USE_LIBJPEG
+ snap.jpeg ? "jpg" :
+#endif
+ "bin");
+ file = fopen(name, "wb");
+ if (file == NULL)
goto requeue;
-
- write(fd, buffer->mem, buffer->bytesused);
- close(fd);
+#if USE_LIBJPEG
+ if (snap.jpeg)
+ jpeg_save(snap.jpeg, file, buffer);
+ else
+#endif
+ fwrite(buffer->mem, buffer->bytesused, 1, file);
+ fclose(file);
}
requeue:
@@ -220,12 +236,30 @@ static int snapshot_init(struct omap3_isp_device *isp)
return ret;
}
+#if USE_LIBJPEG
+ if (snap.jpeg_enable) {
+ snap.jpeg = jpeg_init(&snap.format);
+ if (snap.jpeg == NULL) {
+ printf("Unable to initialize JPEG compressor.\n");
+ return -EIO;
+ }
+ }
+#endif
+
printf("snapshot configured for %04x %ux%u\n",
snap.format.code, snap.format.width, snap.format.height);
return 0;
}
+static void snapshot_cleanup(void)
+{
+#if USE_LIBJPEG
+ if (snap.jpeg != NULL)
+ jpeg_cleanup(snap.jpeg);
+#endif
+}
+
/* -----------------------------------------------------------------------------
* Viewfinder
*/
@@ -394,6 +428,9 @@ static void usage(const char *argv0)
printf("-f, --format fmt Snapshot format\n");
printf("-h, --help Show this help screen\n");
printf("-i, --interval n Capture a snapshot every n frames (default 20)\n");
+#if USE_LIBJPEG
+ printf("-j, --jpeg Save snapshots in JPEG format (implies YUV mode)\n");
+#endif
printf("-k, --skip n Skip n frames per snapshot when saving to disk (default 2)\n");
printf("-N, --snap-frames n Capture n frames per snapshot (default 3)\n");
printf("-n, --snap-number n Capture n snapshots (default 1)\n");
@@ -425,6 +462,9 @@ static struct option opts[] = {
{ "format", 1, 0, 'f' },
{ "help", 0, 0, 'h' },
{ "interval", 1, 0, 'i' },
+#if USE_LIBJPEG
+ { "jpeg", 0, 0, 'j' },
+#endif
{ "save", 0, 0, 'S' },
{ "size", 1, 0, 's' },
{ "snap-frames", 1, 0, 'N' },
@@ -451,7 +491,11 @@ int main(int argc __attribute__((__unused__)), char *argv[] __attribute__((__unu
iq_params_init(&iq_params);
- while ((c = getopt_long(argc, argv, "c:f:hi:k:N:n:Ss:v", opts, NULL)) != -1) {
+ while ((c = getopt_long(argc, argv, "c:f:hi:"
+#if USE_LIBJPEG
+ "j"
+#endif
+ "k:N:n:Ss:v", opts, NULL)) != -1) {
switch (c) {
case 'c':
if (parse_crop(optarg, &snap.crop)) {
@@ -476,6 +520,12 @@ int main(int argc __attribute__((__unused__)), char *argv[] __attribute__((__unu
return 1;
}
break;
+#if USE_LIBJPEG
+ case 'j':
+ snap.jpeg_enable = true;
+ snap.format.code = V4L2_MBUS_FMT_YUYV8_1X16;
+ break;
+#endif
case 'k':
snap.skip = strtoul(optarg, NULL, 10);
break;
@@ -612,6 +662,7 @@ cleanup:
viewfinder_cleanup(isp);
if (iq)
iq_cleanup(iq);
+ snapshot_cleanup();
return exit_code;
}