summaryrefslogtreecommitdiff
path: root/main.c
blob: 7c0e5b5856f2443e0a6b5dc18e75f8b799a1bfed (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
/* Camicro - Microscope camera viewer.
 *
 * Copyright (C) 2019 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>
 */
#define _GNU_SOURCE
#include <stdio.h>
#include <png.h>
#include <SDL.h>

#include "options.h"
#include "device.h"
#include "utils.h"

void
run(struct device *device, struct options *options)
{
    if (options->raw) {
	FILE *out = fopen(options->out, "wb");
	if (!out)
	    utils_fatal("can not open output file `%s': %m", options->out);
	for (int i = 0; i < options->count;) {
	    size_t data_size;
	    const uint8_t *data = device_read_raw(device, &data_size);
	    if (data) {
		utils_info("write %d (%zu)", i, data_size);
		int r = fwrite(data, data_size, 1, out);
		if (r < 0)
		    utils_fatal("can not write: %m");
		i++;
	    }
	}
	fclose(out);
    } else {
	for (int i = 0; i < options->count;) {
	    const uint32_t *pixels = device_read(device);
	    if (pixels) {
		char *name = NULL;
		if (asprintf(&name, options->out, i) < 0)
		    utils_fatal("can not prepare file name");
		utils_info("write %s", name);
		png_image image;
		memset(&image, 0, sizeof(image));
		image.version = PNG_IMAGE_VERSION;
		image.width = options->width;
		image.height = options->height;
		image.format = PNG_FORMAT_BGRA;
		int r = png_image_write_to_file(&image, name, 0, pixels, 0,
			NULL);
		if (r == 0)
		    utils_fatal("can not write image: %s", image.message);
		free(name);
		i++;
	    }
	}
    }
}

void
run_video(struct device *device, struct options *options)
{
    if (SDL_Init(SDL_INIT_VIDEO))
	utils_fatal("unable to initialize SDL: %s", SDL_GetError());
    atexit(SDL_Quit);
    SDL_DisableScreenSaver();
    SDL_Window *window;
    SDL_Renderer *renderer;
    if (SDL_CreateWindowAndRenderer(options->width, options->height,
	    SDL_WINDOW_RESIZABLE, &window, &renderer))
	utils_fatal("unable to create window: %s", SDL_GetError());
    SDL_SetWindowTitle(window, "Moticam");
    SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
    if (SDL_RenderSetLogicalSize(renderer, options->width, options->height))
	utils_fatal("can not set logical size: %s", SDL_GetError());
    SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_BGRA32,
	    SDL_TEXTUREACCESS_STREAMING, options->width, options->height);
    if (!texture)
	utils_fatal("can not create texture: %s", SDL_GetError());
    bool exit = false;
    while (1) {
	SDL_Event event;
	while (SDL_PollEvent(&event)) {
	    if (event.type == SDL_QUIT)
		exit = true;
	    if (event.type == SDL_KEYDOWN
		    && (event.key.keysym.sym == SDLK_q
			|| event.key.keysym.sym == SDLK_ESCAPE))
		exit = true;
	}
	if (exit)
	    break;
	const uint32_t *pixels = device_read(device);
	if (pixels)
	{
	    SDL_UpdateTexture(texture, NULL, pixels, options->width * 4);
	    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
	    SDL_RenderClear(renderer);
	    SDL_RenderCopyEx(renderer, texture, NULL, NULL, 180.0, NULL,
		    SDL_FLIP_NONE);
	    SDL_RenderPresent(renderer);
	}
    }
    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
}

int
main(int argc, char **argv)
{
    struct options options;
    options_parse(argc, argv, &options);
    libusb_context *usb;
    int r = libusb_init(&usb);
    if (r)
	utils_fatal("unable to initialize libusb: %s", libusb_strerror(r));
    struct device *device = device_open(usb, &options);
    if (!device)
	utils_fatal("unable to find device");
    if (options.count)
	run(device, &options);
    else
	run_video(device, &options);
    device_close(device);
    libusb_exit(usb);
    return EXIT_SUCCESS;
}