Skip to content

Commit

Permalink
collection: Fixed and Offseted image collection (& sharedptr)
Browse files Browse the repository at this point in the history
this is used by the editors, with the following syntax:
 2    the sequence #2, the image at position i (current player index)
 2@5  the sequence #2, the image at position 5 (fixed)
 2@+5 the sequence #2, the image at position i+5
 2@-1 the sequence #2, the image at position i-1
  • Loading branch information
kidanger committed Mar 19, 2021
1 parent 36b2e3c commit 7860500
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/EditGUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void EditGUI::validate(Sequence& seq)
prog = std::regex_replace(prog, std::regex("\\$" + std::to_string(i+1)), val);
}

ImageCollection* collection = create_edited_collection(edittype, prog);
std::shared_ptr<ImageCollection> collection = create_edited_collection(edittype, prog);
if (collection) {
seq.collection = collection;
}
Expand Down
16 changes: 8 additions & 8 deletions src/ImageCollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class NumpyVideoImageCollection : public VideoImageCollection {
}
};

static ImageCollection* selectCollection(const std::string& filename)
static std::shared_ptr<ImageCollection> selectCollection(const std::string& filename)
{
struct stat st;
unsigned char tag[4];
Expand All @@ -309,13 +309,13 @@ static ImageCollection* selectCollection(const std::string& filename)
fclose(file);

if (tag[0] == 'V' && tag[1] == 'P' && tag[2] == 'P' && tag[3] == 0) {
return new VPPVideoImageCollection(filename);
return std::make_shared<VPPVideoImageCollection>(filename);
} else if (tag[0] == 0x93 && tag[1] == 'N' && tag[2] == 'U' && tag[3] == 'M') {
return new NumpyVideoImageCollection(filename);
return std::make_shared<NumpyVideoImageCollection>(filename);
}

end:
return new SingleImageImageCollection(filename);
return std::make_shared<SingleImageImageCollection>(filename);
}


Expand All @@ -327,20 +327,20 @@ bool endswith(std::string const &fullString, std::string const &ending) {
}
}

ImageCollection* buildImageCollectionFromFilenames(std::vector<std::string>& filenames)
std::shared_ptr<ImageCollection> buildImageCollectionFromFilenames(std::vector<std::string>& filenames)
{
if (filenames.size() == 1) {
return selectCollection(filenames[0]);
}

//!\ here we assume that a sequence composed of multiple files means that each file contains only one image (not true for video files)
// the reason is just that it would be slow to check the tag of each file
MultipleImageCollection* collection = new MultipleImageCollection();
std::shared_ptr<MultipleImageCollection> collection = std::make_shared<MultipleImageCollection>();
for (auto& f : filenames) {
if (endswith(f, ".npy")) { // TODO: this is ugly, but faster than checking the tag
collection->append(new NumpyVideoImageCollection(f));
collection->append(std::make_shared<NumpyVideoImageCollection>(f));
} else {
collection->append(new SingleImageImageCollection(f));
collection->append(std::make_shared<SingleImageImageCollection>(f));
}
}
return collection;
Expand Down
89 changes: 77 additions & 12 deletions src/ImageCollection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class ImageCollection {
virtual void onFileReload(const std::string& filename) = 0;
};

ImageCollection* buildImageCollectionFromFilenames(std::vector<std::string>& filenames);
std::shared_ptr<ImageCollection> buildImageCollectionFromFilenames(std::vector<std::string>& filenames);

class MultipleImageCollection : public ImageCollection {
std::vector<ImageCollection*> collections;
std::vector<std::shared_ptr<ImageCollection>> collections;
std::vector<int> lengths;
int totalLength;

Expand All @@ -33,13 +33,10 @@ class MultipleImageCollection : public ImageCollection {
}

virtual ~MultipleImageCollection() {
for (auto c : collections) {
delete c;
}
collections.clear();
}

void append(ImageCollection* ic) {
void append(std::shared_ptr<ImageCollection> ic) {
collections.push_back(ic);
int len = ic->getLength();
lengths.push_back(len);
Expand Down Expand Up @@ -152,19 +149,16 @@ class VideoImageCollection : public ImageCollection {
class EditedImageCollection : public ImageCollection {
EditType edittype;
std::string editprog;
std::vector<ImageCollection*> collections;
std::vector<std::shared_ptr<ImageCollection>> collections;

public:

EditedImageCollection(EditType edittype, const std::string& editprog,
const std::vector<ImageCollection*>& collections)
const std::vector<std::shared_ptr<ImageCollection>>& collections)
: edittype(edittype), editprog(editprog), collections(collections) {
}

virtual ~EditedImageCollection() {
for (auto c : collections) {
delete c;
}
collections.clear();
}

Expand Down Expand Up @@ -205,7 +199,7 @@ class MaskedImageCollection : public ImageCollection {

public:

MaskedImageCollection(ImageCollection* parent, int masked)
MaskedImageCollection(std::shared_ptr<ImageCollection> parent, int masked)
: parent(parent), masked(masked) {
}

Expand Down Expand Up @@ -239,3 +233,74 @@ class MaskedImageCollection : public ImageCollection {
}
};

class FixedImageCollection : public ImageCollection {
std::shared_ptr<ImageCollection> parent;
int index;

public:

FixedImageCollection(std::shared_ptr<ImageCollection> parent, int index)
: parent(parent), index(index) {
}

virtual ~FixedImageCollection() {
}

const std::string& getFilename(int) const {
return parent->getFilename(index);
}

std::string getKey(int) const {
return parent->getKey(index);
}

int getLength() const {
return 1;
}

std::shared_ptr<ImageProvider> getImageProvider(int) const {
return parent->getImageProvider(index);
}

void onFileReload(const std::string& filename) {
parent->onFileReload(filename);
}
};

class OffsetedImageCollection : public ImageCollection {
std::shared_ptr<ImageCollection> parent;
int offset;

public:

OffsetedImageCollection(std::shared_ptr<ImageCollection> parent, int offset)
: parent(parent), offset(offset) {
}

virtual ~OffsetedImageCollection() {
}

const std::string& getFilename(int index) const {
index = std::max(0, index + offset);
return parent->getFilename(index);
}

std::string getKey(int index) const {
index = std::max(0, index + offset);
return parent->getKey(index);
}

int getLength() const {
return parent->getLength() - offset;
}

std::shared_ptr<ImageProvider> getImageProvider(int index) const {
index = std::max(0, index + offset);
return parent->getImageProvider(index);
}

void onFileReload(const std::string& filename) {
parent->onFileReload(filename);
}
};

4 changes: 2 additions & 2 deletions src/Sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void Sequence::loadFilenames() {
filenames.push_back("-");
}

ImageCollection* col = buildImageCollectionFromFilenames(filenames);
std::shared_ptr<ImageCollection> col = buildImageCollectionFromFilenames(filenames);
this->collection = col;
this->uneditedCollection = col;

Expand Down Expand Up @@ -502,7 +502,7 @@ void Sequence::removeCurrentFrame()
return;
}
int index = player->frame - 1;
collection = new MaskedImageCollection(uneditedCollection, index);
collection = std::make_shared<MaskedImageCollection>(uneditedCollection, index);
uneditedCollection = collection;
editGUI->validate(*this);
player->reconfigureBounds();
Expand Down
4 changes: 2 additions & 2 deletions src/Sequence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct Sequence {
std::string glob;
std::string glob_;

ImageCollection* collection;
std::shared_ptr<ImageCollection> collection;
std::vector<std::string> svgglobs;
std::vector<std::vector<std::string>> svgcollection;
std::map<std::string, std::shared_ptr<SVG>> scriptSVGs;
Expand All @@ -41,7 +41,7 @@ struct Sequence {
std::shared_ptr<Image> image;
std::string error;

ImageCollection* uneditedCollection;
std::shared_ptr<ImageCollection> uneditedCollection;
EditGUI* editGUI;

Sequence();
Expand Down
31 changes: 22 additions & 9 deletions src/editors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,30 +229,43 @@ std::shared_ptr<Image> edit_images(EditType edittype, const std::string& _prog,
#include "Sequence.hpp"
#include "globals.hpp"

ImageCollection* create_edited_collection(EditType edittype, const std::string& _prog)
std::shared_ptr<ImageCollection> create_edited_collection(EditType edittype, const std::string& _prog)
{
char* prog = (char*) _prog.c_str();
std::vector<Sequence*> sequences;
std::vector<std::shared_ptr<ImageCollection>> collections;
while (*prog && *prog != ' ') {
char* old = prog;
int a = strtol(prog, &prog, 10) - 1;
if (prog == old) break;
if (a >= 0 && a < gSequences.size()) {
sequences.push_back(gSequences[a]);
Sequence* s = gSequences[a];
std::shared_ptr<ImageCollection> c(s->uneditedCollection);
if (*prog == '@') {
prog++;
int relative = *prog == '-' || *prog == '+';
char* old = prog;
int a = strtol(prog, &prog, 10);
int value = a;
if (prog == old) break;

printf("relative %d value %d\n", relative, value);
if (relative) {
c = std::make_shared<OffsetedImageCollection>(c, value);
} else {
c = std::make_shared<FixedImageCollection>(c, value);
}
}
collections.push_back(c);
}
if (*prog == ' ') break;
if (*prog) prog++;
}
while (*prog == ' ') prog++;

if (sequences.empty()) {
if (collections.empty()) {
return nullptr;
}

std::vector<ImageCollection*> collections;
for (auto s : sequences) {
collections.push_back(s->uneditedCollection);
}
return new EditedImageCollection(edittype, std::string(prog), collections);
return std::make_shared<EditedImageCollection>(edittype, std::string(prog), collections);
}

2 changes: 1 addition & 1 deletion src/editors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ std::shared_ptr<Image> edit_images(EditType edittype, const std::string& prog,
const std::vector<std::shared_ptr<Image>>& images,
std::string& error);

class ImageCollection* create_edited_collection(EditType edittype, const std::string& prog);
std::shared_ptr<class ImageCollection> create_edited_collection(EditType edittype, const std::string& prog);

2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ int main(int argc, char* argv[])
for (auto seq : gSequences) {
if (!seq->player)
continue;
ImageCollection* collection = seq->collection;
std::shared_ptr<ImageCollection> collection = seq->collection;
if (!collection || collection->getLength() == 0)
continue;
int frame = (seq->player->frame + i - 1) % collection->getLength();
Expand Down

0 comments on commit 7860500

Please sign in to comment.