Commit 18c305fc authored by Davis King's avatar Davis King

Changed the serialization formats for the matrix and array2d objects so that

they are compatible.  This was done in a way that is backwards compatible with
previous versions of dlib.  That is, we can still load data serialized by
previous dlib versions.  However, older versions of dlib can't load the new
serialization format.
parent adf0bc4f
...@@ -364,8 +364,12 @@ namespace dlib ...@@ -364,8 +364,12 @@ namespace dlib
{ {
try try
{ {
serialize(item.nc(),out); // The reason the serialization is a little funny is because we are trying to
serialize(item.nr(),out); // maintain backwards compatibility with an older serialization format used by
// dlib while also encoding things in a way that lets the array2d and matrix
// objects have compatible serialization formats.
serialize(-item.nr(),out);
serialize(-item.nc(),out);
item.reset(); item.reset();
while (item.move_next()) while (item.move_next())
...@@ -389,9 +393,20 @@ namespace dlib ...@@ -389,9 +393,20 @@ namespace dlib
{ {
try try
{ {
long nc, nr; long nr, nc;
deserialize(nc,in);
deserialize(nr,in); deserialize(nr,in);
deserialize(nc,in);
// this is the newer serialization format
if (nr < 0 || nc < 0)
{
nr *= -1;
nc *= -1;
}
else
{
std::swap(nr,nc);
}
item.set_size(nr,nc); item.set_size(nr,nc);
......
...@@ -265,7 +265,9 @@ namespace dlib ...@@ -265,7 +265,9 @@ namespace dlib
std::ostream& out std::ostream& out
); );
/*! /*!
provides serialization support Provides serialization support. Note that the serialization formats used by the
dlib::matrix and dlib::array2d objects are compatible. That means you can load the
serialized data from one into another and it will work properly.
!*/ !*/
template < template <
......
...@@ -31,8 +31,12 @@ namespace dlib ...@@ -31,8 +31,12 @@ namespace dlib
{ {
try try
{ {
serialize(item.nc(),out); // The reason the serialization is a little funny is because we are trying to
serialize(item.nr(),out); // maintain backwards compatibility with an older serialization format used by
// dlib while also encoding things in a way that lets the array2d and matrix
// objects have compatible serialization formats.
serialize(-item.nr(),out);
serialize(-item.nc(),out);
COMPILE_TIME_ASSERT(sizeof(rgb_pixel) == 3); COMPILE_TIME_ASSERT(sizeof(rgb_pixel) == 3);
...@@ -57,9 +61,20 @@ namespace dlib ...@@ -57,9 +61,20 @@ namespace dlib
{ {
COMPILE_TIME_ASSERT(sizeof(rgb_pixel) == 3); COMPILE_TIME_ASSERT(sizeof(rgb_pixel) == 3);
long nc, nr; long nr, nc;
deserialize(nc,in);
deserialize(nr,in); deserialize(nr,in);
deserialize(nc,in);
// this is the newer serialization format
if (nr < 0 || nc < 0)
{
nr *= -1;
nc *= -1;
}
else
{
std::swap(nr,nc);
}
item.set_size(nr,nc); item.set_size(nr,nc);
...@@ -85,8 +100,12 @@ namespace dlib ...@@ -85,8 +100,12 @@ namespace dlib
{ {
try try
{ {
serialize(item.nc(),out); // The reason the serialization is a little funny is because we are trying to
serialize(item.nr(),out); // maintain backwards compatibility with an older serialization format used by
// dlib while also encoding things in a way that lets the array2d and matrix
// objects have compatible serialization formats.
serialize(-item.nr(),out);
serialize(-item.nc(),out);
COMPILE_TIME_ASSERT(sizeof(bgr_pixel) == 3); COMPILE_TIME_ASSERT(sizeof(bgr_pixel) == 3);
...@@ -111,9 +130,21 @@ namespace dlib ...@@ -111,9 +130,21 @@ namespace dlib
{ {
COMPILE_TIME_ASSERT(sizeof(bgr_pixel) == 3); COMPILE_TIME_ASSERT(sizeof(bgr_pixel) == 3);
long nc, nr; long nr, nc;
deserialize(nc,in);
deserialize(nr,in); deserialize(nr,in);
deserialize(nc,in);
// this is the newer serialization format
if (nr < 0 || nc < 0)
{
nr *= -1;
nc *= -1;
}
else
{
std::swap(nr,nc);
}
item.set_size(nr,nc); item.set_size(nr,nc);
...@@ -139,8 +170,12 @@ namespace dlib ...@@ -139,8 +170,12 @@ namespace dlib
{ {
try try
{ {
serialize(item.nc(),out); // The reason the serialization is a little funny is because we are trying to
serialize(item.nr(),out); // maintain backwards compatibility with an older serialization format used by
// dlib while also encoding things in a way that lets the array2d and matrix
// objects have compatible serialization formats.
serialize(-item.nr(),out);
serialize(-item.nc(),out);
COMPILE_TIME_ASSERT(sizeof(hsi_pixel) == 3); COMPILE_TIME_ASSERT(sizeof(hsi_pixel) == 3);
...@@ -165,9 +200,21 @@ namespace dlib ...@@ -165,9 +200,21 @@ namespace dlib
{ {
COMPILE_TIME_ASSERT(sizeof(hsi_pixel) == 3); COMPILE_TIME_ASSERT(sizeof(hsi_pixel) == 3);
long nc, nr; long nr, nc;
deserialize(nc,in);
deserialize(nr,in); deserialize(nr,in);
deserialize(nc,in);
// this is the newer serialization format
if (nr < 0 || nc < 0)
{
nr *= -1;
nc *= -1;
}
else
{
std::swap(nr,nc);
}
item.set_size(nr,nc); item.set_size(nr,nc);
...@@ -193,8 +240,12 @@ namespace dlib ...@@ -193,8 +240,12 @@ namespace dlib
{ {
try try
{ {
serialize(item.nc(),out); // The reason the serialization is a little funny is because we are trying to
serialize(item.nr(),out); // maintain backwards compatibility with an older serialization format used by
// dlib while also encoding things in a way that lets the array2d and matrix
// objects have compatible serialization formats.
serialize(-item.nr(),out);
serialize(-item.nc(),out);
COMPILE_TIME_ASSERT(sizeof(rgb_alpha_pixel) == 4); COMPILE_TIME_ASSERT(sizeof(rgb_alpha_pixel) == 4);
...@@ -219,9 +270,21 @@ namespace dlib ...@@ -219,9 +270,21 @@ namespace dlib
{ {
COMPILE_TIME_ASSERT(sizeof(rgb_alpha_pixel) == 4); COMPILE_TIME_ASSERT(sizeof(rgb_alpha_pixel) == 4);
long nc, nr; long nr, nc;
deserialize(nc,in);
deserialize(nr,in); deserialize(nr,in);
deserialize(nc,in);
// this is the newer serialization format
if (nr < 0 || nc < 0)
{
nr *= -1;
nc *= -1;
}
else
{
std::swap(nr,nc);
}
item.set_size(nr,nc); item.set_size(nr,nc);
...@@ -247,8 +310,12 @@ namespace dlib ...@@ -247,8 +310,12 @@ namespace dlib
{ {
try try
{ {
serialize(item.nc(),out); // The reason the serialization is a little funny is because we are trying to
serialize(item.nr(),out); // maintain backwards compatibility with an older serialization format used by
// dlib while also encoding things in a way that lets the array2d and matrix
// objects have compatible serialization formats.
serialize(-item.nr(),out);
serialize(-item.nc(),out);
if (item.size() != 0) if (item.size() != 0)
out.write((char*)&item[0][0], sizeof(unsigned char)*item.size()); out.write((char*)&item[0][0], sizeof(unsigned char)*item.size());
...@@ -269,9 +336,20 @@ namespace dlib ...@@ -269,9 +336,20 @@ namespace dlib
{ {
try try
{ {
long nc, nr; long nr, nc;
deserialize(nc,in);
deserialize(nr,in); deserialize(nr,in);
deserialize(nc,in);
// this is the newer serialization format
if (nr < 0 || nc < 0)
{
nr *= -1;
nc *= -1;
}
else
{
std::swap(nr,nc);
}
item.set_size(nr,nc); item.set_size(nr,nc);
......
...@@ -1694,8 +1694,12 @@ namespace dlib ...@@ -1694,8 +1694,12 @@ namespace dlib
{ {
try try
{ {
serialize(item.nr(),out); // The reason the serialization is a little funny is because we are trying to
serialize(item.nc(),out); // maintain backwards compatibility with an older serialization format used by
// dlib while also encoding things in a way that lets the array2d and matrix
// objects have compatible serialization formats.
serialize(-item.nr(),out);
serialize(-item.nc(),out);
for (long r = 0; r < item.nr(); ++r) for (long r = 0; r < item.nr(); ++r)
{ {
for (long c = 0; c < item.nc(); ++c) for (long c = 0; c < item.nc(); ++c)
...@@ -1728,6 +1732,13 @@ namespace dlib ...@@ -1728,6 +1732,13 @@ namespace dlib
deserialize(nr,in); deserialize(nr,in);
deserialize(nc,in); deserialize(nc,in);
// this is the newer serialization format
if (nr < 0 || nc < 0)
{
nr *= -1;
nc *= -1;
}
if (NR != 0 && nr != NR) if (NR != 0 && nr != NR)
throw serialization_error("Error while deserializing a dlib::matrix. Invalid rows"); throw serialization_error("Error while deserializing a dlib::matrix. Invalid rows");
if (NC != 0 && nc != NC) if (NC != 0 && nc != NC)
......
...@@ -672,7 +672,9 @@ namespace dlib ...@@ -672,7 +672,9 @@ namespace dlib
std::ostream& out std::ostream& out
); );
/*! /*!
Provides serialization support Provides serialization support. Note that the serialization formats used by the
dlib::matrix and dlib::array2d objects are compatible. That means you can load the
serialized data from one into another and it will work properly.
!*/ !*/
template < template <
......
This diff is collapsed.
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