Take DCP-o-matic's version of Data class.
authorCarl Hetherington <cth@carlh.net>
Sun, 29 Nov 2015 23:45:59 +0000 (23:45 +0000)
committerCarl Hetherington <cth@carlh.net>
Fri, 4 Dec 2015 21:12:46 +0000 (21:12 +0000)
src/data.cc
src/data.h
src/interop_subtitle_asset.cc
src/j2k.cc
src/smpte_subtitle_asset.cc
test/bench.cc
test/dcp_font_test.cc
tools/dcpdumpsub.cc

index 66970c91b4f1b7f72e208041ba2b42ab08bdeb3d..e843736100012e562805596dcdcda60d0de0bb2f 100644 (file)
 
 */
 
-/** @file  src/data.cc
- *  @brief Data class.
- */
-
 #include "data.h"
 #include "util.h"
 #include "exceptions.h"
 #include <cstdio>
+#include <cerrno>
 
+using boost::shared_array;
 using namespace dcp;
 
-/** Construct a Data object from the contents of a file.
- *  @param file File to read.
- */
+Data::Data ()
+       : _size (0)
+{
+
+}
+
+Data::Data (int size)
+       : _data (new uint8_t[size])
+       , _size (size)
+{
+
+}
+
+Data::Data (uint8_t const * data, int size)
+       : _data (new uint8_t[size])
+       , _size (size)
+{
+       memcpy (_data.get(), data, size);
+}
+
+Data::Data (shared_array<uint8_t> data, int size)
+       : _data (data)
+       , _size (size)
+{
+
+}
+
 Data::Data (boost::filesystem::path file)
 {
+       _size = boost::filesystem::file_size (file);
+       _data.reset (new uint8_t[_size]);
+
        FILE* f = fopen_boost (file, "rb");
        if (!f) {
                throw FileError ("could not open file for reading", file, errno);
        }
 
-       size = boost::filesystem::file_size (file);
-       data.reset (new uint8_t[size]);
-       size_t const read = fread (data.get(), 1, size, f);
+       size_t const r = fread (_data.get(), 1, _size, f);
+       if (r != size_t (_size)) {
+               fclose (f);
+               throw FileError ("could not read from file", file, errno);
+       }
+
        fclose (f);
+}
 
-       if (read != size) {
-               throw FileError ("could not read file", file, -1);
+void
+Data::write (boost::filesystem::path file) const
+{
+       FILE* f = fopen_boost (file, "wb");
+       if (!f) {
+               throw FileError ("could not write to file", file, errno);
+       }
+       size_t const r = fwrite (_data.get(), 1, _size, f);
+       if (r != size_t (_size)) {
+               fclose (f);
+               throw FileError ("could not write to file", file, errno);
        }
+       fclose (f);
 }
 
-Data::Data (uint8_t const * data_, boost::uintmax_t size_)
-       : data (new uint8_t[size])
-       , size (size_)
+void
+Data::write_via_temp (boost::filesystem::path temp, boost::filesystem::path final) const
 {
-       memcpy (data.get(), data_, size);
+       write (temp);
+       boost::filesystem::rename (temp, final);
 }
index 12949fe44fa346a0c6ffeda76c590eeb3bdca762..606890312f525c35c4af066513a8da1af6bbd2c5 100644 (file)
 #ifndef LIBDCP_DATA_H
 #define LIBDCP_DATA_H
 
-/** @file  src/data.h
- *  @brief Data class.
- */
-
 #include <boost/shared_array.hpp>
 #include <boost/filesystem.hpp>
 #include <stdint.h>
 
 namespace dcp {
 
-/** A block of arbitrary data */
 class Data
 {
 public:
-       Data () {}
+       Data ();
+       Data (int size);
+       Data (uint8_t const * data, int size);
+       Data (boost::shared_array<uint8_t> data, int size);
+       Data (boost::filesystem::path file);
 
-       Data (boost::shared_array<uint8_t> data_, boost::uintmax_t size_)
-               : data (data_)
-               , size (size_)
-       {}
+       virtual ~Data () {}
 
-       Data (uint8_t const * data, boost::uintmax_t size_);
+       void write (boost::filesystem::path file) const;
+       void write_via_temp (boost::filesystem::path temp, boost::filesystem::path final) const;
 
-       Data (boost::filesystem::path file);
+       boost::shared_array<uint8_t> data () const {
+               return _data;
+       }
+
+       int size () const {
+               return _size;
+       }
 
-       boost::shared_array<uint8_t> data;
-       boost::uintmax_t size;
+private:
+       boost::shared_array<uint8_t> _data;
+       int _size;
 };
 
 }
index 8b88ce6b1191cf3c8263a080ef07b20f775d5cf0..4ba24a19595ef45a14e2fdfec0293e9e7fb5ecb0 100644 (file)
@@ -173,7 +173,7 @@ InteropSubtitleAsset::write (boost::filesystem::path p) const
                        ++j;
                }
                if (j != _fonts.end ()) {
-                       fwrite (j->data.data.get(), 1, j->data.size, f);
+                       fwrite (j->data.data().get(), 1, j->data.size(), f);
                        j->file = file;
                }
                fclose (f);
index 5318f0c291292e42047c5aea710d3e27bdb931d7..c5502ec34f645a7411a9b8b3fee7ba32850c4a02 100644 (file)
@@ -35,7 +35,7 @@ using namespace dcp;
 shared_ptr<dcp::OpenJPEGImage>
 dcp::decompress_j2k (Data data, int reduce)
 {
-       return dcp::decompress_j2k (data.data.get(), data.size, reduce);
+       return dcp::decompress_j2k (data.data().get(), data.size(), reduce);
 }
 
 class ReadBuffer
@@ -153,7 +153,7 @@ public:
        OPJ_SIZE_T write (void* buffer, OPJ_SIZE_T nb_bytes)
        {
                DCP_ASSERT ((_offset + nb_bytes) < MAX_J2K_SIZE);
-               memcpy (_data.data.get() + _offset, buffer, nb_bytes);
+               memcpy (_data.data().get() + _offset, buffer, nb_bytes);
                _offset += nb_bytes;
                return nb_bytes;
        }
index 08462a4aef609df52282866ebe9f10e665fcc85e..00a9299aacf7957cff023375267dac1ba83a84da 100644 (file)
@@ -273,8 +273,8 @@ SMPTESubtitleAsset::write (boost::filesystem::path p) const
                }
                if (j != _fonts.end ()) {
                        ASDCP::TimedText::FrameBuffer buffer;
-                       buffer.SetData (j->data.data.get(), j->data.size);
-                       buffer.Size (j->data.size);
+                       buffer.SetData (j->data.data().get(), j->data.size());
+                       buffer.Size (j->data.size());
                        r = writer.WriteAncillaryResource (buffer);
                        if (ASDCP_FAILURE (r)) {
                                boost::throw_exception (MXFFileError ("could not write font to timed text resource", p.string(), r));
index e76b3a970202c099d8229890a0106e71a5a7a569..be66f9089a6c8cc0b798ab997ad91871a8458252 100644 (file)
@@ -96,6 +96,6 @@ main (int argc, char* argv[])
        cout << "Compress:   " << count / compress.get() << " fps.\n";
 
        FILE* f = fopen ("check.j2c", "wb");
-       fwrite (recomp.data.get(), 1, recomp.size, f);
+       fwrite (recomp.data().get(), 1, recomp.size(), f);
        fclose (f);
 }
index daa16940cabfe097aba949fde6b1a3b2575a0751..83aef25582d992c6ed5936e25d988981391f9816 100644 (file)
@@ -69,7 +69,7 @@ BOOST_AUTO_TEST_CASE (interop_dcp_font_test)
        fread (ref.get(), 1, size, f);
        fclose (f);
 
-       BOOST_CHECK_EQUAL (memcmp (subs2->_fonts.front().data.data.get(), ref.get(), size), 0);
+       BOOST_CHECK_EQUAL (memcmp (subs2->_fonts.front().data.data().get(), ref.get(), size), 0);
 }
 
 /** Create a DCP with SMPTE subtitles and check that the font is written and read back correctly */
@@ -106,6 +106,6 @@ BOOST_AUTO_TEST_CASE (smpte_dcp_font_test)
        fread (ref.get(), 1, size, f);
        fclose (f);
 
-       BOOST_REQUIRE (subs2->_fonts.front().data.data);
-       BOOST_CHECK_EQUAL (memcmp (subs2->_fonts.front().data.data.get(), ref.get(), size), 0);
+       BOOST_REQUIRE (subs2->_fonts.front().data.data());
+       BOOST_CHECK_EQUAL (memcmp (subs2->_fonts.front().data.data().get(), ref.get(), size), 0);
 }
index ac9c2522aa046dbcd869ee87e8c03a34c22428c5..ca98551b742550b5f42b11dc706801415ef0c491 100644 (file)
@@ -83,7 +83,7 @@ main (int argc, char* argv[])
                                cerr << "Could not open font file " << i->first << ".ttf for writing";
                                exit (EXIT_FAILURE);
                        }
-                       fwrite (i->second.data.get(), 1, i->second.size, f);
+                       fwrite (i->second.data().get(), 1, i->second.size(), f);
                        fclose (f);
                }
        }