Image sequence listing ...

M

majestik666

Am not sure if it's been asked before (did a search but didn't come up
with anything
related...).
I'm trying to write some code to "collapse" image sequences into
a single item , i.e.:
image.1.exr
image.2.exr
....
image.100.exr

into
image.[1-100].exr

This is for display only so i do not show up 100 files but only one
as some file managers do.

Anyone got any info on doing this quickly ?

I use Boost already in my app, so I guess the regex boost lib could
help
but not really sure of the most efficient way of doing it
 
V

Victor Bazarov

Am not sure if it's been asked before (did a search but didn't come up
with anything
related...).
I'm trying to write some code to "collapse" image sequences into
a single item , i.e.:
image.1.exr
image.2.exr
...
image.100.exr

into
image.[1-100].exr

This is for display only so i do not show up 100 files but only one
as some file managers do.

Anyone got any info on doing this quickly ?

What's your C++ language question? Are you trying to declare an array
to help you manage the sequence? You can also do it using 'std::map'
or 'std::vector', but I am unsure what to recommend since you didn't
show any code, and C++ doesn't have a definition of "image", nor does
it have a definition of "collapsing" of those.
I use Boost already in my app, so I guess the regex boost lib could
help
but not really sure of the most efficient way of doing it

V
 
M

majestik666

Am not sure if it's been asked before (did a search but didn't come up
with anything
related...).
I'm trying to write some code to "collapse" image sequences into
a single item , i.e.:
image.1.exr
image.2.exr
...
image.100.exr
into
image.[1-100].exr

This is for display only so i do not show up 100 files but only one
as some file managers do.
Anyone got any info on doing this quickly ?

What's your C++ language question? Are you trying to declare an array
to help you manage the sequence? You can also do it using 'std::map'
or 'std::vector', but I am unsure what to recommend since you didn't
show any code, and C++ doesn't have a definition of "image", nor does
it have a definition of "collapsing" of those.
I use Boost already in my app, so I guess the regex boost lib could
help
but not really sure of the most efficient way of doing it

V

Well I guess my c++ question is about
how do you list a folder, and while doing that
collapsing the filenames that are in the form prefix.%d.suffix
into "groups" like prefix.[first-last].suffix
it's not image related really, just about how to work out
the strings to collapes into one.
 
V

Victor Bazarov

[..]
Well I guess my c++ question is about
how do you list a folder, and while doing that
collapsing the filenames that are in the form prefix.%d.suffix
into "groups" like prefix.[first-last].suffix
it's not image related really, just about how to work out
the strings to collapes into one.

Ah... OK. Folders aside, files aside, your question is what you
should do to form "blah.[N-M].blah" if you have a bunch of strings
all containing "blah.<N>.blah", "blah.<N+1>.blah", etc., until the
"blah.<M>.blah" where N and M are two numbers. Right?

This is not a C++ language question. It's an algorithm question.
You need an algorithm for string processing. I would probably
suggest something like

// find the beginning of the sequence
int N;
for (N = 0; N < INT_MAX; ++N) {
std::eek:stringstream os;
os << "prefix." << N << ".suffix";
std::string filename(os.str());
if (/* exists the file with pattern 'filename' */)
break;
}
// now N is your start
int M;
for (int M = N; M<INT_MAX; ++M) {
std::eek:stringstream os;
os << "prefix." << M << ".suffix";
std::string filenam(os.str());
if (/* does NOT exist file with pattern 'filename' */)
break;
}
if (--M > N) { // got the sequence!
std::eek:stringstream os;
os << "prefix.[" << N << '-' << M << "].suffix";
return os.str(); // here you go
}
else { // it's a single file, maybe.
...
}

V
 
M

majestik666

[..]
Well I guess my c++ question is about
how do you list a folder, and while doing that
collapsing the filenames that are in the form prefix.%d.suffix
into "groups" like prefix.[first-last].suffix
it's not image related really, just about how to work out
the strings to collapes into one.

Ah... OK. Folders aside, files aside, your question is what you
should do to form "blah.[N-M].blah" if you have a bunch of strings
all containing "blah.<N>.blah", "blah.<N+1>.blah", etc., until the
"blah.<M>.blah" where N and M are two numbers. Right?

This is not a C++ language question. It's an algorithm question.
You need an algorithm for string processing. I would probably
suggest something like

// find the beginning of the sequence
int N;
for (N = 0; N < INT_MAX; ++N) {
std::eek:stringstream os;
os << "prefix." << N << ".suffix";
std::string filename(os.str());
if (/* exists the file with pattern 'filename' */)
break;
}
// now N is your start
int M;
for (int M = N; M<INT_MAX; ++M) {
std::eek:stringstream os;
os << "prefix." << M << ".suffix";
std::string filenam(os.str());
if (/* does NOT exist file with pattern 'filename' */)
break;
}
if (--M > N) { // got the sequence!
std::eek:stringstream os;
os << "prefix.[" << N << '-' << M << "].suffix";
return os.str(); // here you go
}
else { // it's a single file, maybe.
...
}

V

well yes it's the problem, but the initial conditions are a bit
different
let's say i have a list of strings in the form you mentioned
prefix.<N>.suffix, I can have different prefixes etc.
but I guess I cannot avoid going through the files one by one
and looking if i can join it with other files in the same folder.
 
V

Victor Bazarov

[..]
Well I guess my c++ question is about
how do you list a folder, and while doing that
collapsing the filenames that are in the form prefix.%d.suffix
into "groups" like prefix.[first-last].suffix
it's not image related really, just about how to work out
the strings to collapes into one.

Ah... OK. Folders aside, files aside, your question is what you
should do to form "blah.[N-M].blah" if you have a bunch of strings
all containing "blah.<N>.blah", "blah.<N+1>.blah", etc., until the
"blah.<M>.blah" where N and M are two numbers. Right?

This is not a C++ language question. It's an algorithm question.
You need an algorithm for string processing. I would probably
suggest something like

// find the beginning of the sequence
int N;
for (N = 0; N < INT_MAX; ++N) {
std::eek:stringstream os;
os << "prefix." << N << ".suffix";
std::string filename(os.str());
if (/* exists the file with pattern 'filename' */)
break;
}
// now N is your start
int M;
for (int M = N; M<INT_MAX; ++M) {
std::eek:stringstream os;
os << "prefix." << M << ".suffix";
std::string filenam(os.str());
if (/* does NOT exist file with pattern 'filename' */)
break;
}
if (--M > N) { // got the sequence!
std::eek:stringstream os;
os << "prefix.[" << N << '-' << M << "].suffix";
return os.str(); // here you go
}
else { // it's a single file, maybe.
...
}

V

well yes it's the problem, but the initial conditions are a bit
different
let's say i have a list of strings in the form you mentioned
prefix.<N>.suffix, I can have different prefixes etc.
but I guess I cannot avoid going through the files one by one
and looking if i can join it with other files in the same folder.

It's all in how you check for the existence of the file. If your
system allows enumerating with a pattern (like "*.1001.img"), then
you're in luck and you need to put the asterisk instead of 'prefix'.

The algorithm as I wrote it is also substandard from the performance
point of view. It would be nice if you could simply get all the
strings (names of the files) fitting the pattern "*.[0-9]+.suffix",
and sort them while disregarding the prefix (you need a custom
comparison functor for that). Then you simply have an array of
strings, and the very first one contains your N, which you can
simply extract from the string itself. And you can simply keep
extracting the number from each string and compare it to what you
expect (previous + 1). If it isn't, your sequence is over, and
you may need to start another sequence...

Anyway, this doesn't seem to be a C++ topic either. Try posting to
comp.programming, the general algorithm discussion newsgroup.

V
 
J

James Kanze

(e-mail address removed) wrote:
[..]
Well I guess my c++ question is about
how do you list a folder, and while doing that
collapsing the filenames that are in the form prefix.%d.suffix
into "groups" like prefix.[first-last].suffix
it's not image related really, just about how to work out
the strings to collapes into one.
Ah... OK. Folders aside, files aside, your question is what you
should do to form "blah.[N-M].blah" if you have a bunch of strings
all containing "blah.<N>.blah", "blah.<N+1>.blah", etc., until the
"blah.<M>.blah" where N and M are two numbers. Right?
This is not a C++ language question. It's an algorithm question.
You need an algorithm for string processing. I would probably
suggest something like
// find the beginning of the sequence
int N;
for (N = 0; N < INT_MAX; ++N) {
std::eek:stringstream os;
os << "prefix." << N << ".suffix";
std::string filename(os.str());
if (/* exists the file with pattern 'filename' */)
break;
}
// now N is your start
int M;
for (int M = N; M<INT_MAX; ++M) {
std::eek:stringstream os;
os << "prefix." << M << ".suffix";
std::string filenam(os.str());
if (/* does NOT exist file with pattern 'filename' */)
break;
}
if (--M > N) { // got the sequence!
std::eek:stringstream os;
os << "prefix.[" << N << '-' << M << "].suffix";
return os.str(); // here you go
}
else { // it's a single file, maybe.
...
}
well yes it's the problem, but the initial conditions are a
bit different let's say i have a list of strings in the form
you mentioned prefix.<N>.suffix, I can have different
prefixes etc. but I guess I cannot avoid going through the
files one by one and looking if i can join it with other
files in the same folder.
It's all in how you check for the existence of the file. If
your system allows enumerating with a pattern (like
"*.1001.img"), then you're in luck and you need to put the
asterisk instead of 'prefix'.
The algorithm as I wrote it is also substandard from the
performance point of view.

Two quick comments: boost::regex would be useful for determining
patterns. Note too that if the strings are sorted (which he
probably wants to do anyway, if they are in fact filenames), all
of the candidates for a match will be adjacent. Taking
advantage of this pre-condition, something like the following
should work:

int
asNumber(
std::string const& str )
{
std::istringstream s( str ) ;
int i ;
s >> i >> std::ws ;
return s && s.get() == EOF && i >= 0
? i
: -1 ;
}

template< typename FwdIter, typename OutIter >
void
collapse(
FwdIter begin,
FwdIter end,
OutIter dest )
{
static boost::regex const
matcher(
"^\\(.*\\)\\.\\([1-9]\\d*\\)\\.\\(.*\\)$" ) ;
boost::smatch results ;
while ( begin != end ) {
if ( ! boost::regex_match( *begin, results, matcher ) ) {
*dest = *begin ;
++ dest ;
++ begin ;
} else {
std::string prefix = results[ 1 ] ;
int start =
asNumber( results[ 2 ] ) ;
std::string suffix = results[ 3 ] ;
int expect = start + 1 ;
++ begin ;
while ( boost::regex_match( *begin, results, matcher)
&& results[ 1 ] == prefix
&& asNumber( results[ 2 ] ) == expect
&& results[ 3 ] == suffix ) {
++ begin ;
++ expect ;
}
std::eek:stringstream s ;
s << prefix << ".[" << start << '-'
<< expect << "]." << suffix ;
*dest = s.str() ;
++ dest ;
}
}
}

(Note that since I haven't got Boost installed on the machine
here, I can't test it. But the general idea should work.)

As for performance, just about anything he does will be hardly
noticeable compared to the time it takes to read the directory
into memory to begin with.
It would be nice if you could simply get all the
strings (names of the files) fitting the pattern "*.[0-9]+.suffix",
and sort them while disregarding the prefix (you need a custom
comparison functor for that).

This should be possible; at least, it's possible with my
implementation of File. But reading the directory several times
is probably not the best solution. Read everything into memory
once, and work there. (Where your suggestion here is nothing
more than copy_if with a regular expression based predicate.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top