Commit d3bc010e authored by Davis King's avatar Davis King

Added the image_window and image_display objects.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402965
parent 95ccc17e
......@@ -5650,6 +5650,240 @@ namespace dlib
}
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// image_display member functions
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
image_display::
image_display(
drawable_window& w
): scrollable_region(w)
{
enable_mouse_drag();
set_horizontal_scroll_increment(1);
set_vertical_scroll_increment(1);
set_horizontal_mouse_wheel_scroll_increment(30);
set_vertical_mouse_wheel_scroll_increment(30);
enable_events();
}
// ----------------------------------------------------------------------------------------
image_display::
~image_display(
)
{
disable_events();
parent.invalidate_rectangle(rect);
}
// ----------------------------------------------------------------------------------------
void image_display::
add_overlay (
const overlay_rect& overlay
)
{
auto_mutex M(m);
// push this new overlay into our overlay vector
overlay_rects.push_back(overlay);
// make the parent window redraw us now that we changed the overlay
parent.invalidate_rectangle(rect);
}
// ----------------------------------------------------------------------------------------
void image_display::
add_overlay (
const overlay_line& overlay
)
{
auto_mutex M(m);
// push this new overlay into our overlay vector
overlay_lines.push_back(overlay);
// make the parent window redraw us now that we changed the overlay
parent.invalidate_rectangle(rect);
}
// ----------------------------------------------------------------------------------------
void image_display::
add_overlay (
const std::vector<overlay_rect>& overlay
)
{
auto_mutex M(m);
// push this new overlay into our overlay vector
overlay_rects.insert(overlay_rects.end(), overlay.begin(), overlay.end());
// make the parent window redraw us now that we changed the overlay
parent.invalidate_rectangle(rect);
}
// ----------------------------------------------------------------------------------------
void image_display::
add_overlay (
const std::vector<overlay_line>& overlay
)
{
auto_mutex M(m);
// push this new overlay into our overlay vector
overlay_lines.insert(overlay_lines.end(), overlay.begin(), overlay.end());
// make the parent window redraw us now that we changed the overlay
parent.invalidate_rectangle(rect);
}
// ----------------------------------------------------------------------------------------
void image_display::
clear_overlay (
)
{
auto_mutex M(m);
overlay_rects.clear();
overlay_lines.clear();
parent.invalidate_rectangle(rect);
}
// ----------------------------------------------------------------------------------------
void image_display::
draw (
const canvas& c
) const
{
scrollable_region::draw(c);
rectangle area = display_rect().intersect(c);
if (area.is_empty())
return;
const point origin(total_rect().tl_corner());
draw_image(c, origin, img, area);
// now draw all the overlay rectangles
for (unsigned long i = 0; i < overlay_rects.size(); ++i)
{
draw_rectangle(c, translate_rect(overlay_rects[i].rect, origin), overlay_rects[i].color, area);
if (overlay_rects[i].label.size() != 0)
{
// make a rectangle that is at the spot we want to draw our string
rectangle r(overlay_rects[i].rect.br_corner(),
overlay_rects[i].rect.br_corner() + point(10000,10000));
r = translate_rect(r, origin);
mfont->draw_string(c, r, overlay_rects[i].label, overlay_rects[i].color, 0,
std::string::npos, area);
}
}
// now draw all the overlay lines
for (unsigned long i = 0; i < overlay_lines.size(); ++i)
{
draw_line(c, overlay_lines[i].p1+origin, overlay_lines[i].p2+origin, overlay_lines[i].color, area);
}
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// image_window member functions
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
image_window::
image_window(
) :
gui_img(*this)
{
// show this window on the screen
show();
}
// ----------------------------------------------------------------------------------------
image_window::
~image_window(
)
{
// You should always call close_window() in the destructor of window
// objects to ensure that no events will be sent to this window while
// it is being destructed.
close_window();
}
// ----------------------------------------------------------------------------------------
void image_window::
add_overlay (
const overlay_rect& overlay
)
{
gui_img.add_overlay(overlay);
}
// ----------------------------------------------------------------------------------------
void image_window::
add_overlay (
const overlay_line& overlay
)
{
gui_img.add_overlay(overlay);
}
// ----------------------------------------------------------------------------------------
void image_window::
add_overlay (
const std::vector<overlay_rect>& overlay
)
{
gui_img.add_overlay(overlay);
}
// ----------------------------------------------------------------------------------------
void image_window::
add_overlay (
const std::vector<overlay_line>& overlay
)
{
gui_img.add_overlay(overlay);
}
// ----------------------------------------------------------------------------------------
void image_window::
clear_overlay (
)
{
gui_img.clear_overlay();
}
// ----------------------------------------------------------------------------------------
void image_window::
on_window_resized(
)
{
drawable_window::on_window_resized();
unsigned long width, height;
get_size(width,height);
gui_img.set_size(width, height);
}
// ----------------------------------------------------------------------------------------
}
......
......@@ -15,6 +15,7 @@
#include "base_widgets.h"
#include "../member_function_pointer.h"
#include "../array.h"
#include "../array2d.h"
#include "../sequence.h"
#include "../dir_nav.h"
#include "../queue.h"
......@@ -23,6 +24,7 @@
#include "../string.h"
#include "../misc_api.h"
#include <cctype>
#include <vector>
#ifdef _MSC_VER
// This #pragma directive is also located in the algs.h file but for whatever
......@@ -3042,6 +3044,170 @@ namespace dlib
member_function_pointer<unsigned long, unsigned long>::kernel_1a_c text_modified_handler;
};
// ----------------------------------------------------------------------------------------
class image_display : public scrollable_region
{
/*!
INITIAL VALUE
- img.size() == 0
- overlay_rects.size() == 0
- overlay_lines.size() == 0
CONVENTION
- img == the image this object displays
- overlay_rects == the overlay rectangles this object displays
- overlay_lines == the overlay lines this object displays
!*/
public:
image_display(
drawable_window& w
);
~image_display(
);
template <
typename image_type
>
void set_image (
const image_type& new_img
)
{
auto_mutex M(m);
assign_image(img,new_img);
set_total_rect_size(img.nc(), img.nr());
}
struct overlay_rect
{
overlay_rect() { assign_pixel(color, 0);}
template <typename pixel_type>
overlay_rect(const rectangle& r, pixel_type p)
: rect(r) { assign_pixel(color, p); }
template <typename pixel_type>
overlay_rect(const rectangle& r, pixel_type p, const std::string& l)
: rect(r),label(l) { assign_pixel(color, p); }
rectangle rect;
rgb_alpha_pixel color;
std::string label;
};
struct overlay_line
{
overlay_line() { assign_pixel(color, 0);}
template <typename pixel_type>
overlay_line(const point& p1_, const point& p2_, pixel_type p)
: p1(p1_), p2(p2_) { assign_pixel(color, p); }
point p1;
point p2;
rgb_alpha_pixel color;
};
void add_overlay (
const overlay_rect& overlay
);
void add_overlay (
const overlay_line& overlay
);
void add_overlay (
const std::vector<overlay_rect>& overlay
);
void add_overlay (
const std::vector<overlay_line>& overlay
);
void clear_overlay (
);
private:
void draw (
const canvas& c
) const;
array2d<rgb_alpha_pixel>::kernel_1a img;
std::vector<overlay_rect> overlay_rects;
std::vector<overlay_line> overlay_lines;
// restricted functions
image_display(image_display&); // copy constructor
image_display& operator=(image_display&); // assignment operator
};
// ----------------------------------------------------------------------------------------
class image_window : public drawable_window
{
public:
typedef image_display::overlay_rect overlay_rect;
typedef image_display::overlay_line overlay_line;
image_window(
);
~image_window(
);
template < typename image_type >
void set_image (
const image_type& img
)
{
auto_mutex M(wm);
gui_img.set_image(img);
// set the size of this window to match the size of the input image
set_size(img.nc(),img.nr());
// call this to make sure everything else is setup properly
on_window_resized();
}
void add_overlay (
const overlay_rect& overlay
);
void add_overlay (
const overlay_line& overlay
);
void add_overlay (
const std::vector<overlay_rect>& overlay
);
void add_overlay (
const std::vector<overlay_line>& overlay
);
void clear_overlay (
);
private:
void on_window_resized(
);
// restricted functions
image_window(image_window&);
image_window& operator= (image_window&);
image_display gui_img;
};
// ----------------------------------------------------------------------------------------
}
......
......@@ -2018,6 +2018,291 @@ namespace dlib
text_grid& operator=(text_grid&); // assignment operator
};
// ----------------------------------------------------------------------------------------
class image_display : public scrollable_region
{
/*!
INITIAL VALUE
This object isn't displaying anything.
WHAT THIS OBJECT REPRESENTS
This object represents an image inside a scrollable region.
You give it an image to display by calling set_image().
This widget also allows you to add rectangle and line overlays that
will be drawn on top of the image.
The image is drawn such that:
- the pixel img[0][0] is the upper left corner of the image.
- the pixel img[img.nr()-1][0] is the lower left corner of the image.
- the pixel img[0][img.nc()-1] is the upper right corner of the image.
- the pixel img[img.nr()-1][img.nc()-1] is the lower right corner of the image.
!*/
public:
image_display(
drawable_window& w
);
/*!
ensures
- #*this is properly initialized
- #*this has been added to window w
- #parent_window() == w
!*/
~image_display(
);
/*!
ensures
- all resources associated with *this have been released
!*/
template <
typename image_type
>
void set_image (
const image_type& new_img
);
/*!
requires
- image_type == an implementation of array2d/array2d_kernel_abstract.h
- pixel_traits<typename image_type::type> must be defined
ensures
- #*this widget is now displaying the given image new_img.
!*/
struct overlay_rect
{
/*!
WHAT THIS OBJECT REPRESENTS
This object represents a rectangle that is drawn on top of the
image shown by this object. Each rectangle is represented by
a rectangle object as well as a color and text label. The label
is drawn below the lower right corner of the rectangle.
!*/
rectangle rect;
rgb_alpha_pixel color;
std::string label;
overlay_rect(
);
/*!
ensures
- #color == rgb_alpha_pixel(0,0,0,0)
- #rect == rectangle()
- #label.size() == 0
!*/
template <typename pixel_type>
overlay_rect(
const rectangle& r,
pixel_type p
);
/*!
ensures
- #rect == r
- performs assign_pixel(color, p)
- #label.size() == 0
!*/
template <typename pixel_type>
overlay_rect(
const rectangle& r,
pixel_type p,
const std::string& l
);
/*!
ensures
- #rect == r
- performs assign_pixel(color, p)
- #label = l
!*/
};
struct overlay_line
{
/*!
WHAT THIS OBJECT REPRESENTS
This object represents a line that is drawn on top of the
image shown by this object. Each line is represented by
its two end points (p1 and p2) as well as a color.
!*/
point p1;
point p2;
rgb_alpha_pixel color;
overlay_line(
);
/*!
ensures
- #color == rgb_alpha_pixel(0,0,0,0)
- #p1 == point()
- #p2 == point()
!*/
template <typename pixel_type>
overlay_line(
const point& p1_,
const point& p2_,
pixel_type p
);
/*!
ensures
- performs assign_pixel(color, p)
- #p1 == p1_
- #p2 == p2_
!*/
};
void add_overlay (
const overlay_rect& overlay
);
/*!
ensures
- adds the given overlay rectangle into this object such
that it will be displayed.
!*/
void add_overlay (
const overlay_line& overlay
);
/*!
ensures
- adds the given overlay line into this object such
that it will be displayed.
!*/
void add_overlay (
const std::vector<overlay_rect>& overlay
);
/*!
ensures
- adds the given set of overlay rectangles into this object such
that they will be displayed.
!*/
void add_overlay (
const std::vector<overlay_line>& overlay
);
/*!
ensures
- adds the given set of overlay lines into this object such
that they will be displayed.
!*/
void clear_overlay (
);
/*!
ensures
- removes all overlays from this object.
!*/
private:
// restricted functions
image_display(image_display&); // copy constructor
image_display& operator=(image_display&); // assignment operator
};
// ----------------------------------------------------------------------------------------
class image_window : public drawable_window
{
/*!
INITIAL VALUE
- initially, this object is visible on the screen
WHAT THIS OBJECT REPRESENTS
This is a simple window that is just a container for an image_display.
It exists to make it easy to throw image_displays onto the screen
without having to put together your own drawable_window objects.
!*/
public:
typedef image_display::overlay_rect overlay_rect;
typedef image_display::overlay_line overlay_line;
image_window(
);
/*!
ensures
- this object is properly initialized
!*/
~image_window(
);
/*!
ensures
- any resources associated with this object have been released
!*/
template < typename image_type >
void set_image (
const image_type& img
);
/*!
requires
- image_type == an implementation of array2d/array2d_kernel_abstract.h
- pixel_traits<typename image_type::type> must be defined
ensures
- #*this window is now displaying the given image new_img.
!*/
void add_overlay (
const overlay_rect& overlay
);
/*!
ensures
- adds the given overlay rectangle into this object such
that it will be displayed.
!*/
void add_overlay (
const overlay_line& overlay
);
/*!
ensures
- adds the given overlay line into this object such
that it will be displayed.
!*/
void add_overlay (
const std::vector<overlay_rect>& overlay
);
/*!
ensures
- adds the given set of overlay rectangles into this object such
that they will be displayed.
!*/
void add_overlay (
const std::vector<overlay_line>& overlay
);
/*!
ensures
- adds the given set of overlay lines into this object such
that they will be displayed.
!*/
void clear_overlay (
);
/*!
ensures
- removes all overlays from this object.
!*/
private:
// restricted functions
image_window(image_window&);
image_window& operator= (image_window&);
};
// ----------------------------------------------------------------------------------------
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment