@@ -133,8 +133,8 @@ void ImageData::readMetadataFromCicp(const ColorProfile::CICP& cicp) {
133133 hdrMetadata.bestGuessWhiteLevel = ituth273::bestGuessReferenceWhiteLevel (cicp.transfer );
134134}
135135
136- vector<string > ImageData::channelsInLayer (string_view layerName) const {
137- vector<string > result;
136+ vector<string_view > ImageData::channelsInLayer (string_view layerName) const {
137+ vector<string_view > result;
138138
139139 for (const auto & c : channels) {
140140 // If the layer name starts at the beginning, and if no other dot is found after the end of the layer name, then we have found a
@@ -834,29 +834,33 @@ Texture* Image::texture(span<const string> channelNames, EInterpolationMode minF
834834 default : throw runtime_error{" Unsupported number of channels for texture." };
835835 }
836836
837- Texture::ComponentFormat componentFormat = Texture::ComponentFormat::Float16;
838- for (const auto & chanName : channelNames) {
839- const Channel* chan = channel (chanName);
840- if (chan && chan->desiredPixelFormat () == EPixelFormat::F32) {
841- componentFormat = Texture::ComponentFormat::Float32;
842- break ; // No need to check further, we already have a channel that requires F32.
843- }
844- }
837+ const Texture::ComponentFormat componentFormat = ranges::any_of (
838+ channelNames | views::transform ([this ](const auto & c) { return channel (c); }),
839+ [](const auto & c) { return c && c->desiredPixelFormat () == EPixelFormat::F32; }
840+ ) ?
841+ Texture::ComponentFormat::Float32 :
842+ Texture::ComponentFormat::Float16;
845843
846844 mTextures .emplace (
847- lookup,
848- ImageTexture{
845+ piecewise_construct,
846+ tuple{
847+ lookup
848+ },
849+ tuple{
849850 new Texture{
850- pixelFormat, componentFormat,
851- {size ().x (), size ().y ()},
852- toNanogui (minFilter),
853- toNanogui (magFilter),
854- Texture::WrapMode::ClampToEdge,
855- 1 , Texture::TextureFlags::ShaderRead,
856- true , },
857- {channelNames.begin (), channelNames.end ()},
851+ pixelFormat,
852+ componentFormat,
853+ {size ().x (), size ().y ()},
854+ toNanogui (minFilter),
855+ toNanogui (magFilter),
856+ Texture::WrapMode::ClampToEdge,
857+ 1 ,
858+ Texture::TextureFlags::ShaderRead,
859+ true ,
860+ },
861+ channelNames | toVector,
858862 false ,
859- }
863+ }
860864 );
861865
862866 auto & texture = mTextures .at (lookup).nanoguiTexture ;
@@ -973,7 +977,7 @@ void Image::ungroup(string_view groupName) {
973977}
974978
975979vector<ChannelGroup> Image::getGroupedChannels (string_view layerName) const {
976- vector<vector<string>> groups = {
980+ static const vector<vector<string>> groups = {
977981 {" R" , " G" , " B" },
978982 {" r" , " g" , " b" },
979983 {" X" , " Y" , " Z" },
@@ -984,40 +988,37 @@ vector<ChannelGroup> Image::getGroupedChannels(string_view layerName) const {
984988 {" z" },
985989 };
986990
987- static constexpr auto createChannelGroup = [](string_view layer, vector<string > channels) {
991+ static constexpr auto createChannelGroup = [](string_view layer, span< const string_view > channels) {
988992 TEV_ASSERT (!channels.empty (), " Can't create a channel group without channels." );
989993
990- vector<string_view> channelTails = { channels. begin (), channels. end ()} ;
994+ vector<string_view> channelTails = channels | views::transform (Channel::tail) | toVector ;
991995 channelTails.erase (unique (begin (channelTails), end (channelTails)), end (channelTails));
992- transform (begin (channelTails), end (channelTails), begin (channelTails), Channel::tail);
993996
994997 const string channelsString = join (channelTails, " ," );
995998 const string name = layer.empty () ?
996999 channelsString :
9971000 (channelTails.size () == 1 ? fmt::format (" {}{}" , layer, channelsString) : fmt::format (" {}({})" , layer, channelsString));
9981001
999- return ChannelGroup{name, std::move (channels) };
1002+ return ChannelGroup{name, channels | views::transform ([]( const auto & c) { return string{c}; }) | toVector };
10001003 };
10011004
1002- string alphaChannelName = fmt::format (" {}A" , layerName);
1005+ const string alphaChannelName = fmt::format (" {}A" , layerName);
10031006
1004- vector<string > allChannels = mData .channelsInLayer (layerName);
1007+ vector<string_view > allChannels = mData .channelsInLayer (layerName);
10051008
1006- auto alphaIt = find (begin ( allChannels), end (allChannels) , alphaChannelName);
1007- bool hasAlpha = alphaIt != end (allChannels);
1009+ const auto alphaIt = ranges:: find (allChannels, alphaChannelName);
1010+ const bool hasAlpha = alphaIt != end (allChannels);
10081011 if (hasAlpha) {
10091012 allChannels.erase (alphaIt);
10101013 }
10111014
10121015 vector<ChannelGroup> result;
10131016
10141017 for (const auto & group : groups) {
1015- vector<string > groupChannels;
1018+ vector<string_view > groupChannels;
10161019 for (string_view channel : group) {
1017- string name = fmt::format (" {}{}" , layerName, channel);
1018- auto it = find (begin (allChannels), end (allChannels), name);
1019- if (it != end (allChannels)) {
1020- groupChannels.emplace_back (name);
1020+ if (const auto it = ranges::find (allChannels, fmt::format (" {}{}" , layerName, channel)); it != end (allChannels)) {
1021+ groupChannels.emplace_back (*it);
10211022 allChannels.erase (it);
10221023 }
10231024 }
@@ -1027,20 +1028,20 @@ vector<ChannelGroup> Image::getGroupedChannels(string_view layerName) const {
10271028 groupChannels.emplace_back (alphaChannelName);
10281029 }
10291030
1030- result.emplace_back (createChannelGroup (layerName, std::move ( groupChannels) ));
1031+ result.emplace_back (createChannelGroup (layerName, groupChannels));
10311032 }
10321033 }
10331034
1034- for (const auto & name : allChannels) {
1035+ for (auto & name : allChannels) {
10351036 if (hasAlpha) {
1036- result.emplace_back (createChannelGroup (layerName, vector<string >{name, alphaChannelName}));
1037+ result.emplace_back (createChannelGroup (layerName, array<string_view, 2 >{name, alphaChannelName}));
10371038 } else {
1038- result.emplace_back (createChannelGroup (layerName, vector<string>{ name}));
1039+ result.emplace_back (createChannelGroup (layerName, span{& name, 1 }));
10391040 }
10401041 }
10411042
10421043 if (hasAlpha && result.empty ()) {
1043- result.emplace_back (createChannelGroup (layerName, vector<string >{alphaChannelName}));
1044+ result.emplace_back (createChannelGroup (layerName, array<string_view, 1 >{alphaChannelName}));
10441045 }
10451046
10461047 TEV_ASSERT (!result.empty (), " Images with no channels should never exist." );
@@ -1182,7 +1183,7 @@ Task<HeapArray<float>> Image::getRgbaHdrImageData(
11821183 const Channel* alphaChannel = nullptr ;
11831184
11841185 // Only treat the alpha channel specially if it is not the only channel of the image.
1185- if (! all_of ( begin ( channels), end (channels), [](const Channel& c) { return c.isAlpha (); })) {
1186+ if (ranges::any_of ( channels, [](const Channel& c) { return ! c.isAlpha (); })) {
11861187 if (channels.back ().isAlpha ()) {
11871188 alphaChannel = &channels.back ();
11881189 }
@@ -1376,21 +1377,21 @@ string Image::toString() const {
13761377
13771378 sstream << " \n Channels:\n " ;
13781379
1379- auto localLayers = mData .layers ;
1380- transform (begin (localLayers), end (localLayers), begin (localLayers), [this ](string layer) {
1381- auto channels = mData .channelsInLayer (layer);
1382- transform (begin (channels), end (channels), begin (channels), [](string channel) { return Channel::tail (channel); });
1380+ sstream << join (
1381+ mData .layers | views::transform ([this ](string_view layer) {
1382+ const auto channels = mData .channelsInLayer (layer) | views::transform ([](string_view c) { return Channel::tail (c); });
13831383
1384- if (layer.empty ()) {
1385- return join (channels, " ," );
1386- } else if (channels.size () == 1 ) {
1387- return layer + channels.front ();
1388- } else {
1389- return layer + " (" s + join (channels, " ," ) + " )" s;
1390- }
1391- });
1384+ if (layer.empty ()) {
1385+ return join (channels, " ," );
1386+ } else if (channels.size () == 1 ) {
1387+ return fmt::format (" {}{}" , layer, channels.front ());
1388+ } else {
1389+ return fmt::format (" {}({})" , layer, join (channels, " ," ));
1390+ }
1391+ }),
1392+ " \n "
1393+ );
13921394
1393- sstream << join (localLayers, " \n " );
13941395 return std::move (sstream).str ();
13951396}
13961397
0 commit comments