summaryrefslogtreecommitdiff
path: root/src/dummy.c
blob: 834f212f0e40437a97a28dbf53269d5c73b16035 (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
/* 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;
}