Hacks. tiff
authorCarl Hetherington <cth@carlh.net>
Mon, 7 Nov 2016 11:25:14 +0000 (11:25 +0000)
committerCarl Hetherington <cth@carlh.net>
Mon, 7 Nov 2016 11:25:14 +0000 (11:25 +0000)
j2kbench.cc

index f81e7f6c809e9cf14cee3c52833724142a3af2d0..8dd29ff9b3c634c8a4ba13383b0607d03f98a78e 100644 (file)
@@ -10,46 +10,79 @@ using std::cout;
 using boost::shared_ptr;
 
 void
-thread (int iterations)
+thread (int iterations, shared_ptr<const dcp::OpenJPEGImage> image)
 {
        int const width = 1998;
        int const height = 1080;
 
        for (int i = 0; i < iterations; ++i) {
-               shared_ptr<const dcp::OpenJPEGImage> xyz (new dcp::OpenJPEGImage (dcp::Size (width, height)));
-               int32_t* X = xyz->data(0);
-               int32_t* Y = xyz->data(1);
-               int32_t* Z = xyz->data(2);
-               for (int y = 0; y < height; ++y) {
-                       for (int x = 0; x < width; ++x)  {
-                               *X++ = x;
-                               *Y++ = y;
-                               *Z++ = (x * y) % 4096;
-                       }
-               }
-
-               dcp::compress_j2k (xyz, 100000000, 24, false, false);
+               dcp::compress_j2k (image, 100000000, 24, false, false);
        }
 }
 
+shared_ptr<const dcp::OpenJPEGImage>
+load_frame (boost::filesystem::path file)
+{
+       boost::uintmax_t const size = boost::filesystem::file_size (file);
+       FILE* f = fopen_boost (path, "rb");
+       if (!f) {
+               cerr << "Could not open " << file.string() << "\n";
+               exit (EXIT_FAILURE);
+       }
+
+       uint8_t* data = new uint8_t[size];
+       if (fread (data, 1, size, f) != size) {
+               delete[] data;
+               cerr << "Could not read " << file.string() << "\n";
+               exit (EXIT_FAILURE);
+       }
+
+       fclose (f);
+       Magick::Blob blob;
+       blob.update (data, size);
+       delete[] data;
+
+       Magick::Image* magick_image = new Magick::Image (blob);
+       dcp::Size size (magick_image->columns(), magick_image->rows());
+       int const stride = size.width() * 6;
+
+       uint8_t* rgb = new uint8_t[size.width() * size.height() * 6];
+
+       /* Write line-by-line here as _image must be aligned, and write() cannot be told about strides */
+       uint8_t* p = data;
+       for (int i = 0; i < size.height; ++i) {
+               using namespace MagickCore;
+               magick_image->write (0, i, size.width, 1, "RGB", ShortPixel, p);
+               p += stride;
+       }
+
+       delete magick_image;
+
+       return dcp::rgb_to_xyz (rgb, size, stride, dcp::ColourConversion::rec709_to_xyz(), optional<dcp::NoteHandler> ());
+}
+
+
 int
 main (int argc, char* argv[])
 {
-       if (argc < 3) {
-               cout << "Syntax: " << argv[0] << " <threads> <iterations>\n";
+       if (argc < 4) {
+               cout << "Syntax: " << argv[0] << " <threads> <iterations> <frame file>\n";
                exit (EXIT_FAILURE);
        }
 
        int const num_threads = atoi (argv[1]);
        int const num_iterations = atoi (argv[2]);
-       cout << num_threads << " threads, " << num_iterations << " iterations.\n";
+       boost::filesystem::path frame_file = argv[3];
+       cout << num_threads << " threads, " << num_iterations << " iterations, frame file " << frame_file.string() << ".\n";
+
+       shared_ptr<const dcp::OpenJPEGImage> image = load_frame (frame_file);
 
        struct timeval start;
        gettimeofday (&start, 0);
 
        list<boost::thread*> threads;
        for (int i = 0; i < num_threads; ++i) {
-               threads.push_back (new boost::thread (boost::bind (&thread, num_iterations)));
+               threads.push_back (new boost::thread (boost::bind (&thread, num_iterations, image)));
        }
 
        BOOST_FOREACH (boost::thread* i, threads) {