1 /*! \file
2  * \brief Sprite data handling.
3  * \details Functions and structs for reading, writing and modifying OMF:2097 sprite data.
4  * \copyright MIT license.
5  * \date 2013-2014
6  * \author huntercool
7  * \author Andrew Thompson
8  * \author Tuomas Virtanen
9  */
10 
11 #ifndef _SD_SPRITE_H
12 #define _SD_SPRITE_H
13 
14 #include <stdint.h>
15 #include "shadowdive/vga_image.h"
16 #include "shadowdive/rgba_image.h"
17 #ifdef SD_USE_INTERNAL
18     #include "shadowdive/internal/reader.h"
19     #include "shadowdive/internal/writer.h"
20 #endif
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /*! \brief Sprite image
27  *
28  * An encoded sprite image. The image is paletted, and encoded with
29  * a type of RLE encoding. Decoding can be done to an RGBA image with
30  * a palette, or directly to a VGA image. Sprite length varies depending on how many
31  * "invisible" pixels it has.
32  */
33 typedef struct {
34     int16_t pos_x; ///< Position of sprite, X-axis
35     int16_t pos_y; ///< Position of sprite, Y-axis
36     uint8_t index; ///< Sprite index
37     uint8_t missing; ///< Is sprite data missing? If this is 1, then data points to the data of another sprite.
38     uint16_t width; ///< Pixel width of the sprite
39     uint16_t height; ///< Pixel height of the sprite
40     uint16_t len; ///< Byte length of the packed sprite data
41     char *data; ///< Packed sprite data
42 } sd_sprite;
43 
44 /*! \brief Initialize sprite structure
45  *
46  * Initializes the sprite structure with empty values.
47  *
48  * \retval SD_INVALID_INPUT Sprite struct pointer was NULL
49  * \retval SD_SUCCESS Success.
50  *
51  * \param sprite Allocated sprite struct pointer.
52  */
53 int sd_sprite_create(sd_sprite *sprite);
54 
55 /*! \brief Copy sprite structure
56  *
57  * Copies the contents of an sprite structure. _ALL_ internals will be copied.
58  * The copied structure must be freed using sd_sprite_free().
59  *
60  * Destination buffer does not need to be cleared. Source buffer must be a valid
61  * sprite structure, or problems are likely to appear.
62  *
63  * \retval SD_OUT_OF_MEMORY Memory ran out. Destination struct should now be considered invalid and freed.
64  * \retval SD_INVALID_INPUT Either input value was NULL.
65  * \retval SD_SUCCESS Success.
66  *
67  * \param dst Destination sprite struct pointer.
68  * \param src Source sprite struct pointer.
69  */
70 int sd_sprite_copy(sd_sprite *dst, const sd_sprite *src);
71 
72 /*! \brief Free sprite structure
73  *
74  * Frees up all memory reserved by the sprite structure.
75  * All contents will be freed, all pointers to contents will be invalid.
76  *
77  * \param sprite Sprite struct to free.
78  */
79 void sd_sprite_free(sd_sprite *sprite);
80 
81 /*! \brief Encode RGBA data to sprite data
82  *
83  * Encodes RGBA image data to sprite image data. Color values will be matched to exact values in
84  * the palette. If no matching value is found for the pixel, the pixel color will be black.
85  *
86  * \retval SD_OUT_OF_MEMORY Memory ran out. Any output should be considered invalid and freed.
87  * \retval SD_INVALID_INPUT Dst, src or palette was NULL.
88  * \retval SD_SUCCESS Success.
89  *
90  * \param dst Destination sprite struct pointer.
91  * \param src Source RGBA image pointer
92  * \param pal Palette that should be used for the conversion
93  * \param remapping Palette remapping table that should be used. -1 for none.
94  */
95 int sd_sprite_rgba_encode(
96     sd_sprite *dst,
97     const sd_rgba_image *src,
98     const sd_palette *pal,
99     int remapping);
100 
101 /*! \brief Decode sprite data to RGBA format
102  *
103  * Decodes the sprite image to RGBA image format.
104  *
105  * Note! The output RGBA image will be created here. If the image had been
106  * already created by using sd_rgba_image_create() previously, there may
107  * potentially be a memory leak, since the old image internals will not be freed.
108  *
109  * \retval SD_OUT_OF_MEMORY Memory ran out. Any output should be considered invalid and freed.
110  * \retval SD_INVALID_INPUT Dst, src or palette was NULL.
111  * \retval SD_SUCCESS Success.
112  *
113  * \param dst Destination RGBA image struct pointer.
114  * \param src Source Sprite image pointer
115  * \param pal Palette that should be used for the conversion
116  * \param remapping Palette remapping table that should be used. -1 for none.
117  */
118 int sd_sprite_rgba_decode(
119     sd_rgba_image *dst,
120     const sd_sprite *src,
121     const sd_palette *pal,
122     int remapping);
123 
124 /*! \brief Decode sprite to VGA image format.
125  *
126  * Decodes the sprite image to VGA image format.
127  *
128  * Note! The output VGA image will be created here. If the image had been
129  * already created by using sd_vga_image_create() previously, there may
130  * potentially be a memory leak, since the old image internals will not be freed.
131  *
132  * \retval SD_OUT_OF_MEMORY Memory ran out. Any output should be considered invalid and freed.
133  * \retval SD_INVALID_INPUT Dst or src was NULL.
134  * \retval SD_SUCCESS Success.
135  *
136  * \param dst Destination VGA image struct pointer.
137  * \param src Source sprite image pointer
138  */
139 int sd_sprite_vga_decode(
140     sd_vga_image *dst,
141     const sd_sprite *src);
142 
143 /*! \brief Encode sprite from VGA image format.
144  *
145  * Encodes a VGA image to sprite format
146  *
147  * \retval SD_OUT_OF_MEMORY Memory ran out. Any output should be considered invalid and freed.
148  * \retval SD_INVALID_INPUT Dst or src was NULL.
149  * \retval SD_SUCCESS Success.
150  *
151  * \param dst Destination Sprite image struct pointer.
152  * \param src Source VGA image pointer
153  */
154 int sd_sprite_vga_encode(
155     sd_sprite *dst,
156     const sd_vga_image *src);
157 
158 #ifdef SD_USE_INTERNAL
159 int sd_sprite_load(sd_reader *reader, sd_sprite *sprite);
160 int sd_sprite_save(sd_writer *writer, const sd_sprite *sprite);
161 #endif
162 
163 #ifdef __cplusplus
164 }
165 #endif
166 
167 #endif // _SD_SPRITE_H
168