1 /* vim:expandtab:ts=2 sw=2:
2 */
3 /*  Grafx2 - The Ultimate 256-color bitmap paint program
4 
5 	Copyright owned by various GrafX2 authors, see COPYRIGHT.txt for details.
6 
7     Grafx2 is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     as published by the Free Software Foundation; version 2
10     of the License.
11 
12     Grafx2 is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with Grafx2; if not, see <http://www.gnu.org/licenses/>
19 */
20 //////////////////////////////////////////////////////////////////////////////
21 ///@file loadsave.h
22 /// Saving and loading different picture formats.
23 /// Also handles showing the preview in fileselectors.
24 //////////////////////////////////////////////////////////////////////////////
25 
26 #ifndef __LOADSAVE_H__
27 #define __LOADSAVE_H__
28 
29 #include <stdio.h>
30 #include "gfx2surface.h"
31 
32 enum CONTEXT_TYPE {
33   CONTEXT_MAIN_IMAGE,
34   CONTEXT_BRUSH,
35   CONTEXT_PREVIEW,
36   CONTEXT_PREVIEW_PALETTE,
37   CONTEXT_SURFACE,
38   CONTEXT_PALETTE
39 };
40 
41 /// Data for a cycling color series. Heavily cloned from T_Gradient_array.
42 typedef struct
43 {
44   byte Start;    ///< First color
45   byte End;      ///< Last color
46   byte Inverse;  ///< Boolean, true if the gradient goes in descending order
47   byte Speed;    ///< Frequency of cycling, from 1 (slow) to COLOR_CYCLING_SPEED_MAX (fast)
48 } T_Color_cycle;
49 
50 typedef struct
51 {
52   /// Kind of context. Internally used to differentiate the "sub-classes"
53   enum CONTEXT_TYPE Type;
54 
55   // File properties
56 
57   char * File_name;         ///< File name in UTF-8 (or short ASCII file nmae under win32)
58   word * File_name_unicode; ///< Wide character version of the filename
59   char * File_directory;    ///< Directory. If NULL File_name should be the full path name
60   byte Format;
61 
62   // Image properties
63 
64   T_Palette Palette;
65   short Width;
66   short Height;
67   short Original_width;   /// Size of the whole image in case of PREVIEW of a thumbnail
68   short Original_height;
69   int  Nb_layers;
70   char Comment[COMMENT_SIZE+1];
71   byte Background_transparent;
72   byte Transparent_color;
73   byte bpp;
74   /// Pixel ratio of the image
75   enum PIXEL_RATIO Ratio;
76 
77   /// Load/save address of first pixel
78   byte *Target_address;
79   /// Pitch: Difference of addresses between one pixel and the one just "below" it
80   long Pitch;
81 
82   /// Original file name, stored in GIF file
83   char * Original_file_name;
84   /// Original file directory, stored in GIF file
85   char * Original_file_directory;
86 
87   byte Color_cycles;
88   T_Color_cycle Cycle_range[16];
89 
90   /// Internal: during load, marks which layer is being loaded.
91   int Current_layer;
92 
93   /// Internal: Used to mark truecolor images on loading. Only used by preview.
94   //byte Is_truecolor;
95   /// Internal: Temporary RGB buffer when loading 24bit images
96   T_Components *Buffer_image_24b;
97 
98   /// Internal: Temporary buffer when saving the flattened copy of something
99   byte *Buffer_image;
100 
101   // Internal: working data for preview case
102   short Preview_factor_X;
103   short Preview_factor_Y;
104   short Preview_pos_X;
105   short Preview_pos_Y;
106   byte *Preview_bitmap;
107   byte  Preview_usage[256];
108 
109   // Internal: returned surface for Surface case
110   T_GFX2_Surface * Surface;
111 
112 } T_IO_Context;
113 
114 #define PREVIEW_WIDTH  120
115 #define PREVIEW_HEIGHT  80
116 
117 /// Type of a function that can be called for a T_IO_Context. Kind of a method.
118 typedef void (* Func_IO_Test) (T_IO_Context *, FILE *);
119 typedef void (* Func_IO) (T_IO_Context *);
120 
121 /*
122 void Pixel_load_in_current_screen (word x_pos, word y_pos, byte color);
123 void Pixel_load_in_preview (word x_pos, word y_pos, byte color);
124 void Pixel_load_in_brush (word x_pos, word y_pos, byte color);
125 */
126 
127 // Setup for loading a preview in fileselector
128 void Init_context_preview(T_IO_Context * context, const char *file_name, const char *file_directory);
129 // Setup for loading/saving the current main image
130 void Init_context_layered_image(T_IO_Context * context, const char *file_name, const char *file_directory);
131 // Setup for loading/saving an intermediate backup
132 void Init_context_backup_image(T_IO_Context * context, const char *file_name, const char *file_directory);
133 // Setup for loading/saving the flattened version of current main image
134 void Init_context_flat_image(T_IO_Context * context, const char *file_name, const char *file_directory);
135 // Setup for loading/saving the user's brush
136 void Init_context_brush(T_IO_Context * context, const char *file_name, const char *file_directory);
137 // Setup for saving an arbitrary undo/redo step, from either the main or spare page.
138 void Init_context_history_step(T_IO_Context * context, T_Page *page);
139 // Setup for loading an image into a new GFX2 surface.
140 void Init_context_surface(T_IO_Context * context, const char *file_name, const char *file_directory);
141 
142 // Cleans up resources (currently: the 24bit buffer)
143 void Destroy_context(T_IO_Context *context);
144 
145 ///
146 /// High-level picture loading function.
147 void Load_image(T_IO_Context *context);
148 
149 ///
150 /// High-level picture saving function.
151 void Save_image(T_IO_Context *context);
152 
153 ///
154 /// Checks if there are any pending safety backups, and then opens them.
155 /// Returns 0 if there were none
156 /// Returns non-zero if some backups were loaded.
157 int Check_recovery(void);
158 
159 /// Makes a safety backup periodically.
160 void Rotate_safety_backups(void);
161 
162 /// Remove safety backups. Need to call on normal program exit.
163 void Delete_safety_backups(void);
164 
165 /// Data for an image file format.
166 typedef struct {
167   enum FILE_FORMATS Identifier; ///< Identifier for this format
168   const char *Label;       ///< Five-letter label
169   Func_IO_Test Test;       ///< Function which tests if the file is of this format
170   Func_IO Load;            ///< Function which loads an image of this format
171   Func_IO Save;            ///< Function which saves an image of this format
172   byte Palette_only;       ///< Boolean, true if this format saves/loads only the palette.
173   byte Comment;            ///< This file format allows a text comment
174   byte Supports_layers;    ///< Boolean, true if this format preserves layers on saving
175   const char *Default_extension; ///< Default file extension
176   const char *Extensions;  ///< List of semicolon-separated file extensions
177 } T_Format;
178 
179 /// Array of the known file formats
180 extern const T_Format File_formats[];
181 
182 ///
183 /// Function which attempts to save backups of the images (main and spare),
184 /// called in case of SIGSEGV.
185 /// It will save an image only if it has just one layer... otherwise,
186 /// the risk of flattening a layered image (or saving just one detail layer)
187 /// is too high.
188 void Image_emergency_backup(void);
189 
190 ///
191 /// Load an arbitrary Surface.
192 /// @param filename file to load.
193 /// @param directory path of the file to load. if NULL, filename have to be a full path name
194 /// @param gradients Pass the address of a target T_Gradient_array if you want the gradients, NULL otherwise
195 T_GFX2_Surface * Load_surface(const char *filename, const char *directory, T_Gradient_array *gradients);
196 
197 
198 /*
199 /// Pixel ratio of last loaded image: one of :PIXEL_SIMPLE, :PIXEL_WIDE or :PIXEL_TALL
200 extern enum PIXEL_RATIO Ratio_of_loaded_image;
201 */
202 
203 const T_Format * Get_fileformat(byte format);
204 
205 // -- File formats
206 
207 /// Total number of known file formats
208 unsigned int Nb_known_formats(void);
209 
210 // Internal use
211 
212 /// Generic allocation and similar stuff, done at beginning of image load, as soon as size is known.
213 void Pre_load(T_IO_Context *context, short width, short height, long file_size, int format, enum PIXEL_RATIO ratio, byte bpp);
214 /// Fill the entire current layer/frame of an image being loaded with a color.
215 void Fill_canvas(T_IO_Context *context, byte color);
216 
217 /// Query the color of a pixel (to save)
218 byte Get_pixel(T_IO_Context *context, short x, short y);
219 /// Set the color of a pixel (on load)
220 void Set_pixel(T_IO_Context *context, short x, short y, byte c);
221 /// Set the color of a 24bit pixel (on load)
222 void Set_pixel_24b(T_IO_Context *context, short x, short y, byte r, byte g, byte b);
223 /// Function to call when need to switch layers.
224 void Set_loading_layer(T_IO_Context *context, int layer);
225 /// Function to call when need to switch layers.
226 void Set_saving_layer(T_IO_Context *context, int layer);
227 /// Function to call when loading an image's duration
228 void Set_frame_duration(T_IO_Context *context, int duration);
229 /// Function to call to get an image's duration for saving
230 int Get_frame_duration(T_IO_Context *context);
231 /// Function to set a specific image mode
232 void Set_image_mode(T_IO_Context *context, enum IMAGE_MODES mode);
233 /// get the current image mode
234 enum IMAGE_MODES Get_image_mode(T_IO_Context *context);
235 
236 // =================================================================
237 // What follows here are the definitions of functions and data
238 // useful for fileformats.c, miscfileformats.c etc.
239 // =================================================================
240 
241 // This is here and not in fileformats.c because the emergency save uses it...
242 typedef struct
243 {
244   byte Filler1[6];
245   word Width;
246   word Height;
247   byte Filler2[118];
248   T_Palette Palette;
249 } T_IMG_Header;
250 
251 // Data for 24bit loading
252 
253 /*
254 typedef void (* Func_24b_display) (short,short,byte,byte,byte);
255 
256 extern int Image_24b;
257 extern T_Components * Buffer_image_24b;
258 extern Func_24b_display Pixel_load_24b;
259 
260 void Init_preview_24b(short width,short height,long size,int format);
261 void Pixel_load_in_24b_preview(short x_pos,short y_pos,byte r,byte g,byte b);
262 */
263 //
264 
265 /*
266 void Init_preview(short width,short height,long size,int format,enum PIXEL_RATIO ratio);
267 */
268 
269 #endif
270