Last active
August 29, 2015 14:13
-
-
Save jrk/19d63e5aae54d71a831c to your computer and use it in GitHub Desktop.
Halide image_io JPEG saver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "jo_jpeg.cpp" // http://www.jonolick.com/uploads/7/9/2/1/7921194/jo_jpeg.cpp | |
template<typename T> | |
void save_jpeg(Image<T> im, std::string filename, int quality = 90) { | |
_assert(im.channels() > 0 && im.channels() < 5, | |
"Can't write JPEG files that have other than 1, 2, 3, or 4 channels\n"); | |
im.copy_to_host(); | |
// convert to 8-bit and interleave | |
uint8_t* buf = new uint8_t[im.width()*im.height()*im.channels()]; | |
int y_stride = im.width()*im.channels(), | |
x_stride = im.channels(); | |
for (int y = 0; y < im.height(); ++y) { | |
for (int x = 0; x < im.width(); ++x) { | |
for (int c = 0; c < im.channels(); ++c) { | |
convert(im(x, y, c), buf[y*y_stride + x*x_stride + c]); | |
} | |
} | |
} | |
bool success = jo_write_jpg(filename.c_str(), | |
(void*)buf, | |
im.width(), | |
im.height(), | |
im.channels(), | |
quality); | |
_assert(success, "Failed to write JPEG\n"); | |
delete buf; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment