comparison foosdk/sdk/foobar2000/SDK/album_art_helpers.h @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #pragma once
2 #include "album_art.h"
3
4 //! Implements album_art_data.
5 class album_art_data_impl : public album_art_data {
6 public:
7 const void * data() const override {return m_content.get_ptr();}
8 size_t size() const override {return m_content.get_size();}
9
10 const void * get_data() const { return m_content.get_ptr(); }
11 size_t sizeget_() const { return m_content.get_size(); }
12
13 void * get_ptr() {return m_content.get_ptr();}
14 void set_size(t_size p_size) {m_content.set_size(p_size);}
15
16 //! Reads picture data from the specified stream object.
17 void from_stream(stream_reader * p_stream,t_size p_bytes,abort_callback & p_abort) {
18 set_size(p_bytes); p_stream->read_object(get_ptr(),p_bytes,p_abort);
19 }
20
21 //! Creates an album_art_data object from picture data contained in a memory buffer.
22 static album_art_data_ptr g_create(const void * p_buffer,t_size p_bytes) {
23 service_ptr_t<album_art_data_impl> instance = new service_impl_t<album_art_data_impl>();
24 instance->set_size(p_bytes);
25 memcpy(instance->get_ptr(),p_buffer,p_bytes);
26 return instance;
27 }
28 //! Creates an album_art_data object from picture data contained in a stream.
29 static album_art_data_ptr g_create(stream_reader * p_stream,t_size p_bytes,abort_callback & p_abort) {
30 service_ptr_t<album_art_data_impl> instance = new service_impl_t<album_art_data_impl>();
31 instance->from_stream(p_stream,p_bytes,p_abort);
32 return instance;
33 }
34
35 private:
36 pfc::array_t<t_uint8> m_content;
37 };
38
39
40 //! Helper - simple implementation of album_art_extractor_instance.
41 class album_art_extractor_instance_simple : public album_art_extractor_instance {
42 public:
43 void set(const GUID & p_what,album_art_data_ptr p_content) {m_content.set(p_what,p_content);}
44 bool have_item(const GUID & p_what) {return m_content.have_item(p_what);}
45 album_art_data_ptr query(const GUID & p_what,abort_callback & p_abort) {
46 p_abort.check();
47 album_art_data_ptr temp;
48 if (!m_content.query(p_what,temp)) throw exception_album_art_not_found();
49 return temp;
50 }
51 bool is_empty() const {return m_content.get_count() == 0;}
52 bool remove(const GUID & p_what) {
53 return m_content.remove(p_what);
54 }
55 void remove_all() {
56 m_content.remove_all();
57 }
58 private:
59 pfc::map_t<GUID,album_art_data_ptr> m_content;
60 };
61
62 //! Helper implementation of album_art_extractor - reads album art from arbitrary file formats that comply with APEv2 tagging specification.
63 class album_art_extractor_impl_stdtags : public album_art_extractor_v2 {
64 public:
65 //! @param exts Semicolon-separated list of file format extensions to support.
66 album_art_extractor_impl_stdtags(const char * exts, const GUID & guid) : m_guid(guid) {
67 pfc::splitStringSimple_toList(m_extensions,';',exts);
68 }
69
70 bool is_our_path(const char * p_path,const char * p_extension) override {
71 (void)p_path;
72 return m_extensions.have_item(p_extension);
73 }
74
75 album_art_extractor_instance_ptr open(file_ptr p_filehint,const char * p_path,abort_callback & p_abort) override {
76 PFC_ASSERT( is_our_path(p_path, pfc::string_extension(p_path) ) );
77 file_ptr l_file ( p_filehint );
78 if (l_file.is_empty()) filesystem::g_open_read(l_file, p_path, p_abort);
79 return tag_processor_album_art_utils::get()->open( l_file, p_abort );
80 }
81
82 GUID get_guid() override {
83 return m_guid;
84 }
85 private:
86 pfc::avltree_t<pfc::string,pfc::string::comparatorCaseInsensitiveASCII> m_extensions;
87 const GUID m_guid;
88 };
89
90 //! Helper implementation of album_art_editor - edits album art from arbitrary file formats that comply with APEv2 tagging specification.
91 class album_art_editor_impl_stdtags : public album_art_editor_v2 {
92 public:
93 //! @param exts Semicolon-separated list of file format extensions to support.
94 album_art_editor_impl_stdtags(const char * exts, const GUID & guid) : m_guid(guid) {
95 pfc::splitStringSimple_toList(m_extensions,';',exts);
96 }
97
98 bool is_our_path(const char * p_path,const char * p_extension) override {
99 (void)p_path;
100 return m_extensions.have_item(p_extension);
101 }
102
103 album_art_editor_instance_ptr open(file_ptr p_filehint,const char * p_path,abort_callback & p_abort) override {
104 PFC_ASSERT( is_our_path(p_path, pfc::string_extension(p_path) ) );
105 file_ptr l_file ( p_filehint );
106 if (l_file.is_empty()) filesystem::g_open(l_file, p_path, filesystem::open_mode_write_existing, p_abort);
107 return tag_processor_album_art_utils::get()->edit( l_file, p_abort );
108 }
109 GUID get_guid() override {
110 return m_guid;
111 }
112 private:
113 pfc::avltree_t<pfc::string,pfc::string::comparatorCaseInsensitiveASCII> m_extensions;
114 const GUID m_guid;
115
116 };
117
118 //! Helper - a more advanced implementation of album_art_extractor_instance.
119 class album_art_extractor_instance_fileref : public album_art_extractor_instance {
120 public:
121 album_art_extractor_instance_fileref(file::ptr f) : m_file(f) {}
122
123 void set(const GUID & p_what,t_filesize p_offset, t_filesize p_size) {
124 const t_fileref ref = {p_offset, p_size};
125 m_data.set(p_what, ref);
126 m_cache.remove(p_what);
127 }
128
129 bool have_item(const GUID & p_what) {
130 return m_data.have_item(p_what);
131 }
132
133 album_art_data_ptr query(const GUID & p_what,abort_callback & p_abort) {
134 album_art_data_ptr item;
135 if (m_cache.query(p_what,item)) return item;
136 t_fileref ref;
137 if (!m_data.query(p_what, ref)) throw exception_album_art_not_found();
138 m_file->seek(ref.m_offset, p_abort);
139 item = album_art_data_impl::g_create(m_file.get_ptr(), pfc::downcast_guarded<t_size>(ref.m_size), p_abort);
140 m_cache.set(p_what, item);
141 return item;
142 }
143 bool is_empty() const {return m_data.get_count() == 0;}
144 private:
145 struct t_fileref {
146 t_filesize m_offset, m_size;
147 };
148 const file::ptr m_file;
149 pfc::map_t<GUID, t_fileref> m_data;
150 pfc::map_t<GUID, album_art_data::ptr> m_cache;
151 };
152
153 //! album_art_path_list implementation helper
154 class album_art_path_list_impl : public album_art_path_list {
155 public:
156 album_art_path_list_impl(const char* single) { m_data.set_size(1); m_data[0] = single; }
157 template<typename t_in> album_art_path_list_impl(const t_in & in) {pfc::list_to_array(m_data, in);}
158 const char * get_path(t_size index) const {return m_data[index];}
159 t_size get_count() const {return m_data.get_size();}
160 private:
161 pfc::array_t<pfc::string8> m_data;
162 };
163
164 //! album_art_path_list implementation helper
165 class album_art_path_list_dummy : public album_art_path_list {
166 public:
167 const char * get_path(t_size) const override {FB2K_BugCheck();}
168 t_size get_count() const override {return 0;}
169 };