@@ -34,6 +34,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3434#include < cmath>
3535
3636#include < OpenImageIO/imageio.h>
37+ #include < OpenImageIO/imagebuf.h>
38+ #include < OpenImageIO/imagebufalgo.h>
3739#include < OpenImageIO/argparse.h>
3840#include < OpenImageIO/strutil.h>
3941#include < OpenImageIO/timer.h>
@@ -655,7 +657,10 @@ int main (int argc, const char *argv[]) {
655657
656658 double setuptime = timer.lap ();
657659
660+ // Local memory for the pixels
658661 std::vector<Color3> pixels (xres * yres, Color3 (0 ,0 ,0 ));
662+ // Make an ImageBuf that wraps it ('pixels' still owns the memory)
663+ ImageBuf pixelbuf (ImageSpec (xres, yres, 3 , TypeDesc::FLOAT), pixels.data ());
659664
660665 // Create shared counter to iterate over one scanline at a time
661666 Counter scanline_counter (errhandler, yres, " Rendering" );
@@ -664,23 +669,31 @@ int main (int argc, const char *argv[]) {
664669 for (int i = 0 ; i < num_threads; i++)
665670 workers.add_thread (new std::thread (scanline_worker, std::ref (scanline_counter), std::ref (pixels)));
666671 workers.join_all ();
672+ double runtime = timer.lap ();
667673
668674 // Write image to disk
669- ImageOutput* out = ImageOutput::create (imagefile);
670- ImageSpec spec (xres, yres, 3 , TypeDesc::HALF);
671- if (out && out->open (imagefile, spec)) {
672- out->write_image (TypeDesc::TypeFloat, &pixels[0 ]);
673- } else {
674- errhandler.error (" Unable to write output image" );
675+ if (Strutil::iends_with (imagefile, " .jpg" ) ||
676+ Strutil::iends_with (imagefile, " .jpeg" ) ||
677+ Strutil::iends_with (imagefile, " .gif" ) ||
678+ Strutil::iends_with (imagefile, " .png" )) {
679+ // JPEG, GIF, and PNG images should be automatically saved as sRGB
680+ // because they are almost certainly supposed to be displayed on web
681+ // pages.
682+ ImageBufAlgo::colorconvert (pixelbuf, pixelbuf,
683+ " linear" , " sRGB" , false , " " , " " );
675684 }
676- delete out;
685+ pixelbuf.set_write_format (TypeDesc::HALF);
686+ if (! pixelbuf.write (imagefile))
687+ errhandler.error (" Unable to write output image: %s" ,
688+ pixelbuf.geterror ().c_str ());
677689
678690 // Print some debugging info
679691 if (debug1 || runstats || profile) {
680- double runtime = timer.lap ();
692+ double writetime = timer.lap ();
681693 std::cout << " \n " ;
682694 std::cout << " Setup: " << OIIO::Strutil::timeintervalformat (setuptime,2 ) << " \n " ;
683695 std::cout << " Run : " << OIIO::Strutil::timeintervalformat (runtime,2 ) << " \n " ;
696+ std::cout << " Write: " << OIIO::Strutil::timeintervalformat (writetime,2 ) << " \n " ;
684697 std::cout << " \n " ;
685698 std::cout << shadingsys->getstats (5 ) << " \n " ;
686699 OIIO::TextureSystem *texturesys = shadingsys->texturesys ();
0 commit comments