1// Aseprite
2// Copyright (C) 2021 Igara Studio S.A.
3// Copyright (C) 2001-2017 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "app/file/file_formats_manager.h"
13
14#include "app/file/file_format.h"
15#include "app/file/format_options.h"
16#include "base/string.h"
17#include "dio/detect_format.h"
18
19#include <algorithm>
20#include <cstring>
21
22namespace app {
23
24extern FileFormat* CreateAseFormat();
25extern FileFormat* CreateBmpFormat();
26extern FileFormat* CreateCssFormat();
27extern FileFormat* CreateFliFormat();
28extern FileFormat* CreateGifFormat();
29extern FileFormat* CreateIcoFormat();
30extern FileFormat* CreateJpegFormat();
31extern FileFormat* CreatePcxFormat();
32extern FileFormat* CreatePngFormat();
33extern FileFormat* CreateSvgFormat();
34extern FileFormat* CreateTgaFormat();
35
36#ifdef ENABLE_PSD
37extern FileFormat* CreatePsdFormat();
38#endif
39
40#ifdef ENABLE_WEBP
41extern FileFormat* CreateWebPFormat();
42#endif
43
44static FileFormatsManager* singleton = NULL;
45
46// static
47FileFormatsManager* FileFormatsManager::instance()
48{
49 if (!singleton)
50 singleton = new FileFormatsManager;
51 return singleton;
52}
53
54// static
55void FileFormatsManager::destroyInstance()
56{
57 delete singleton;
58 singleton = NULL;
59}
60
61FileFormatsManager::FileFormatsManager()
62{
63 // The first format is the default image format in FileSelector
64 registerFormat(CreateAseFormat());
65 registerFormat(CreateBmpFormat());
66 registerFormat(CreateCssFormat());
67 registerFormat(CreateFliFormat());
68 registerFormat(CreateGifFormat());
69 registerFormat(CreateIcoFormat());
70 registerFormat(CreateJpegFormat());
71 registerFormat(CreatePcxFormat());
72 registerFormat(CreatePngFormat());
73
74#ifdef ENABLE_PSD
75 registerFormat(CreatePsdFormat());
76#endif
77
78 registerFormat(CreateSvgFormat());
79 registerFormat(CreateTgaFormat());
80
81#ifdef ENABLE_WEBP
82 registerFormat(CreateWebPFormat());
83#endif
84}
85
86FileFormatsManager::~FileFormatsManager()
87{
88 FileFormatsList::iterator end = this->end();
89 for (FileFormatsList::iterator it = begin(); it != end; ++it) {
90 delete (*it); // delete the FileFormat
91 }
92}
93
94void FileFormatsManager::registerFormat(FileFormat* fileFormat)
95{
96 m_formats.push_back(fileFormat);
97}
98
99FileFormatsList::iterator FileFormatsManager::begin()
100{
101 return m_formats.begin();
102}
103
104FileFormatsList::iterator FileFormatsManager::end()
105{
106 return m_formats.end();
107}
108
109FileFormat* FileFormatsManager::getFileFormat(const dio::FileFormat dioFormat) const
110{
111 for (FileFormat* ff : m_formats)
112 if (ff->dioFormat() == dioFormat)
113 return ff;
114 return nullptr;
115}
116
117} // namespace app
118