summaryrefslogtreecommitdiffstats
path: root/bin/tools/grab.c
blob: a3e1839aa0a2d6b927113409f44e532c40dc405d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* Copyright (C) 2011, 2012 Matthias Vogelgesang <matthias.vogelgesang@kit.edu>
   (Karlsruhe Institute of Technology)

   This library is free software; you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as published by the
   Free Software Foundation; either version 2.1 of the License, or (at your
   option) any later version.

   This library 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 Lesser General Public License for more
   details.

   You should have received a copy of the GNU Lesser General Public License along
   with this library; if not, write to the Free Software Foundation, Inc., 51
   Franklin St, Fifth Floor, Boston, MA 02110, USA */

#include "config.h"

#include <glib-object.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "uca-plugin-manager.h"
#include "uca-camera.h"
#include "uca-ring-buffer.h"
#include "common.h"

#ifdef HAVE_LIBTIFF
#include <tiffio.h>
#endif


typedef struct {
    gint n_frames;
    gchar *filename;
#ifdef HAVE_LIBTIFF
    gboolean write_tiff;
#endif
} Options;


static guint
get_bytes_per_pixel (guint bits_per_pixel)
{
    return bits_per_pixel > 8 ? 2 : 1;
}

static guint
count_format_specifiers (const gchar *template)
{
    GRegex *regex;
    GMatchInfo *info;
    gint count = 0;

    regex = g_regex_new ("%[0-9]+[ui]", 0, 0, NULL);
    g_regex_match (regex, template, 0, &info);

    while (g_match_info_matches (info)) {
        count++;
        g_match_info_next (info, NULL);
    }

    g_match_info_free (info);
    g_regex_unref (regex);

    return count > 0 ? (guint) count : 0;
}

#ifdef HAVE_LIBTIFF
static void
write_tiff (UcaRingBuffer *buffer,
            Options *opts,
            guint width,
            guint height,
            guint bits_per_pixel)
{
    TIFF *tif;
    guint32 rows_per_strip;
    guint n_frames;
    guint bits_per_sample;
    gsize bytes_per_pixel;

    if (count_format_specifiers (opts->filename) > 0)
        g_warning ("Can only write multi-page TIFF, format specifier is ignored.\n");

    tif = TIFFOpen (opts->filename, "w");
    n_frames = uca_ring_buffer_get_num_blocks (buffer);
    rows_per_strip = TIFFDefaultStripSize (tif, (guint32) - 1);
    bytes_per_pixel = get_bytes_per_pixel (bits_per_pixel);
    bits_per_sample = bits_per_pixel > 8 ? 16 : 8;

    /* Write multi page TIFF file */
    TIFFSetField (tif, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);

    for (guint i = 0; i < n_frames; i++) {
        gpointer data;
        gsize offset = 0;

        data = uca_ring_buffer_get_read_pointer (buffer);

        TIFFSetField (tif, TIFFTAG_IMAGEWIDTH, width);
        TIFFSetField (tif, TIFFTAG_IMAGELENGTH, height);
        TIFFSetField (tif, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
        TIFFSetField (tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
        TIFFSetField (tif, TIFFTAG_SAMPLESPERPIXEL, 1);
        TIFFSetField (tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
        TIFFSetField (tif, TIFFTAG_ROWSPERSTRIP, rows_per_strip);
        TIFFSetField (tif, TIFFTAG_PAGENUMBER, i, n_frames);

        for (guint y = 0; y < height; y++, offset += width * bytes_per_pixel)
            TIFFWriteScanline (tif, data + offset, y, 0);

        TIFFWriteDirectory (tif);
    }

    TIFFClose (tif);
}
#endif

static void
write_raw (UcaRingBuffer *buffer,
           Options *opts)
{
    guint n_frames;
    gsize size;
    guint num_format_specifiers;
    gboolean multiple_files;
    FILE *fp;

    size = uca_ring_buffer_get_block_size (buffer);
    n_frames = uca_ring_buffer_get_num_blocks (buffer);
    num_format_specifiers = count_format_specifiers (opts->filename);

    if (num_format_specifiers > 1) {
        g_printerr ("Can only use zero or one format specifiers. Aborting write.\n");
        return;
    }

    multiple_files = num_format_specifiers == 1;

    if (!multiple_files)
        fp = fopen (opts->filename, "wb");

    for (gint i = 0; i < n_frames; i++) {
        gpointer data;

        if (multiple_files) {
            gchar *filename;
            filename = g_strdup_printf (opts->filename, i);
            fp = fopen (filename, "wb");
            g_free (filename);
        }

        data = uca_ring_buffer_get_read_pointer (buffer);
        fwrite (data, size, 1, fp);

        if (multiple_files)
            fclose (fp);
    }

    if (!multiple_files)
        fclose (fp);
}

static GError *
record_frames (UcaCamera *camera, Options *opts)
{
    guint roi_width;
    guint roi_height;
    guint bits;
    guint pixel_size;
    gsize size;
    gint n_frames;
    guint n_allocated;
    guint n_digits;
    gchar *fmt_string;
    GTimer *timer;
    gdouble elapsed;
    UcaRingBuffer *buffer;
    GError *error = NULL;

    g_object_get (G_OBJECT (camera),
                  "roi-width", &roi_width,
                  "roi-height", &roi_height,
                  "sensor-bitdepth", &bits,
                  NULL);

    pixel_size = get_bytes_per_pixel (bits);
    size = roi_width * roi_height * pixel_size;
    n_allocated = opts->n_frames > 0 ? opts->n_frames : 256;
    buffer = uca_ring_buffer_new (size, n_allocated);
    timer = g_timer_new();

    g_print ("Acquiring %i images at %ix%i with %i bits per pixel\n",
             opts->n_frames, roi_width, roi_height, bits);

    uca_camera_start_recording(camera, &error);

    if (error != NULL)
        return error;

    n_frames = 0;
    n_digits = floor (log10 (abs (opts->n_frames))) + 1;
    fmt_string = g_strdup_printf ("\33[2K\r%%%ii/%%i images acquired ...", n_digits);
    g_timer_start(timer);

    while (1) {
        uca_camera_grab (camera, uca_ring_buffer_get_write_pointer (buffer), &error);
        uca_ring_buffer_write_advance (buffer);

        if (error != NULL)
            return error;

        g_print (fmt_string, ++n_frames, opts->n_frames);

        if (n_frames == opts->n_frames)
            break;
    }

    elapsed = g_timer_elapsed (timer, NULL);
    g_print ("\nAcquired %3.2f images/s = %3.2f ms/image = %.4f MB/s\n",
             n_frames / elapsed, elapsed / n_frames * 1000.,
             n_frames * size / 1024. / 1024. / elapsed);

    uca_camera_stop_recording (camera, &error);

    if (opts->filename == NULL)
        g_print ("No filename given, not writing data.\n");
    else {
#ifdef HAVE_LIBTIFF
        if (g_str_has_suffix (opts->filename, ".tif") || g_str_has_suffix (opts->filename, ".tiff"))
            write_tiff (buffer, opts, roi_width, roi_height, bits);
        else
            write_raw (buffer, opts);
#else
        write_raw (buffer, opts);
#endif
    }

    g_object_unref (buffer);
    g_timer_destroy (timer);

    return error;
}

int
main (int argc, char *argv[])
{
    GOptionContext *context;
    UcaPluginManager *manager;
    UcaCamera *camera;
    GError *error = NULL;

    static Options opts = {
        .n_frames = -1,
        .filename = NULL,
    };

    static GOptionEntry entries[] = {
        { "num-frames", 'n', 0, G_OPTION_ARG_INT, &opts.n_frames, "Number of frames to acquire", "N" },
        { "output", 'o', 0, G_OPTION_ARG_STRING, &opts.filename, "Output file name template", "FILE" },
        { NULL }
    };

#if !(GLIB_CHECK_VERSION (2, 36, 0))
    g_type_init();
#endif

    manager = uca_plugin_manager_new ();
    context = uca_common_context_new (manager);
    g_option_context_add_main_entries (context, entries, NULL);

    if (!g_option_context_parse (context, &argc, &argv, &error)) {
        g_print ("Failed parsing arguments: %s\n", error->message);
        goto cleanup_manager;
    }

    if (argc < 2) {
        g_print ("%s\n", g_option_context_get_help (context, TRUE, NULL));
        goto cleanup_manager;
    }

    if (opts.n_frames < 0) {
        g_print ("You must specify the number of acquired frames with -n/--num-frames.\n");
        goto cleanup_manager;
    }

    camera = uca_common_get_camera (manager, argv[argc - 1], &error);

    if (camera == NULL) {
        g_print ("Error during initialization: %s\n", error->message);
        goto cleanup_manager;
    }

    error = record_frames (camera, &opts);

    if (error != NULL)
        g_print ("Error: %s\n", error->message);

    g_option_context_free (context);
    g_object_unref (camera);

cleanup_manager:
    g_object_unref (manager);

    return error != NULL ? 1 : 0;
}