Skip to content

Commit

Permalink
chore: more convertion to new string formatting (#4189)
Browse files Browse the repository at this point in the history
In this PR, we convert nearly all `sprintf` uses to `format`.

It should not change any behavior or compatibility. Nonetheless, I think
this is destined for master only and won't be backported, to reduce
risk.

Along the way, we add format_to and format_to_n to Strutil.

---------

Signed-off-by: Larry Gritz <[email protected]>
  • Loading branch information
lgritz committed Mar 24, 2024
1 parent e705c5d commit 77d2a53
Show file tree
Hide file tree
Showing 44 changed files with 370 additions and 353 deletions.
12 changes: 6 additions & 6 deletions src/cineon.imageio/libcineon/CineonHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,12 +512,12 @@ void cineon::IndustryHeader::FilmEdgeCode(char *edge, size_t size) const
&& this->count == 0xffffffff)
*edge = 0;
else {
std::string e = OIIO::Strutil::sprintf(
"%02u%02u%02u%06u%04u",
(unsigned int)this->filmManufacturingIdCode,
(unsigned int)this->filmType,
(unsigned int)this->perfsOffset, this->prefix, this->count);
OIIO::Strutil::safe_strcpy(edge, e, size);
std::string e = OIIO::Strutil::fmt::format(
"{:02}{:02}{:02}{:06}{:04}",
(unsigned int)this->filmManufacturingIdCode,
(unsigned int)this->filmType, (unsigned int)this->perfsOffset,
this->prefix, this->count);
OIIO::Strutil::safe_strcpy(edge, e, size);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/dicom.imageio/dicominput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ DICOMInput::read_metadata()
std::string tagname = tag.getTagName();
if (ignore_tags.find(tagname) != ignore_tags.end())
continue;
std::string name = Strutil::sprintf("dicom:%s", tag.getTagName());
std::string name = Strutil::fmt::format("dicom:{}",
tag.getTagName());
DcmEVR evr = tag.getEVR();
// VR codes explained:
// http://dicom.nema.org/Dicom/2013/output/chtml/part05/sect_6.2.html
Expand Down
7 changes: 4 additions & 3 deletions src/dpx.imageio/dpxinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ DPXInput::seek_subimage(int subimage, int miplevel)
default: {
for (int i = 0; i < m_dpx.header.ImageElementComponentCount(subimage);
i++) {
std::string ch = Strutil::sprintf("channel%d", i);
std::string ch = Strutil::fmt::format("channel{}", i);
m_spec.channelnames.push_back(ch);
}
}
Expand Down Expand Up @@ -319,7 +319,7 @@ DPXInput::seek_subimage(int subimage, int miplevel)
if (!std::isnan(m_dpx.header.Gamma()) && m_dpx.header.Gamma() != 0) {
float g = float(m_dpx.header.Gamma());
m_spec.attribute("oiio:ColorSpace",
Strutil::sprintf("Gamma%.2g", g));
Strutil::fmt::format("Gamma{:.2}", g));
m_spec.attribute("oiio:Gamma", g);
break;
}
Expand Down Expand Up @@ -537,7 +537,8 @@ DPXInput::seek_subimage(int subimage, int miplevel)
// don't set the attribute at all
break;
default:
tmpstr = Strutil::sprintf("Undefined %d", (int)m_dpx.header.Signal());
tmpstr = Strutil::fmt::format("Undefined {}",
(int)m_dpx.header.Signal());
break;
}
if (!tmpstr.empty())
Expand Down
31 changes: 17 additions & 14 deletions src/dpx.imageio/libdpx/DPXHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <ctime>
#include <limits>

#include <OpenImageIO/strutil.h>
#include <OpenImageIO/sysutil.h>

#include "DPXHeader.h"
Expand Down Expand Up @@ -685,25 +686,27 @@ void dpx::IndustryHeader::SetFileEdgeCode(const char *edge)
}


void dpx::IndustryHeader::TimeCode(char *str) const
void dpx::IndustryHeader::TimeCode(char* str) const
{
U32 tc = this->timeCode;
::snprintf(str, 12, "%c%c:%c%c:%c%c:%c%c",
Hex((tc & 0xf0000000) >> 28), Hex((tc & 0xf000000) >> 24),
Hex((tc & 0xf00000) >> 20), Hex((tc & 0xf0000) >> 16),
Hex((tc & 0xf000) >> 12), Hex((tc & 0xf00) >> 8),
Hex((tc & 0xf0) >> 4), Hex(tc & 0xf));
using OIIO::Strutil::format_to_n;
U32 tc = this->timeCode;
format_to_n(str, 12, "{:c}{:c}:{:c}{:c}:{:c}{:c}:{:c}{:c}",
Hex((tc & 0xf0000000) >> 28), Hex((tc & 0xf000000) >> 24),
Hex((tc & 0xf00000) >> 20), Hex((tc & 0xf0000) >> 16),
Hex((tc & 0xf000) >> 12), Hex((tc & 0xf00) >> 8),
Hex((tc & 0xf0) >> 4), Hex(tc & 0xf));
}


void dpx::IndustryHeader::UserBits(char *str) const
void dpx::IndustryHeader::UserBits(char* str) const
{
U32 ub = this->userBits;
::snprintf(str, 12, "%c%c:%c%c:%c%c:%c%c",
Hex((ub & 0xf0000000) >> 28), Hex((ub & 0xf000000) >> 24),
Hex((ub & 0xf00000) >> 20), Hex((ub & 0xf0000) >> 16),
Hex((ub & 0xf000) >> 12), Hex((ub & 0xf00) >> 8),
Hex((ub & 0xf0) >> 4), Hex(ub & 0xf));
using OIIO::Strutil::format_to_n;
U32 ub = this->userBits;
format_to_n(str, 12, "{:c}{:c}:{:c}{:c}:{:c}{:c}:{:c}{:c}",
Hex((ub & 0xf0000000) >> 28), Hex((ub & 0xf000000) >> 24),
Hex((ub & 0xf00000) >> 20), Hex((ub & 0xf0000) >> 16),
Hex((ub & 0xf000) >> 12), Hex((ub & 0xf00) >> 8),
Hex((ub & 0xf0) >> 4), Hex(ub & 0xf));
}


Expand Down
14 changes: 8 additions & 6 deletions src/fits.imageio/fitsinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,18 +414,20 @@ FitsInput::convert_date(const std::string& date)
std::string ndate;
if (date[4] == '-') {
// YYYY-MM-DDThh:mm:ss convention is used since 1 January 2000
ndate = Strutil::sprintf("%04u:%02u:%02u", stoi(&date[0]),
stoi(&date[5]), stoi(&date[8]));
ndate = Strutil::fmt::format("{:04d}:{:02d}:{:02d}", stoi(&date[0]),
stoi(&date[5]), stoi(&date[8]));
if (date.size() >= 11 && date[10] == 'T')
ndate += Strutil::sprintf(" %02u:%02u:%02u", stoi(&date[11]),
stoi(&date[14]), stoi(&date[17]));
ndate += Strutil::fmt::format(" {:02d}:{:02d}:{:02d}",
stoi(&date[11]), stoi(&date[14]),
stoi(&date[17]));
return ndate;
}

if (date[2] == '/') {
// DD/MM/YY convention was used before 1 January 2000
ndate = Strutil::sprintf("19%02u:%02u:%02u 00:00:00", stoi(&date[6]),
stoi(&date[3]), stoi(&date[0]));
ndate = Strutil::fmt::format("19{:02d}:{:02d}:{:02d} 00:00:00",
stoi(&date[6]), stoi(&date[3]),
stoi(&date[0]));
return ndate;
}
// unrecognized format
Expand Down
8 changes: 4 additions & 4 deletions src/fits.imageio/fitsoutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ FitsOutput::create_fits_header(void)
if (keyname == "DateTime") {
using Strutil::stoi;
keyname = "Date";
value = Strutil::sprintf("%04u-%02u-%02uT%02u:%02u:%02u",
stoi(&value[0]), stoi(&value[5]),
stoi(&value[8]), stoi(&value[11]),
stoi(&value[14]), stoi(&value[17]));
value = Strutil::fmt::format("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}",
stoi(&value[0]), stoi(&value[5]),
stoi(&value[8]), stoi(&value[11]),
stoi(&value[14]), stoi(&value[17]));
}

header += create_card(keyname, value);
Expand Down
2 changes: 1 addition & 1 deletion src/hdr.imageio/hdrinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ HdrInput::RGBE_ReadHeader()
m_spec.attribute("oiio:ColorSpace", "linear");
else
m_spec.attribute("oiio:ColorSpace",
Strutil::sprintf("Gamma%.2g", g));
Strutil::fmt::format("Gamma{:.2g}", g));

} else if (Strutil::parse_values(line,
"EXPOSURE=", span<float>(tempf))) {
Expand Down
4 changes: 4 additions & 0 deletions src/include/OpenImageIO/strutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ using old::format;
#endif


// format_to comes from fmt library
using ::fmt::format_to;
using ::fmt::format_to_n;


/// Strutil::printf (fmt, ...)
/// Strutil::fprintf (FILE*, fmt, ...)
Expand Down
43 changes: 22 additions & 21 deletions src/iv/imageviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1085,54 +1085,55 @@ ImageViewer::updateStatusBar()
return;
}
std::string message;
message = Strutil::sprintf("(%d/%d) : ", m_current_image + 1,
(int)m_images.size());
message = Strutil::fmt::format("({}/{}) : ", m_current_image + 1,
(int)m_images.size());
message += cur()->shortinfo();
statusImgInfo->setText(message.c_str());

message.clear();
switch (m_color_mode) {
case RGBA:
message = Strutil::sprintf("RGBA (%d-%d)", m_current_channel,
m_current_channel + 3);
message = Strutil::fmt::format("RGBA ({}-{})", m_current_channel,
m_current_channel + 3);
break;
case RGB:
message = Strutil::sprintf("RGB (%d-%d)", m_current_channel,
m_current_channel + 2);
message = Strutil::fmt::format("RGB ({}-{})", m_current_channel,
m_current_channel + 2);
break;
case LUMINANCE:
message = Strutil::sprintf("Lum (%d-%d)", m_current_channel,
m_current_channel + 2);
message = Strutil::fmt::format("Lum ({}-{})", m_current_channel,
m_current_channel + 2);
break;
case HEATMAP: message = "Heat ";
case SINGLE_CHANNEL:
if ((int)spec->channelnames.size() > m_current_channel
&& spec->channelnames[m_current_channel].size())
message += spec->channelnames[m_current_channel];
else if (m_color_mode == HEATMAP) {
message += Strutil::sprintf("%d", m_current_channel);
message += Strutil::fmt::format("{}", m_current_channel);
} else {
message = Strutil::sprintf("chan %d", m_current_channel);
message = Strutil::fmt::format("chan {}", m_current_channel);
}
break;
}
message += Strutil::sprintf(" %g:%g exp %+.1f gam %.2f",
zoom() >= 1 ? zoom() : 1.0f,
zoom() >= 1 ? 1.0f : 1.0f / zoom(),
cur()->exposure(), cur()->gamma());
message += Strutil::fmt::format(" {}:{} exp {:+.1f} gam {:.2f}",
zoom() >= 1 ? zoom() : 1.0f,
zoom() >= 1 ? 1.0f : 1.0f / zoom(),
cur()->exposure(), cur()->gamma());
if (cur()->nsubimages() > 1) {
if (cur()->auto_subimage()) {
message += Strutil::sprintf(" subimg AUTO (%d/%d)",
cur()->subimage() + 1,
cur()->nsubimages());
message += Strutil::fmt::format(" subimg AUTO ({}/{})",
cur()->subimage() + 1,
cur()->nsubimages());
} else {
message += Strutil::sprintf(" subimg %d/%d", cur()->subimage() + 1,
cur()->nsubimages());
message += Strutil::fmt::format(" subimg {}/{}",
cur()->subimage() + 1,
cur()->nsubimages());
}
}
if (cur()->nmiplevels() > 1) {
message += Strutil::sprintf(" MIP %d/%d", cur()->miplevel() + 1,
cur()->nmiplevels());
message += Strutil::fmt::format(" MIP {}/{}", cur()->miplevel() + 1,
cur()->nmiplevels());
}

statusViewInfo->setText(message.c_str()); // tr("iv status"));
Expand Down
20 changes: 10 additions & 10 deletions src/iv/ivgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -866,8 +866,8 @@ IvGL::paint_pixelview()
texty = closeupsize + yspacing;
}
}
std::string s = Strutil::sprintf("(%d, %d)", (int)real_xp + spec.x,
(int)real_yp + spec.y);
std::string s = Strutil::fmt::format("({}, {})", (int)real_xp + spec.x,
(int)real_yp + spec.y);
shadowed_text(textx, texty, 0.0f, s, font);
texty += yspacing;
img->getpixel((int)real_xp + spec.x, (int)real_yp + spec.y, fpixel);
Expand All @@ -876,20 +876,20 @@ IvGL::paint_pixelview()
case TypeDesc::UINT8: {
ImageBuf::ConstIterator<unsigned char, unsigned char> p(
*img, (int)real_xp + spec.x, (int)real_yp + spec.y);
s = Strutil::sprintf("%s: %3d (%5.3f)",
spec.channelnames[i].c_str(), (int)(p[i]),
fpixel[i]);
s = Strutil::fmt::format("{}: {:3} ({:5.3f})",
spec.channelnames[i], int(p[i]),
fpixel[i]);
} break;
case TypeDesc::UINT16: {
ImageBuf::ConstIterator<unsigned short, unsigned short> p(
*img, (int)real_xp + spec.x, (int)real_yp + spec.y);
s = Strutil::sprintf("%s: %3d (%5.3f)",
spec.channelnames[i].c_str(), (int)(p[i]),
fpixel[i]);
s = Strutil::fmt::format("{}: {:3} ({:5.3f})",
spec.channelnames[i], int(p[i]),
fpixel[i]);
} break;
default: // everything else, treat as float
s = Strutil::sprintf("%s: %5.3f", spec.channelnames[i].c_str(),
fpixel[i]);
s = Strutil::fmt::format("{}: {:5.3f}", spec.channelnames[i],
fpixel[i]);
}
shadowed_text(textx, texty, 0.0f, s, font);
texty += yspacing;
Expand Down
Loading

0 comments on commit 77d2a53

Please sign in to comment.