comparison foosdk/sdk/foobar2000/SDK/image.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 "imageLoaderLite.h"
3 #include "filesystem.h"
4 #include "tracks.h"
5
6 namespace fb2k {
7
8 //! \since 2.0
9 struct imageSize_t {
10 double width, height;
11 static imageSize_t empty() { imageSize_t s = {}; return s; }
12
13 static bool equals(imageSize_t const & v1, imageSize_t const & v2);
14
15 bool isValid() const;
16 imageSize_t fitIn( double longerEdge ) const;
17 imageSize_t fitIn( imageSize_t size ) const;
18 bool operator==(const imageSize_t & other) const { return equals(*this, other); }
19 bool operator!=(const imageSize_t & other) const { return !equals(*this, other); }
20
21 void sanitize();
22
23 //! Helper to allow imageSize_t objects to be used in various sorted contexts.
24 static bool greater(imageSize_t const & v1, imageSize_t const & v2);
25 bool operator>(const imageSize_t & other) const { return greater(*this, other); }
26 bool operator<(const imageSize_t & other) const { return greater(other, *this); }
27 };
28 inline imageSize_t imageSizeMake(double w, double h) { imageSize_t s = { w, h }; return s; }
29
30 //! \since 2.0
31 class image : public service_base {
32 FB2K_MAKE_SERVICE_INTERFACE( image, service_base );
33 public:
34
35 //! Source URL of this image. May be null if not known or available.
36 virtual stringRef sourceURL() = 0;
37 //! Source data of this image. May be null if not known or available.
38 virtual memBlockRef sourceData() = 0;
39 //! Resize the image to the specified size.
40 virtual imageRef resize(imageSize_t toSize) = 0;
41
42 //! Returns image size.
43 virtual imageSize_t size() = 0;
44
45 image::ptr resizeToFit( imageSize_t fitInSize );
46
47
48 //! Saves as PNG.
49 virtual void saveAsPNG( const char * pathSaveAt ) = 0;
50 //! Saves as JPEG. Quality represented in 0..1 scale
51 virtual void saveAsJPEG( const char * pathSaveAt, float jpegQuality) = 0;
52
53 //! Returns if the image has alpha channel or not
54 virtual bool hasAlpha() { return false; }
55
56 virtual bool isFileBacked() { return false; }
57
58 //! Returns platform-specific native data. The data remains owned by this image object.
59 virtual nativeImage_t getNative() = 0;
60 //! Detaches platform-specific native data from this image object. The caller becomes the owner of the native data and is responsible for its deletion.
61 virtual nativeImage_t detachNative() = 0;
62
63 static image::ptr empty() { return NULL; }
64 };
65
66 struct imageCreatorArg_t {
67 imageSize_t inWantSize;
68 imageSize_t outRealSize;
69
70 static imageCreatorArg_t empty() {
71 imageCreatorArg_t arg = {};
72 return arg;
73 }
74 };
75
76 //! \since 2.0
77 //! Provides access to OS specific image object creation facilities. \n
78 //! By convention, imageCreator methods return nullptr when loading the image fails, rather than throw exceptions.
79 class imageCreator : public service_base {
80 FB2K_MAKE_SERVICE_COREAPI( imageCreator );
81 public:
82 typedef imageCreatorArg_t arg_t;
83
84 virtual imageRef loadImageNamed(const char * imgName, imageSize_t sizeHint = imageSize_t::empty()) = 0;
85 virtual imageRef loadImageData(const void * data, size_t size, stringRef sourceURL = nullptr) = 0;
86 virtual imageRef loadImageData(memBlockRef block, stringRef sourceURL = nullptr) = 0;
87 virtual imageRef loadImageFromFile(const char * filePath, abort_callback & aborter) = 0;
88
89 //! Opportunistic image loader helper. Returns immediately without doing any file access and an existing instance of an image object is ready for reuse. Returns null if no such object is available at this time.
90 virtual imageRef tryReuseImageInstance( const char * filePath ) = 0;
91
92
93 virtual imageRef loadImageNamed2( const char * imgName, arg_t & arg );
94 virtual imageRef loadImageData2( const void * data, size_t size, stringRef sourceURL, arg_t & arg );
95 virtual imageRef loadImageData2(memBlockRef block, stringRef sourceURL, arg_t & arg);
96 virtual imageRef loadImageFromFile2(const char * filePath, abort_callback & aborter, arg_t & arg);
97 virtual imageRef loadImageFromFile3( fsItemFile::ptr fsFile, abort_callback & aborter, arg_t & arg );
98 };
99
100 inline imageRef imageWithData( const void * data, size_t size ) {return imageCreator::get()->loadImageData( data, size ); }
101 inline imageRef imageWithData( memBlockRef data ) {return imageCreator::get()->loadImageData(data); }
102
103
104 struct imageLocation_t {
105
106 bool isValid() const { return path.is_valid(); }
107 bool isEmpty() const { return path.is_empty(); }
108 void setPath( const char * path );
109 void setPath( stringRef path );
110 bool setTrack2( trackRef track );
111 bool setTrack2( trackRef track, const GUID & albumArtID );
112 void setStock( const char * name );
113 void setEmbedded( const char * path, const GUID & albumArtID );
114
115 //! Returns stock image name if this is a stock image reference, nullptr otherwise.
116 stringRef getStock( ) const;
117
118 static bool equals( const imageLocation_t & l1, const imageLocation_t & l2 );
119
120 bool operator==( imageLocation_t const & other) const;
121 bool operator!=( imageLocation_t const & other) const;
122
123 stringRef path;
124 t_filetimestamp knownTimeStamp = filetimestamp_invalid;// hint
125 };
126
127 //! \since 2.0
128 //! Image loader service.
129 class imageLoader : public service_base {
130 FB2K_MAKE_SERVICE_COREAPI( imageLoader );
131 public:
132
133 struct arg_t {
134 arg_t() { wantSize = imageSize_t::empty(); }
135 arg_t( imageSize_t const & size ) : wantSize(size) {}
136 imageSize_t wantSize;
137 imageRef bigImageHint; // optional, provide if you have big non resized version available
138 };
139
140 static arg_t defArg() { arg_t r; return r; }
141
142 virtual imageRef tryLoadFromCache( imageLocation_t const & loc, arg_t const & arg ) = 0;
143 virtual imageRef loadSynchronously( imageLocation_t const & loc, arg_t const & arg, abort_callback & aborter ) = 0;
144 virtual objRef beginLoad( imageLocation_t const & loc, arg_t const & arg, objReceiverRef receiver ) = 0;
145 //! Retrieves image cache path for the specified location. The file at the returned path may or may not extist. \n
146 //! Caller must provide valid URL and timestamp in imageLocation_t.
147 virtual stringRef cacheLocation( imageLocation_t const & loc, arg_t const & arg) = 0;
148
149 //! Similar to beginLoad; completes synchronously if the image is already in cache, passing the image to the receiver.
150 objRef beginLoadEx( imageLocation_t const & loc, arg_t const & arg, objReceiverRef receiver );
151
152 //! Helper; loads stock image synchronously.
153 imageRef loadStock( const char * name, arg_t const & arg, abort_callback & aborter );
154
155 //! Returns array of possible folder.jpg and alike file names to check for folder-pic. \n
156 //! Provided to avoid hardcoding the list (of folder.jpg, cover.jpg, etc) everywhere. \n
157 //! The returned strings are guaranteed lowercase.
158 virtual arrayRef folderPicNames() = 0;
159 };
160 }
161
162 pfc::string_base & operator<<(pfc::string_base & p_fmt,const fb2k::imageSize_t & imgSize);