-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbmpfromocvpanel.cpp
127 lines (97 loc) · 3.63 KB
/
bmpfromocvpanel.cpp
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
///////////////////////////////////////////////////////////////////////////////
// Name: bmpfromocvpanel.cpp
// Purpose: Displays a wxBitmap originated from OpenCV
// Author: PB
// Created: 2020-09-16
// Copyright: (c) 2020 PB
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#include <wx/wx.h>
#include <wx/colordlg.h>
#include <wx/dcbuffer.h>
#include <wx/fontdlg.h>
#include "bmpfromocvpanel.h"
wxBitmapFromOpenCVPanel::wxBitmapFromOpenCVPanel(wxWindow* parent)
: wxScrolledCanvas(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
{
m_overlayTextColour = *wxGREEN;
m_overlayFont = GetFont();
SetBackgroundColour(*wxBLACK);
SetBackgroundStyle(wxBG_STYLE_PAINT);
SetScrollRate(FromDIP(8), FromDIP(8));
// We need to do this to prevent drawing artefacts
// due to the info "overlay" which does not scroll with the bitmap.
EnableScrolling(false, false);
Bind(wxEVT_PAINT, &wxBitmapFromOpenCVPanel::OnPaint, this);
Bind(wxEVT_LEFT_DCLICK, &wxBitmapFromOpenCVPanel::OnChangeOverlayTextColour, this);
Bind(wxEVT_RIGHT_DCLICK, &wxBitmapFromOpenCVPanel::OnChangeOverlayFont, this);
}
bool wxBitmapFromOpenCVPanel::SetBitmap(const wxBitmap& bitmap, const long timeGet, const long timeConvert)
{
m_bitmap = bitmap;
if ( m_bitmap.IsOk() )
{
if ( m_bitmap.GetSize() != GetVirtualSize() )
{
InvalidateBestSize();
SetVirtualSize(m_bitmap.GetSize());
}
}
else
{
InvalidateBestSize();
SetVirtualSize(1, 1);
}
m_timeGetCVBitmap = timeGet;
m_timeConvertBitmap = timeConvert;
Refresh(); Update();
return true;
}
wxSize wxBitmapFromOpenCVPanel::DoGetBestClientSize() const
{
if ( !m_bitmap.IsOk() )
return FromDIP(wxSize(64, 48)); // completely arbitrary
return m_bitmap.GetSize();
}
void wxBitmapFromOpenCVPanel::OnPaint(wxPaintEvent&)
{
wxAutoBufferedPaintDC dc(this);
dc.Clear();
if ( !m_bitmap.IsOk() )
return;
const wxSize clientSize = GetClientSize();
wxPoint offset = GetViewStart();
int pixelsPerUnitX = 0, pixelsPerUnitY = 0;
wxStopWatch stopWatch;
stopWatch.Start();
DoPrepareDC(dc);
dc.DrawBitmap(m_bitmap, 0, 0, false);
GetScrollPixelsPerUnit(&pixelsPerUnitX, &pixelsPerUnitY);
offset.x *= pixelsPerUnitX; offset.y *= pixelsPerUnitY;
// Draw info "overlay", always at the top left corner of the window
// regardless of how the bitmap is scrolled.
const long drawTime = stopWatch.Time();
wxDCTextColourChanger textColourChanger(dc, m_overlayTextColour);
wxDCFontChanger fontChanger(dc, m_overlayFont);
dc.DrawText(wxString::Format("GetCVBitmap: %ld ms\nConvertCVtoWXBitmap: %ld ms\nDrawWXBitmap: %ld ms\n",
m_timeGetCVBitmap, m_timeConvertBitmap, drawTime),
offset);
}
void wxBitmapFromOpenCVPanel::OnChangeOverlayTextColour(wxMouseEvent&)
{
const wxColour colour = wxGetColourFromUser(this, m_overlayTextColour,
"Color for text overlay");
if ( !colour.IsOk() )
return;
m_overlayTextColour = colour;
Refresh(); Update();
}
void wxBitmapFromOpenCVPanel::OnChangeOverlayFont(wxMouseEvent&)
{
const wxFont font = wxGetFontFromUser(this, m_overlayFont,
"Font for text overlay");
if ( !font.IsOk() )
return;
m_overlayFont = font;
Refresh(); Update();
}