summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2025-01-02 23:52:35 +0100
committerCarl Hetherington <cth@carlh.net>2025-03-08 21:51:03 +0100
commit251c4cadd2708224bcf1774bf7114d44f9ff6c89 (patch)
tree5e505e5e57f556a56eece476b0e8a61554bfa21c
parentcbfad3e48bab16f0d8e1181d2d46194f4e52725c (diff)
Add image_from_jpeg().
-rw-r--r--src/lib/image_jpeg.cc37
-rw-r--r--src/lib/image_jpeg.h4
2 files changed, 41 insertions, 0 deletions
diff --git a/src/lib/image_jpeg.cc b/src/lib/image_jpeg.cc
index 28bbb7574..b92be0d9d 100644
--- a/src/lib/image_jpeg.cc
+++ b/src/lib/image_jpeg.cc
@@ -22,6 +22,7 @@
#include "dcpomatic_assert.h"
#include "exceptions.h"
#include "image.h"
+#include <dcp/array_data.h>
#include <jpeglib.h>
#include "i18n.h"
@@ -133,3 +134,39 @@ image_as_jpeg (shared_ptr<const Image> image, int quality)
}
+shared_ptr<Image>
+image_from_jpeg(boost::filesystem::path file)
+{
+ struct jpeg_decompress_struct decompress;
+ struct jpeg_error_mgr error;
+
+ decompress.err = jpeg_std_error(&error);
+ error.error_exit = error_exit;
+ jpeg_create_decompress(&decompress);
+
+ auto data = dcp::ArrayData(file);
+ jpeg_mem_src(&decompress, data.data(), data.size());
+
+ jpeg_read_header(&decompress, TRUE);
+
+ jpeg_start_decompress(&decompress);
+
+ DCPOMATIC_ASSERT(decompress.output_components == 3);
+
+ auto output = std::make_shared<Image>(
+ AV_PIX_FMT_RGB24,
+ dcp::Size{static_cast<int>(decompress.output_width), static_cast<int>(decompress.output_height)},
+ Image::Alignment::COMPACT
+ );
+
+ while (decompress.output_scanline < decompress.output_height) {
+ uint8_t* pointers[1] = { output->data()[0] + output->stride()[0] * decompress.output_scanline };
+ jpeg_read_scanlines(&decompress, pointers, 1);
+ }
+
+ jpeg_finish_decompress(&decompress);
+ jpeg_destroy_decompress(&decompress);
+
+ return output;
+}
+
diff --git a/src/lib/image_jpeg.h b/src/lib/image_jpeg.h
index 5c017fc2c..29097680e 100644
--- a/src/lib/image_jpeg.h
+++ b/src/lib/image_jpeg.h
@@ -19,4 +19,8 @@
*/
+class Image;
+
+
+std::shared_ptr<Image> image_from_jpeg(boost::filesystem::path file);
dcp::ArrayData image_as_jpeg (std::shared_ptr<const Image> image, int quality);