µGFX  2.9
version 2.9
gdisp_image.h
Go to the documentation of this file.
1 /*
2  * This file is subject to the terms of the GFX License. If a copy of
3  * the license was not distributed with this file, you can obtain one at:
4  *
5  * http://ugfx.io/license.html
6  */
7 
8 /**
9  * @file src/gdisp/gdisp_image.h
10  *
11  * @defgroup Image Image
12  * @ingroup GDISP
13  *
14  * @brief Sub-Module for image handling.
15  *
16  * @{
17  */
18 
19 #ifndef _GDISP_IMAGE_H
20 #define _GDISP_IMAGE_H
21 #if (GFX_USE_GDISP && GDISP_NEED_IMAGE) || defined(__DOXYGEN__)
22 
23 /**
24  * @brief The type of image
25  */
26 typedef gU16 gdispImageType;
27  #define GDISP_IMAGE_TYPE_UNKNOWN 0
28  #define GDISP_IMAGE_TYPE_NATIVE 1
29  #define GDISP_IMAGE_TYPE_GIF 2
30  #define GDISP_IMAGE_TYPE_BMP 3
31  #define GDISP_IMAGE_TYPE_JPG 4
32  #define GDISP_IMAGE_TYPE_PNG 5
33 
34 /**
35  * @brief An image error code
36  */
37 typedef gU16 gdispImageError;
38  #define GDISP_IMAGE_ERR_OK 0
39  #define GDISP_IMAGE_ERR_UNRECOVERABLE 0x8000
40  #define GDISP_IMAGE_ERR_BADFORMAT (GDISP_IMAGE_ERR_UNRECOVERABLE+1)
41  #define GDISP_IMAGE_ERR_BADDATA (GDISP_IMAGE_ERR_UNRECOVERABLE+2)
42  #define GDISP_IMAGE_ERR_UNSUPPORTED (GDISP_IMAGE_ERR_UNRECOVERABLE+3)
43  #define GDISP_IMAGE_ERR_UNSUPPORTED_OK 3
44  #define GDISP_IMAGE_ERR_NOMEMORY (GDISP_IMAGE_ERR_UNRECOVERABLE+4)
45  #define GDISP_IMAGE_ERR_NOSUCHFILE (GDISP_IMAGE_ERR_UNRECOVERABLE+5)
46  #define GDISP_IMAGE_ERR_NULLPOINTER (GDISP_IMAGE_ERR_UNRECOVERABLE+6)
47 
48 /**
49  * @brief Image flags
50  */
51 typedef gU16 gdispImageFlags;
52  #define GDISP_IMAGE_FLG_TRANSPARENT 0x0001 /* The image has transparency */
53  #define GDISP_IMAGE_FLG_ANIMATED 0x0002 /* The image has animation */
54  #define GDISP_IMAGE_FLG_MULTIPAGE 0x0004 /* The image has multiple pages */
55 
56 /**
57  * @brief The structure for an image
58  */
59 typedef struct gImage {
60  gdispImageType type; /* @< The image type */
61  gdispImageFlags flags; /* @< The image flags */
62  gColor bgcolor; /* @< The default background color */
63  gCoord width, height; /* @< The image dimensions */
64  GFILE * f; /* @< The underlying GFILE */
65  #if GDISP_NEED_IMAGE_ACCOUNTING
66  gU32 memused; /* @< How much RAM is currently allocated */
67  gU32 maxmemused; /* @< How much RAM has been allocated (maximum) */
68  #endif
69  const struct gdispImageHandlers * fns; /* @< Don't mess with this! */
70  void * priv; /* @< Don't mess with this! */
72 
73 /**
74  * @brief Initialise a gImage object
75  *
76  * @param[in] img The image structure to initialise
77  *
78  */
80 
81 /**
82  * @brief Open an image using an open GFILE and get it ready for drawing
83  * @details Determine the image format and get ready to decode the first image frame
84  * @return GDISP_IMAGE_ERR_OK (0) on success or an error code.
85  *
86  * @param[in] img The image structure
87  * @param[in] f The open GFILE stream.
88  *
89  * @pre The GFILE must be open for reading.
90  *
91  * @note This determines which decoder to use and then initialises all other fields
92  * in the gImage structure.
93  * @note The image background color is set to White.
94  * @note There are three types of return - everything OK, partial success and unrecoverable
95  * failures. For everything OK it returns GDISP_IMAGE_ERR_OK. A partial success can
96  * be distinguished from a unrecoverable failure by testing the GDISP_IMAGE_ERR_UNRECOVERABLE
97  * bit in the error code.
98  * A partial success return code means an image can still be drawn but perhaps with
99  * reduced functionality eg only the first page of a multi-page image.
100  * @note @p gdispImageClose() should be called when finished with the image. This will close
101  * the image and its underlying GFILE file. Note that images opened with partial success
102  * (eg GDISP_IMAGE_ERR_UNSUPPORTED_OK)
103  * still need to be closed when you are finished with them.
104  */
106 
107 /**
108  * @brief Open an image in a file and get it ready for drawing
109  * @details Determine the image format and get ready to decode the first image frame
110  * @return GDISP_IMAGE_ERR_OK (0) on success or an error code.
111  *
112  * @pre You must have included the file-system support into GFILE that you want to use.
113  *
114  * @param[in] img The image structure
115  * @param[in] filename The filename to open
116  *
117  * @note This function just opens the GFILE using the filename and passes it to @p gdispImageOpenGFile().
118  */
119 #define gdispImageOpenFile(img, filename) gdispImageOpenGFile((img), gfileOpen((filename), "rb"))
120 
121 /**
122  * @brief Open an image in a ChibiOS basefilestream and get it ready for drawing
123  * @details Determine the image format and get ready to decode the first image frame
124  * @return GDISP_IMAGE_ERR_OK (0) on success or an error code.
125  *
126  * @pre GFILE_NEED_CHIBIOSFS and GFX_USE_OS_CHIBIOS must be GFXON. This only makes sense on the ChibiOS
127  * operating system.
128  *
129  * @param[in] img The image structure
130  * @param[in] BaseFileStreamPtr A pointer to an open BaseFileStream
131  *
132  * @note This function just opens the GFILE using the basefilestream and passes it to @p gdispImageOpenGFile().
133  */
134 #define gdispImageOpenBaseFileStream(img, BaseFileStreamPtr) gdispImageOpenGFile((img), gfileOpenBaseFileStream((BaseFileStreamPtr), "rb"))
135 
136 /**
137  * @brief Open an image in memory and get it ready for drawing
138  * @details Determine the image format and get ready to decode the first image frame
139  * @return GDISP_IMAGE_ERR_OK (0) on success or an error code.
140  *
141  * @pre GFILE_NEED_MEMFS must be GFXON
142  *
143  * @param[in] img The image structure
144  * @param[in] ptr A pointer to the image bytes in memory
145  *
146  * @note This function just opens the GFILE using the basefilestream and passes it to @p gdispImageOpenGFile().
147  */
148 #define gdispImageOpenMemory(img, ptr) gdispImageOpenGFile((img), gfileOpenMemory((void *)(ptr), "rb"))
149 
150 /**
151  * @brief Close an image and release any dynamically allocated working storage.
152  *
153  * @param[in] img The image structure
154  *
155  * @pre gdispImageOpenFile() must have returned successfully.
156  *
157  * @note Also calls the IO close function (if it hasn't already been called).
158  */
160 
161 /**
162  * @brief Is an image open.
163  * @return gTrue if the image is currently open.
164  *
165  * @param[in] img The image structure
166  *
167  * @note Be careful with calling this on an uninitialized image structure as the image
168  * will contain random data which may be interpreted as meaning the image
169  * is open. Clearing the Image structure to 0's will guarantee the image
170  * is seen as being closed.
171  */
173 
174 /**
175  * @brief Set the background color of the image.
176  *
177  * @param[in] img The image structure
178  * @param[in] bgcolor The background color to use
179  *
180  * @pre gdispImageOpen() must have returned successfully.
181  *
182  * @note This color is only used when an image has to restore part of the background before
183  * continuing with drawing that includes transparency eg some GIF animations.
184  */
185 void gdispImageSetBgColor(gImage *img, gColor bgcolor);
186 
187 /**
188  * @brief Cache the image
189  * @details Decodes and caches the current frame into RAM.
190  * @return GDISP_IMAGE_ERR_OK (0) on success or an error code.
191  *
192  * @param[in] img The image structure
193  *
194  * @pre gdispImageOpen() must have returned successfully.
195  *
196  * @note This can use a LOT of RAM!
197  * @note The decoder may choose to ignore the request for caching. If it does so it will
198  * return GDISP_IMAGE_ERR_UNSUPPORTED_OK.
199  * @note A fatal error here does not necessarily mean that drawing the image will fail. For
200  * example, a GDISP_IMAGE_ERR_NOMEMORY error simply means there isn't enough RAM to
201  * cache the image.
202  */
204 
205 /**
206  * @brief Draw the image
207  * @return GDISP_IMAGE_ERR_OK (0) on success or an error code.
208  *
209  * @param[in] g The display to draw on
210  * @param[in] img The image structure
211  * @param[in] x,y The screen location to draw the image
212  * @param[in] cx,cy The area on the screen to draw
213  * @param[in] sx,sy The image position to start drawing at
214  *
215  * @pre gdispImageOpen() must have returned successfully.
216  *
217  * @note If sx,sy + cx,cy is outside the image boundaries the area outside the image
218  * is simply not drawn.
219  * @note If @p gdispImageCache() has been called first for this frame, this routine will draw using a
220  * fast blit from the cached frame. If not, it reads the input and decodes it as it
221  * is drawing. This may be significantly slower than if the image has been cached (but
222  * uses a lot less RAM)
223  */
224 gdispImageError gdispGImageDraw(GDisplay *g, gImage *img, gCoord x, gCoord y, gCoord cx, gCoord cy, gCoord sx, gCoord sy);
225 #define gdispImageDraw(img,x,y,cx,cy,sx,sy) gdispGImageDraw(GDISP,img,x,y,cx,cy,sx,sy)
226 
227 /**
228  * @brief Prepare for the next frame/page in the image file.
229  * @return A time in milliseconds to keep displaying the current frame before trying to draw
230  * the next frame. Watch out for the special values gDelayNone and gDelayForever.
231  *
232  * @param[in] img The image structure
233  *
234  * @pre gdispImageOpen() must have returned successfully.
235  *
236  * @note It will return gDelayNone if the first frame/page hasn't been drawn or if the next frame
237  * should be drawn immediately.
238  * @note It will return gDelayForever if another image frame doesn't exist or an error has occurred.
239  * @note Images that support multiple pages (eg TIFF files) will return gDelayNone between pages
240  * and then gDelayForever when there are no more pages.
241  * @note An image that displays a looped animation will never return gDelayForever unless it
242  * gets an error.
243  * @note Calling gdispImageDraw() after getting a gDelayForever will go back to drawing the first
244  * frame/page.
245  */
246 gDelay gdispImageNext(gImage *img);
247 
248 /**
249  * @brief Get the number of entries in the color palette.
250  * @return The number of entries in the color palette or 0 if the image doesn't use a color palette.
251  *
252  * @param[in] img The image structure
253  *
254  * @pre gdispImageOpen() must have returned successfully.
255  */
257 
258 /**
259  * @brief Get an entry in the color palette.
260  * @return The color value at a given position in the color palette.
261  *
262  * @param[in] img The image structure
263  * @param[in] index The index of the color palette entry
264  *
265  * @pre gdispImageOpen() must have returned successfully.
266  *
267  * @note This function will return 0 if the index is out of bounds or if the image doesn't use a color palette.
268  */
270 
271 /**
272  * @brief Modify an entry in the color palette.
273  * @return @p gTrue on success, @p gFalse otherwise.
274  *
275  * @param[in] img The image structure
276  * @param[in] index The index of the color palette entry
277  * @param[in] newColor The new color value of the specified entry
278  *
279  * @pre gdispImageOpen() must have returned successfully.
280  * @note This function will return @p gFalse if the index is out of bounds or if the image doesn't use a color palette.
281  */
282 gBool gdispImageAdjustPalette(gImage *img, gU16 index, gColor newColor);
283 
284 #endif /* GFX_USE_GDISP && GDISP_NEED_IMAGE */
285 #endif /* _GDISP_IMAGE_H */
286 /** @} */
287 
COLOR_TYPE gColor
The color type definition.
Definition: gdisp_colors.h:437
gI16 gCoord
The type for a coordinate or length on the screen.
Definition: gdisp.h:39
struct GFILE GFILE
A file pointer.
Definition: gfile.h:34
gdispImageError gdispImageCache(gImage *img)
Cache the image.
gU16 gdispImageFlags
Image flags.
Definition: gdisp_image.h:51
gdispImageError gdispImageOpenGFile(gImage *img, GFILE *f)
Open an image using an open GFILE and get it ready for drawing.
gBool gdispImageAdjustPalette(gImage *img, gU16 index, gColor newColor)
Modify an entry in the color palette.
gdispImageError gdispGImageDraw(GDisplay *g, gImage *img, gCoord x, gCoord y, gCoord cx, gCoord cy, gCoord sx, gCoord sy)
Draw the image.
gU16 gdispImageGetPaletteSize(gImage *img)
Get the number of entries in the color palette.
void gdispImageInit(gImage *img)
Initialise a gImage object.
gU16 gdispImageError
An image error code.
Definition: gdisp_image.h:37
gDelay gdispImageNext(gImage *img)
Prepare for the next frame/page in the image file.
gBool gdispImageIsOpen(gImage *img)
Is an image open.
void gdispImageClose(gImage *img)
Close an image and release any dynamically allocated working storage.
void gdispImageSetBgColor(gImage *img, gColor bgcolor)
Set the background color of the image.
gU16 gdispImageType
The type of image.
Definition: gdisp_image.h:26
struct gImage gImage
The structure for an image.
gColor gdispImageGetPalette(gImage *img, gU16 index)
Get an entry in the color palette.
The structure for an image.
Definition: gdisp_image.h:59