summaryrefslogtreecommitdiff
path: root/src/dummy.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dummy.c')
-rw-r--r--src/dummy.c184
1 files changed, 184 insertions, 0 deletions
diff --git a/src/dummy.c b/src/dummy.c
new file mode 100644
index 0000000..834f212
--- /dev/null
+++ b/src/dummy.c
@@ -0,0 +1,184 @@
+/* Camicro - Microscope camera viewer.
+ *
+ * This is a dummy camera for testing.
+ *
+ * Copyright (C) 2020 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: <nico at ni.fr.eu.org>
+ */
+#include "dummy.h"
+
+struct dummy_device {
+ struct device device;
+ int width;
+ int height;
+ int stride;
+ double exposure_ms;
+ double gain;
+ struct image *image;
+};
+
+static char *
+dummy_poll(void);
+
+static struct device *
+dummy_open(GError **error);
+
+const struct device_driver dummy_device_driver = {
+ "Dummy camera",
+ .poll = &dummy_poll,
+ .open = &dummy_open,
+};
+
+static const struct device_info_resolution dummy_device_info_resolution[] = {
+ { 1024, 768 },
+};
+
+static const struct device_info dummy_device_info = {
+ .exposure_min_ms = 1,
+ .exposure_max_ms = 5000,
+ .exposure_default_ms = 100,
+ .exposure_step_ms = 1,
+ .gain_min = 0.5,
+ .gain_max = 5.0,
+ .gain_default = 1,
+ .gain_step = 0.5,
+ .resolutions = 1,
+ .resolution = dummy_device_info_resolution,
+};
+
+static const struct device_info *
+dummy_get_info(struct device *device)
+{
+ return &dummy_device_info;
+}
+
+static void
+dummy_set_exposure(struct device *device, double exposure_ms)
+{
+ struct dummy_device *mdev = (struct dummy_device *) device;
+ mdev->exposure_ms = exposure_ms;
+}
+
+static void
+dummy_set_gain(struct device *device, double gain)
+{
+ struct dummy_device *mdev = (struct dummy_device *) device;
+ mdev->gain = gain;
+}
+
+static void
+dummy_set_resolution(struct device *device, int width, int height,
+ int stride)
+{
+ struct dummy_device *mdev = (struct dummy_device *) device;
+ g_assert(!mdev->image);
+ g_assert(stride == 0 || stride >= width * 4);
+ /* Store parameters. */
+ mdev->width = width;
+ mdev->height = height;
+ mdev->stride = stride ? stride : width * 4;
+}
+
+static bool
+dummy_start(struct device *device, GError **error)
+{
+ g_return_val_if_fail(error == NULL || *error == NULL, false);
+ struct dummy_device *mdev = (struct dummy_device *) device;
+ g_assert(!mdev->image);
+ /* Nothing to do. */
+ return true;
+}
+
+static struct image *
+dummy_read(struct device *device, GError **error)
+{
+ g_return_val_if_fail(error == NULL || *error == NULL, NULL);
+ struct dummy_device *mdev = (struct dummy_device *) device;
+ /* Prepare image. */
+ if (!mdev->image) {
+ mdev->image = image_new(mdev->width, mdev->height, mdev->stride,
+ IMAGE_FORMAT_XBGR32);
+ uint8_t *p = mdev->image->pixels;
+ for (int y = 0; y < mdev->height; y++) {
+ for (int x = 0; x < mdev->width; x++) {
+ *p++ = (x ^ y) & 0xffffff;
+ *p++ = 0;
+ *p++ = 0;
+ *p++ = 0xff;
+ }
+ }
+ }
+ image_ref(mdev->image);
+ return mdev->image;
+}
+
+static bool
+dummy_stop(struct device *device, GError **error)
+{
+ g_return_val_if_fail(error == NULL || *error == NULL, false);
+ struct dummy_device *mdev = (struct dummy_device *) device;
+ if (mdev->image) {
+ image_unref(mdev->image);
+ mdev->image = NULL;
+ }
+ return true;
+}
+
+static bool
+dummy_close(struct device *device, GError **error)
+{
+ g_return_val_if_fail(error == NULL || *error == NULL, false);
+ struct dummy_device *mdev = (struct dummy_device *) device;
+ bool ret = true;
+ if (mdev->image)
+ ret = dummy_stop(device, error);
+ free(device);
+ return ret;
+}
+
+static char *
+dummy_poll(void)
+{
+ return g_strdup("Dummy");
+}
+
+static struct device *
+dummy_open(GError **error)
+{
+ g_return_val_if_fail(error == NULL || *error == NULL, NULL);
+ /* Create context. */
+ struct dummy_device *mdev = g_new(struct dummy_device, 1);
+ mdev->device.get_info = &dummy_get_info;
+ mdev->device.set_exposure = &dummy_set_exposure;
+ mdev->device.set_gain = &dummy_set_gain;
+ mdev->device.set_resolution = &dummy_set_resolution;
+ mdev->device.start = &dummy_start;
+ mdev->device.read = &dummy_read;
+ mdev->device.stop = &dummy_stop;
+ mdev->device.close = &dummy_close;
+ mdev->width = 0;
+ mdev->height = 0;
+ mdev->stride = 0;
+ mdev->exposure_ms = dummy_device_info.exposure_default_ms;
+ mdev->gain = dummy_device_info.gain_default;
+ mdev->image = NULL;
+ /* Done. */
+ return &mdev->device;
+}