Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
409f70eb
Commit
409f70eb
authored
Feb 18, 2015
by
Davis King
Browse files
Made sub_image() a little more robust with regards to object lifetime
and const correctness.
parent
51527818
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
107 additions
and
24 deletions
+107
-24
dlib/image_transforms/interpolation.h
dlib/image_transforms/interpolation.h
+73
-18
dlib/image_transforms/interpolation_abstract.h
dlib/image_transforms/interpolation_abstract.h
+34
-6
No files found.
dlib/image_transforms/interpolation.h
View file @
409f70eb
...
@@ -20,13 +20,46 @@ namespace dlib
...
@@ -20,13 +20,46 @@ namespace dlib
struct
sub_image_proxy
struct
sub_image_proxy
{
{
sub_image_proxy
(
sub_image_proxy
(
T
&
img_
,
T
&
img
,
const
rectangle
&
rect_
rectangle
rect
)
:
img
(
img_
),
rect
(
rect_
.
intersect
(
get_rect
(
img_
)))
)
{}
{
rect
=
rect
.
intersect
(
get_rect
(
img
));
typedef
typename
image_traits
<
T
>::
pixel_type
pixel_type
;
_nr
=
rect
.
height
();
_nc
=
rect
.
width
();
_width_step
=
width_step
(
img
);
_data
=
(
char
*
)
image_data
(
img
)
+
sizeof
(
pixel_type
)
*
rect
.
left
()
+
rect
.
top
()
*
_width_step
;
}
void
*
_data
;
long
_width_step
;
long
_nr
;
long
_nc
;
};
template
<
typename
T
>
struct
const_sub_image_proxy
{
const_sub_image_proxy
(
const
T
&
img
,
rectangle
rect
)
{
rect
=
rect
.
intersect
(
get_rect
(
img
));
typedef
typename
image_traits
<
T
>::
pixel_type
pixel_type
;
T
&
img
;
_nr
=
rect
.
height
();
const
rectangle
rect
;
_nc
=
rect
.
width
();
_width_step
=
width_step
(
img
);
_data
=
(
const
char
*
)
image_data
(
img
)
+
sizeof
(
pixel_type
)
*
rect
.
left
()
+
rect
.
top
()
*
_width_step
;
}
const
void
*
_data
;
long
_width_step
;
long
_nr
;
long
_nc
;
};
};
template
<
typename
T
>
template
<
typename
T
>
...
@@ -39,32 +72,53 @@ namespace dlib
...
@@ -39,32 +72,53 @@ namespace dlib
{
{
typedef
typename
image_traits
<
T
>::
pixel_type
pixel_type
;
typedef
typename
image_traits
<
T
>::
pixel_type
pixel_type
;
};
};
template
<
typename
T
>
struct
image_traits
<
const_sub_image_proxy
<
T
>
>
{
typedef
typename
image_traits
<
T
>::
pixel_type
pixel_type
;
};
template
<
typename
T
>
struct
image_traits
<
const
const_sub_image_proxy
<
T
>
>
{
typedef
typename
image_traits
<
T
>::
pixel_type
pixel_type
;
};
template
<
typename
T
>
template
<
typename
T
>
inline
long
num_rows
(
const
sub_image_proxy
<
T
>&
img
)
{
return
img
.
rect
.
height
()
;
}
inline
long
num_rows
(
const
sub_image_proxy
<
T
>&
img
)
{
return
img
.
_nr
;
}
template
<
typename
T
>
template
<
typename
T
>
inline
long
num_columns
(
const
sub_image_proxy
<
T
>&
img
)
{
return
img
.
rect
.
width
();
}
inline
long
num_columns
(
const
sub_image_proxy
<
T
>&
img
)
{
return
img
.
_nc
;
}
template
<
typename
T
>
inline
long
num_rows
(
const
const_sub_image_proxy
<
T
>&
img
)
{
return
img
.
_nr
;
}
template
<
typename
T
>
inline
long
num_columns
(
const
const_sub_image_proxy
<
T
>&
img
)
{
return
img
.
_nc
;
}
template
<
typename
T
>
template
<
typename
T
>
inline
void
*
image_data
(
sub_image_proxy
<
T
>&
img
)
inline
void
*
image_data
(
sub_image_proxy
<
T
>&
img
)
{
{
typedef
typename
image_traits
<
T
>::
pixel_type
pixel_type
;
return
img
.
_data
;
return
(
char
*
)
image_data
(
img
.
img
)
+
sizeof
(
pixel_type
)
*
img
.
rect
.
left
()
+
img
.
rect
.
top
()
*
width_step
(
img
);
}
}
template
<
typename
T
>
template
<
typename
T
>
inline
const
void
*
image_data
(
const
sub_image_proxy
<
T
>&
img
)
inline
const
void
*
image_data
(
const
sub_image_proxy
<
T
>&
img
)
{
{
typedef
typename
image_traits
<
T
>::
pixel_type
pixel_type
;
return
img
.
_data
;
return
(
const
char
*
)
image_data
(
img
.
img
)
+
sizeof
(
pixel_type
)
*
img
.
rect
.
left
()
+
img
.
rect
.
top
()
*
width_step
(
img
);
}
template
<
typename
T
>
inline
const
void
*
image_data
(
const
const_sub_image_proxy
<
T
>&
img
)
{
return
img
.
_data
;
}
}
template
<
typename
T
>
template
<
typename
T
>
inline
long
width_step
(
inline
long
width_step
(
const
sub_image_proxy
<
T
>&
img
const
sub_image_proxy
<
T
>&
img
)
)
{
return
img
.
_width_step
;
}
{
return
width_step
(
img
.
img
);
template
<
typename
T
>
}
inline
long
width_step
(
const
const_sub_image_proxy
<
T
>&
img
)
{
return
img
.
_width_step
;
}
template
<
template
<
typename
image_type
typename
image_type
...
@@ -80,14 +134,15 @@ namespace dlib
...
@@ -80,14 +134,15 @@ namespace dlib
template
<
template
<
typename
image_type
typename
image_type
>
>
const
sub_image_proxy
<
const
image_type
>
sub_image
(
const
const_
sub_image_proxy
<
image_type
>
sub_image
(
const
image_type
&
img
,
const
image_type
&
img
,
const
rectangle
&
rect
const
rectangle
&
rect
)
)
{
{
return
sub_image_proxy
<
const
image_type
>
(
img
,
rect
);
return
const_
sub_image_proxy
<
image_type
>
(
img
,
rect
);
}
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
class
interpolate_nearest_neighbor
class
interpolate_nearest_neighbor
...
...
dlib/image_transforms/interpolation_abstract.h
View file @
409f70eb
...
@@ -1155,10 +1155,10 @@ namespace dlib
...
@@ -1155,10 +1155,10 @@ namespace dlib
any function that expects a generic image, excepting that you cannot change
any function that expects a generic image, excepting that you cannot change
the size of a sub_image_proxy.
the size of a sub_image_proxy.
Note that it only stores a pointer to the image given to its
constructor
Note that it only stores a pointer to the image
data
given to its
and therefore does not perform a copy. Moreover, this means
that an
constructor
and therefore does not perform a copy. Moreover, this means
instance of this object becomes invalid after the
image it references is
that an
instance of this object becomes invalid after the
underlying image
destroyed.
data it references is
destroyed.
!*/
!*/
sub_image_proxy
(
sub_image_proxy
(
T
&
img
,
T
&
img
,
...
@@ -1187,10 +1187,38 @@ namespace dlib
...
@@ -1187,10 +1187,38 @@ namespace dlib
- returns sub_image_proxy<image_type>(img,rect)
- returns sub_image_proxy<image_type>(img,rect)
!*/
!*/
// ----------------------------------------------------------------------------------------
template
<
typename
image_type
>
struct
const_sub_image_proxy
{
/*!
REQUIREMENTS ON image_type
- image_type == an image object that implements the interface defined in
dlib/image_processing/generic_image.h
WHAT THIS OBJECT REPRESENTS
This object is just like sub_image_proxy except that it does not allow the
pixel data to be modified.
!*/
const_sub_image_proxy
(
const
T
&
img
,
const
rectangle
&
rect
);
/*!
ensures
- This object is an image that represents the part of img contained within
rect. If rect is larger than img then rect is cropped so that it does
not go outside img.
!*/
};
template
<
template
<
typename
image_type
typename
image_type
>
>
const
sub_image_proxy
<
const
image_type
>
sub_image
(
const
const_
sub_image_proxy
<
image_type
>
sub_image
(
const
image_type
&
img
,
const
image_type
&
img
,
const
rectangle
&
rect
const
rectangle
&
rect
);
);
...
@@ -1199,7 +1227,7 @@ namespace dlib
...
@@ -1199,7 +1227,7 @@ namespace dlib
- image_type == an image object that implements the interface defined in
- image_type == an image object that implements the interface defined in
dlib/image_processing/generic_image.h
dlib/image_processing/generic_image.h
ensures
ensures
- returns sub_image_proxy<
const
image_type>(img,rect)
- returns
const_
sub_image_proxy<image_type>(img,rect)
!*/
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment