-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdl_opencv.cpp
More file actions
173 lines (137 loc) · 3.53 KB
/
sdl_opencv.cpp
File metadata and controls
173 lines (137 loc) · 3.53 KB
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
#include "sdl_opencv.h"
#include <iostream>
Sdl_opencv::~Sdl_opencv()
{
if(surface) { SDL_FreeSurface(surface); }
if (renderer) { SDL_DestroyRenderer(renderer); }
if (window) { SDL_DestroyWindow(window); }
SDL_QuitSubSystem(SDL_INIT_VIDEO);
}
void Sdl_opencv::re_create_surface(cv::Mat& image)
{
/*
if (surface)
{
SDL_FreeSurface(surface);
}
surface = SDL_CreateRGBSurfaceFrom(
(void*)image.data,
image.size().width,
image.size().height,
8 * image.channels(),
image.step,
0xff0000,
0x00ff00,
0x0000ff, 0);
//*/
//*
if (texture) { SDL_DestroyTexture(texture); }
auto texture_format = SDL_PIXELFORMAT_RGB24;
if (image.channels() == 3) { texture_format = SDL_PIXELFORMAT_BGR24; }
if (image.channels() == 4) { texture_format = SDL_PIXELFORMAT_BGRA8888; }
texture = SDL_CreateTexture(renderer, texture_format, SDL_TEXTUREACCESS_STREAMING, w, h);
//*/
}
void Sdl_opencv::imshow(cv::Mat& image, int x, int y)
{
if (!initialized)
{
if (!SDL_WasInit(SDL_INIT_VIDEO))
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cerr << "\nCouldn't initialize SDL:" << SDL_GetError();
return;
}
atexit(SDL_Quit);
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
if (!window)
{
w = image.cols;
h = image.rows;
// the flags to pass to SDL_SetVideoMode
int videoFlags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;// | SDL_WINDOW_FULLSCREEN;
window = SDL_CreateWindow(title_.c_str(), x, y, w, h, videoFlags);
}
if (!renderer)
{
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
}
re_create_surface(image);
initialized = true;
}
}
// resize the window and (re)create surface
if (w != image.cols || h != image.rows)
{
w = image.cols;
h = image.rows;
SDL_SetWindowSize(window, w, h);
re_create_surface(image);
}
//*
int pitch = 0;
void* pixels = nullptr;
SDL_LockTexture(texture, NULL, &pixels, &pitch);
if (pitch != image.step) { std::cerr << "texture.pitch != image.step" << std::endl; }
memcpy(pixels, image.data, image.step * h);
SDL_UnlockTexture(texture);
//*/
//texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer); // should be vsynced
//SDL_DestroyTexture(texture);
}
// make sure events are processed
SDL_Keysym Sdl_opencv::waitKey()
{
SDL_Event e; SDL_PollEvent(&e);
return e.key.keysym;
}
#ifdef __TEST_THIS_MODULE__
#include <thread>
using namespace std;
#include "timer_hd.h"
#include<SDL2/SDL_main.h>
#pragma comment(lib, "SDL2/lib/x64/SDL2.lib")
#pragma comment(lib, "SDL2/lib/x64/SDL2main.lib")
//#pragma comment(lib, "SDL2.lib")
#ifdef _DEBUG
#pragma comment(lib, "opencv40/build/x64/vc15/lib/opencv_world401d.lib")
#else
#pragma comment(lib, "opencv40/build/x64/vc15/lib/opencv_world401.lib")
#endif
void test_module_sdl_opencv()
{
cv::Mat img;
Sdl_opencv s("my image");
//auto fname = "/home/frosch/frosch_klein.jpg";
//auto fname = "c:/temp/build_opencv401.png";
//auto fname = "c:/temp/walk_fly_independence.jpg";
auto fname = "c:/temp/build_opencv401.png";
img = cv::imread(fname);
s.imshow(img, 50, 50);
Timer timer0(1);
Timer timer1(500);
int nframes = 0;
int k = 0;
while (true)
{
timer1.tick();
timer0.tick();
SDL_Delay(1);
s.imshow(img, 50, 50);
//if(s.waitKey().sym == SDLK_ESC){break;}
s.waitKey();
auto dt = timer0.tock(false);
timer1.tock();
nframes++;
k++;
if (dt > 0.018)
{
cout << "\nframe drop after " << nframes << " dt=" << dt;
nframes = 0;
}
}
}
#endif