forked from TimSC/sense-scanner
/
imagesequence.cpp
154 lines (137 loc) · 4.6 KB
/
imagesequence.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "imagesequence.h"
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QObject>
#include <iostream>
#include <assert.h>
using namespace std;
QString StripLeftAlphaChars(QString in)
{
//Determine end of alphanumberic chars
int i;
for(i=0;i<in.length();i++)
{
QString c = in.mid(i,1);
const char *cd = c.toLocal8Bit().constData();
int check = (cd[0] >= '0' && cd[0] <= '9');
if(check) break;
}
return in.mid(i);
}
QString GetLeftAlphaChars(QString in)
{
//Determine end of alphanumberic chars
int i;
for(i=0;i<in.length();i++)
{
QString c = in.mid(i,1);
const char *cd = c.toLocal8Bit().constData();
int check = (cd[0] >= '0' && cd[0] <= '9');
if(check) break;
}
return in.left(i);
}
ImageSequence::ImageSequence(QString targetDir, float frameRate) : AbstractMedia()
{
QDir directory(targetDir);
QStringList dirFiles = directory.entryList();
this->targetDir = targetDir;
this->maxIndex = 0;
this->minIndex = 0;
this->numPackedChars = 0;
this->maxPrefix = "";
this->maxExt = "";
this->frameRate = frameRate;
int packedChars = 0;
//Get highest value image
for (int i = 0; i < dirFiles.size(); ++i)
{
QString fina = dirFiles.at(i);
QFileInfo finai = fina;
QString baseName = finai.baseName(); //filename without extension
QString extName = finai.completeSuffix();
//cout << fina.toLocal8Bit().constData() <<"," << baseName.toLocal8Bit().constData() << endl;
QString stripName = StripLeftAlphaChars(baseName);
packedChars = stripName.length();
long long unsigned ind = stripName.toInt();
QString prefix = GetLeftAlphaChars(baseName);
if(ind > maxIndex)
{
this->maxIndex = ind;
this->maxPrefix = prefix;
this->maxExt = extName;
}
}
//Get lowest value file
//TODO this is a much better place to determine if the file name is packed
for(long long unsigned i=0;i<=this->maxIndex;i++)
{
//Test for packed name
QString formatStr;
formatStr.sprintf("%%s/%%s%%0%illd.%%s", packedChars);
QString fina;
fina.sprintf(formatStr.toLocal8Bit().constData(),
this->targetDir.toLocal8Bit().constData(),
this->maxPrefix.toLocal8Bit().constData(), i,
this->maxExt.toLocal8Bit().constData());
QFile file(fina);
if(file.exists())
{
this->minIndex = i;
this->numPackedChars = packedChars;
break;
}
//Test for unpacked name
QString fina2;
fina2.sprintf("%s/%s%lld.%s", this->targetDir.toLocal8Bit().constData(),
this->maxPrefix.toLocal8Bit().constData(), i,
this->maxExt.toLocal8Bit().constData());
QFile file2(fina2);
if(file2.exists())
{
this->minIndex = i;
this->numPackedChars = 0;
break;
}
}
//cout << this->maxPrefix.toLocal8Bit().constData()
// << "\t" << this->maxIndex << "\t" << this->maxExt.toLocal8Bit().constData()
// << "\t" << this->maxPackedChars << endl;
}
ImageSequence::~ImageSequence()
{
}
QSharedPointer<QImage> ImageSequence::Get(long long unsigned ti,
long long unsigned &outFrameTi) //in milliseconds
{
long long unsigned frameNum = float(ti) * this->frameRate / 1000.;
outFrameTi = frameNum * 1000 / this->frameRate;
frameNum += this->minIndex;
if (frameNum < this->minIndex) frameNum = this->minIndex;
if (frameNum > this->maxIndex) frameNum = this->maxIndex;
cout << frameNum << endl;
QString fina;
fina.sprintf("%s/%s%03lld.%s", this->targetDir.toLocal8Bit().constData(),
this->maxPrefix.toLocal8Bit().constData(), frameNum,
this->maxExt.toLocal8Bit().constData());
//cout << fina.toLocal8Bit().constData() << endl;
QImage *image = new QImage(fina);
assert(!image->isNull());
QSharedPointer<QImage> out(image);
return out;
}
long long unsigned ImageSequence::GetNumFrames()
{
return this->maxIndex + 1 - this->minIndex;
}
long long unsigned ImageSequence::Length() //Get length
{
int numFrames = this->GetNumFrames();
return numFrames * 1000. / this->frameRate;
}
long long unsigned ImageSequence::GetFrameStartTime(long long unsigned ti)
{
long long unsigned frameNum = float(ti) * this->frameRate / 1000.;
long long unsigned startTime = frameNum * 1000. / this->frameRate;
return startTime;
}