@@ -615,6 +615,7 @@ write_mipmap(ImageBufAlgo::MakeTextureMode mode, std::shared_ptr<ImageBuf>& img,
615
615
double & stat_miptime, size_t & peak_mem)
616
616
{
617
617
using OIIO::pvt::errorfmt;
618
+ using OIIO::Strutil::sync::print; // Be sure to use synchronized one
618
619
bool envlatlmode = (mode == ImageBufAlgo::MakeTxEnvLatl);
619
620
bool orig_was_overscan = (img->spec ().x || img->spec ().y || img->spec ().z
620
621
|| img->spec ().full_x || img->spec ().full_y
@@ -710,11 +711,18 @@ write_mipmap(ImageBufAlgo::MakeTextureMode mode, std::shared_ptr<ImageBuf>& img,
710
711
return false ;
711
712
}
712
713
713
- stat_writetime += writetimer ();
714
+ double wtime = writetimer ();
715
+ stat_writetime += wtime;
716
+ if (verbose) {
717
+ size_t mem = Sysutil::memory_used (true );
718
+ peak_mem = std::max (peak_mem, mem);
719
+ print (outstream, " {:-15s} ({}) write {}\n " , formatres (outspec),
720
+ Strutil::memformat (mem), Strutil::timeintervalformat (wtime, 2 ));
721
+ }
714
722
715
723
if (mipmap) { // Mipmap levels:
716
724
if (verbose)
717
- outstream << " Mipmapping...\n " << std::flush ;
725
+ print ( outstream, " Mipmapping...\n " ) ;
718
726
std::vector<std::string> mipimages;
719
727
std::string mipimages_unsplit = configspec.get_string_attribute (
720
728
" maketx:mipimages" );
@@ -794,16 +802,16 @@ write_mipmap(ImageBufAlgo::MakeTextureMode mode, std::shared_ptr<ImageBuf>& img,
794
802
return false ;
795
803
}
796
804
if (verbose) {
797
- outstream << " Downsampling filter \" "
798
- << filter-> name ()
799
- << " \" width = " << filter->width ();
800
- if (sharpen > 0 .0f ) {
801
- outstream << " , sharpening " << sharpen << " with "
802
- << sharpenfilt << " unsharp mask "
803
- << (sharpen_first ? " before " : " after " )
804
- << " the resize " ;
805
- }
806
- outstream << " \n " ;
805
+ print ( outstream,
806
+ " Downsampling filter \" {} \" width = {} " ,
807
+ filter-> name (), filter->width () );
808
+ if (sharpen > 0 .0f )
809
+ print (
810
+ outstream,
811
+ " , sharpening {} with {} unsharp mask {} the resize " ,
812
+ sharpen, sharpenfilt,
813
+ (sharpen_first ? " before " : " after " ));
814
+ print ( outstream, " \n " ) ;
807
815
}
808
816
if (do_highlight_compensation)
809
817
ImageBufAlgo::rangecompress (*img, *img);
@@ -838,7 +846,8 @@ write_mipmap(ImageBufAlgo::MakeTextureMode mode, std::shared_ptr<ImageBuf>& img,
838
846
if (clamp_half)
839
847
ImageBufAlgo::clamp (*small, *small, -HALF_MAX, HALF_MAX, true );
840
848
841
- stat_miptime += miptimer ();
849
+ double this_miptime = miptimer ();
850
+ stat_miptime += this_miptime;
842
851
outspec = smallspec;
843
852
outspec.set_format (outputdatatype);
844
853
if (envlatlmode && src_samples_border)
@@ -863,22 +872,23 @@ write_mipmap(ImageBufAlgo::MakeTextureMode mode, std::shared_ptr<ImageBuf>& img,
863
872
out->close ();
864
873
return false ;
865
874
}
866
- stat_writetime += writetimer ();
875
+ double wtime = writetimer ();
876
+ stat_writetime += wtime;
867
877
if (verbose) {
868
878
size_t mem = Sysutil::memory_used (true );
869
879
peak_mem = std::max (peak_mem, mem);
870
- outstream << Strutil::sprintf ( " % -15s (%s) " ,
871
- formatres (smallspec),
872
- Strutil::memformat (mem))
873
- << std::endl ;
880
+ print ( outstream, " {: -15s} ({}) downres {} write {} \n " ,
881
+ formatres (smallspec), Strutil::memformat (mem ),
882
+ Strutil::timeintervalformat (this_miptime, 2 ),
883
+ Strutil::timeintervalformat (wtime, 2 )) ;
874
884
}
875
885
std::swap (img, small);
876
886
}
877
887
}
878
888
879
889
if (verbose)
880
- outstream << " Wrote file: " << outputfilename << " ( "
881
- << Strutil::memformat (Sysutil::memory_used (true )) << " ) \n " ;
890
+ print ( outstream, " Wrote file: {} ({}) \n " , outputfilename,
891
+ Strutil::memformat (Sysutil::memory_used (true ))) ;
882
892
writetimer.reset ();
883
893
writetimer.start ();
884
894
if (!out->close ()) {
@@ -965,6 +975,7 @@ make_texture_impl(ImageBufAlgo::MakeTextureMode mode, const ImageBuf* input,
965
975
const ImageSpec& _configspec, std::ostream* outstream_ptr)
966
976
{
967
977
using OIIO::pvt::errorfmt;
978
+ using OIIO::Strutil::sync::print; // Be sure to use synchronized one
968
979
OIIO_ASSERT (mode >= 0 && mode < ImageBufAlgo::_MakeTxLast);
969
980
double stat_readtime = 0 ;
970
981
double stat_writetime = 0 ;
@@ -974,15 +985,14 @@ make_texture_impl(ImageBufAlgo::MakeTextureMode mode, const ImageBuf* input,
974
985
size_t peak_mem = 0 ;
975
986
Timer alltime;
976
987
977
- #define STATUS (task, timer ) \
978
- { \
979
- size_t mem = Sysutil::memory_used (true ); \
980
- peak_mem = std::max (peak_mem, mem); \
981
- if (verbose) \
982
- outstream << Strutil::sprintf (" %-25s %s (%s)\n " , task, \
983
- Strutil::timeintervalformat (timer, \
984
- 2 ), \
985
- Strutil::memformat (mem)); \
988
+ #define STATUS (task, timer ) \
989
+ { \
990
+ size_t mem = Sysutil::memory_used (true ); \
991
+ peak_mem = std::max (peak_mem, mem); \
992
+ if (verbose) \
993
+ print (outstream, " {:-25s} {} ({})\n " , task, \
994
+ Strutil::timeintervalformat (timer, 2 ), \
995
+ Strutil::memformat (mem)); \
986
996
}
987
997
988
998
ImageSpec configspec = _configspec;
@@ -1273,11 +1283,13 @@ make_texture_impl(ImageBufAlgo::MakeTextureMode mode, const ImageBuf* input,
1273
1283
bool constant_color_detect = configspec.get_int_attribute (
1274
1284
" maketx:constant_color_detect" );
1275
1285
bool opaque_detect = configspec.get_int_attribute (" maketx:opaque_detect" );
1286
+ bool monochrome_detect = configspec.get_int_attribute (
1287
+ " maketx:monochrome_detect" );
1276
1288
bool compute_average_color
1277
1289
= configspec.get_int_attribute (" maketx:compute_average" , 1 );
1278
1290
ImageBufAlgo::PixelStats pixel_stats;
1279
1291
bool compute_stats = (constant_color_detect || opaque_detect
1280
- || compute_average_color);
1292
+ || compute_average_color || monochrome_detect );
1281
1293
if (compute_stats) {
1282
1294
pixel_stats = ImageBufAlgo::computePixelStats (*src);
1283
1295
}
@@ -1335,14 +1347,21 @@ make_texture_impl(ImageBufAlgo::MakeTextureMode mode, const ImageBuf* input,
1335
1347
std::swap (src, newsrc); // N.B. the old src will delete
1336
1348
}
1337
1349
1338
- // If requested - and we're a monochrome image - drop the extra channels
1339
- if (configspec.get_int_attribute (" maketx:monochrome_detect" )
1340
- && nchannels <= 0 && src->nchannels () == 3
1341
- && src->spec ().alpha_channel < 0 && // RGB only
1342
- ImageBufAlgo::isMonochrome (*src)) {
1350
+ // If requested - and we're a monochrome image - drop the extra channels.
1351
+ // In addition to only doing this for RGB images (3 channels, no alpha),
1352
+ // we also check the stat averages are the same for all three channels (if
1353
+ // the channel averages are not identical, they surely cannot be the same
1354
+ // for all pixels, so there is no point wasting the time of the call to
1355
+ // isMonochrome().
1356
+ if (monochrome_detect && nchannels <= 0 && src->nchannels () == 3
1357
+ && src->spec ().alpha_channel < 0
1358
+ && pixel_stats.avg [0 ] == pixel_stats.avg [1 ]
1359
+ && pixel_stats.avg [0 ] == pixel_stats.avg [2 ]
1360
+ && ImageBufAlgo::isMonochrome (*src)) {
1343
1361
if (verbose)
1344
- outstream
1345
- << " Monochrome image detected. Converting to single channel texture.\n " ;
1362
+ print (
1363
+ outstream,
1364
+ " Monochrome image detected. Converting to single channel texture.\n " );
1346
1365
std::shared_ptr<ImageBuf> newsrc (new ImageBuf (src->spec ()));
1347
1366
ImageBufAlgo::channels (*newsrc, *src, 1 , NULL , NULL , NULL , true );
1348
1367
newsrc->specmod ().default_channel_names ();
@@ -1354,7 +1373,7 @@ make_texture_impl(ImageBufAlgo::MakeTextureMode mode, const ImageBuf* input,
1354
1373
if ((nchannels > 0 ) && (nchannels != src->nchannels ())) {
1355
1374
if (verbose)
1356
1375
outstream << " Overriding number of channels to " << nchannels
1357
- << " \n " ;
1376
+ << std::endl ;
1358
1377
std::shared_ptr<ImageBuf> newsrc (new ImageBuf (src->spec ()));
1359
1378
ImageBufAlgo::channels (*newsrc, *src, nchannels, NULL , NULL , NULL ,
1360
1379
true );
@@ -1655,14 +1674,6 @@ make_texture_impl(ImageBufAlgo::MakeTextureMode mode, const ImageBuf* input,
1655
1674
STATUS (" color convert" , stat_colorconverttime);
1656
1675
}
1657
1676
1658
- // Force float for the sake of the ImageBuf math.
1659
- // Also force float if we do not allow for the pixel shift,
1660
- // since resize_block_ requires floating point buffers.
1661
- const int allow_shift = configspec.get_int_attribute (
1662
- " maketx:allow_pixel_shift" );
1663
- if (configspec.get_int_attribute (" maketx:forcefloat" , 1 ) || !allow_shift)
1664
- dstspec.set_format (TypeDesc::FLOAT);
1665
-
1666
1677
// Handle resize to power of two, if called for
1667
1678
if (configspec.get_int_attribute (" maketx:resize" ) && !shadowmode) {
1668
1679
dstspec.width = ceil2 (dstspec.width );
@@ -1684,6 +1695,15 @@ make_texture_impl(ImageBufAlgo::MakeTextureMode mode, const ImageBuf* input,
1684
1695
|| Strutil::iends_with (outputfilename, " .exr" )))
1685
1696
do_resize = true ;
1686
1697
1698
+ // Force float for the sake of the ImageBuf math.
1699
+ // Also force float if we do not allow for the pixel shift,
1700
+ // since resize_block_ requires floating point buffers.
1701
+ const int allow_shift = configspec.get_int_attribute (
1702
+ " maketx:allow_pixel_shift" );
1703
+ if (configspec.get_int_attribute (" maketx:forcefloat" , 1 )
1704
+ || (do_resize && !allow_shift))
1705
+ dstspec.set_format (TypeDesc::FLOAT);
1706
+
1687
1707
if (orig_was_overscan && out && !out->supports (" displaywindow" )) {
1688
1708
errorfmt (
1689
1709
" Format \" {}\" does not support separate display "
@@ -1705,13 +1725,16 @@ make_texture_impl(ImageBufAlgo::MakeTextureMode mode, const ImageBuf* input,
1705
1725
toplevel = src;
1706
1726
} else if (!do_resize) {
1707
1727
// Need format conversion, but no resize -- just copy the pixels
1728
+ if (verbose)
1729
+ print (outstream, " Copying for format conversion from {} to {}\n " ,
1730
+ src->spec ().format , dstspec.format );
1708
1731
toplevel.reset (new ImageBuf (dstspec));
1709
1732
toplevel->copy_pixels (*src);
1710
1733
} else {
1711
1734
// Resize
1712
1735
if (verbose)
1713
- outstream << " Resizing image to " << dstspec.width << " x "
1714
- << dstspec.height << std::endl ;
1736
+ print ( outstream, " Resizing image to {} x {} \n " , dstspec.width ,
1737
+ dstspec.height ) ;
1715
1738
string_view resize_filter (filtername);
1716
1739
if (Strutil::istarts_with (resize_filter, " unsharp-" ))
1717
1740
resize_filter = " lanczos3" ;
@@ -1888,28 +1911,22 @@ make_texture_impl(ImageBufAlgo::MakeTextureMode mode, const ImageBuf* input,
1888
1911
if (verbose || configspec.get_int_attribute (" maketx:runstats" )
1889
1912
|| configspec.get_int_attribute (" maketx:stats" )) {
1890
1913
double all = alltime ();
1891
- Strutil::fprintf (outstream, " maketx run time (seconds): %5.2f\n " , all);
1892
- Strutil::fprintf (outstream, " file read: %5.2f\n " ,
1893
- stat_readtime);
1894
- Strutil::fprintf (outstream, " file write: %5.2f\n " ,
1895
- stat_writetime);
1896
- Strutil::fprintf (outstream, " initial resize: %5.2f\n " ,
1897
- stat_resizetime);
1898
- Strutil::fprintf (outstream, " hash: %5.2f\n " ,
1899
- stat_hashtime);
1900
- Strutil::fprintf (outstream, " pixelstats: %5.2f\n " ,
1901
- stat_pixelstatstime);
1902
- Strutil::fprintf (outstream, " mip computation: %5.2f\n " , stat_miptime);
1903
- Strutil::fprintf (outstream, " color convert: %5.2f\n " ,
1904
- stat_colorconverttime);
1905
- Strutil::fprintf (
1914
+ print (outstream, " maketx run time (seconds): {:5.2f}\n " , all);
1915
+ print (outstream, " file read: {:5.2f}\n " , stat_readtime);
1916
+ print (outstream, " file write: {:5.2f}\n " , stat_writetime);
1917
+ print (outstream, " initial resize: {:5.2f}\n " , stat_resizetime);
1918
+ print (outstream, " hash: {:5.2f}\n " , stat_hashtime);
1919
+ print (outstream, " pixelstats: {:5.2f}\n " , stat_pixelstatstime);
1920
+ print (outstream, " mip computation: {:5.2f}\n " , stat_miptime);
1921
+ print (outstream, " color convert: {:5.2f}\n " , stat_colorconverttime);
1922
+ print (
1906
1923
outstream,
1907
- " unaccounted: % 5.2f (% 5.2f % 5.2f % 5.2f % 5.2f % 5.2f)\n " ,
1924
+ " unaccounted: {: 5.2f} ({: 5.2f} {: 5.2f} {: 5.2f} {: 5.2f} {: 5.2f} )\n " ,
1908
1925
all - stat_readtime - stat_writetime - stat_resizetime
1909
1926
- stat_hashtime - stat_miptime,
1910
1927
misc_time_1, misc_time_2, misc_time_3, misc_time_4, misc_time_5);
1911
- Strutil::fprintf (outstream, " maketx peak memory used: %s \n " ,
1912
- Strutil::memformat (peak_mem));
1928
+ print (outstream, " maketx peak memory used: {} \n " ,
1929
+ Strutil::memformat (peak_mem));
1913
1930
}
1914
1931
1915
1932
#undef STATUS
0 commit comments