Huge rewrite of the file loading/saving system. Normally safer in case of problem. Added incremental safety backups at regular intervals in the 'application data' directory (Windows) or $HOME/.grafx2 (unix). Keeps 8 files, saves every 30-60s and/or every 10-30 clicks.

git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@1245 416bcca6-2ee7-4201-b75f-2eb2f807beb1
This commit is contained in:
Yves Rizoud
2010-01-13 02:09:47 +00:00
parent c93a9bd1c1
commit 8bdd163ede
14 changed files with 1903 additions and 1480 deletions

View File

@@ -24,30 +24,108 @@
/// Also handles showing the preview in fileselectors.
//////////////////////////////////////////////////////////////////////////////
#ifndef __LOADSAVE_H__
#define __LOADSAVE_H__
#include <stdio.h>
enum CONTEXT_TYPE {
CONTEXT_MAIN_IMAGE,
CONTEXT_BRUSH,
CONTEXT_PREVIEW,
};
typedef struct
{
/// Kind of context. Internally used to differentiate the "sub-classes"
enum CONTEXT_TYPE Type;
// File properties
char * File_name;
char * File_directory;
byte Format;
// Image properties
T_Palette Palette;
short Width;
short Height;
byte Nb_layers;
char Comment[COMMENT_SIZE+1];
short Transparent_color;
/// Pixel ratio of the image
enum PIXEL_RATIO Ratio;
/// Load/save address of first pixel
byte *Target_address;
/// Pitch: Difference of addresses between one pixel and the one just "below" it
long Pitch;
/// Internal: Used to mark truecolor images on loading. Only used by preview.
//byte Is_truecolor;
/// Internal: Temporary RGB buffer when loading 24bit images
T_Components *Buffer_image_24b;
/// Internal: Temporary buffer when saving the flattened copy of something
byte *Buffer_image;
// Internal: working data for preview case
short Preview_factor_X;
short Preview_factor_Y;
short Preview_pos_X;
short Preview_pos_Y;
} T_IO_Context;
/// Type of a function that can be called for a T_IO_Context. Kind of a method.
typedef void (* Func_IO) (T_IO_Context *);
/*
void Pixel_load_in_current_screen (word x_pos, word y_pos, byte color);
void Pixel_load_in_preview (word x_pos, word y_pos, byte color);
void Pixel_load_in_brush (word x_pos, word y_pos, byte color);
*/
void Get_full_filename(char * filename, byte is_colorix_format);
// Setup for loading a preview in fileselector
void Init_context_preview(T_IO_Context * context, char *file_name, char *file_directory);
// Setup for loading/saving the current main image
void Init_context_layered_image(T_IO_Context * context, char *file_name, char *file_directory);
// Setup for loading/saving the flattened version of current main image
void Init_context_flat_image(T_IO_Context * context, char *file_name, char *file_directory);
// Setup for loading/saving the user's brush
void Init_context_brush(T_IO_Context * context, char *file_name, char *file_directory);
// Setup for saving an arbitrary undo/redo step, from either the main or spare page.
void Init_context_history_step(T_IO_Context * context, T_Page *page);
// Cleans up resources (currently: the 24bit buffer)
void Destroy_context(T_IO_Context *context);
///
/// High-level picture loading function.
/// Handles loading an image or a brush, or previewing only.
/// @param image true if the fileselector is the one for loading images (not brush)
void Load_image(byte image);
void Load_image(T_IO_Context *context);
///
/// High-level picture saving function.
/// @param image true if the image should be saved (instead of the brush)
void Save_image(byte image);
void Save_image(T_IO_Context *context);
///
/// Checks if there are any pending safety backups, and then opens them.
/// Returns 0 if there were none
/// Returns non-zero if some backups were loaded.
int Check_recovery(void);
/// Remove safety backups. Need to call on normal program exit.
void Delete_safety_backups(void);
/// Data for an image file format.
typedef struct {
byte Identifier; ///< Identifier for this format in enum :FILE_FORMATS
char *Label; ///< Five-letter label
Func_action Test; ///< Function which tests if the file is of this format
Func_action Load; ///< Function which loads an image of this format
Func_action Save; ///< Function which saves an image of this format
Func_IO Test; ///< Function which tests if the file is of this format
Func_IO Load; ///< Function which loads an image of this format
Func_IO Save; ///< Function which saves an image of this format
byte Palette_only; ///< Boolean, true if this format saves/loads only the palette.
byte Comment; ///< This file format allows a text comment
byte Supports_layers; ///< Boolean, true if this format preserves layers on saving
@@ -63,8 +141,10 @@ extern T_Format File_formats[];
/// called in case of SIGSEGV.
void Image_emergency_backup(void);
/*
/// Pixel ratio of last loaded image: one of :PIXEL_SIMPLE, :PIXEL_WIDE or :PIXEL_TALL
extern enum PIXEL_RATIO Ratio_of_loaded_image;
*/
T_Format * Get_fileformat(byte format);
@@ -77,6 +157,25 @@ T_Format * Get_fileformat(byte format);
#define NB_KNOWN_FORMATS 18 ///< Total number of known file formats.
#endif
// Internal use
/// Generic allocation and similar stuff, done at beginning of image load, as soon as size is known.
void Pre_load(T_IO_Context *context, short width, short height, long file_size, int format, enum PIXEL_RATIO ratio, byte truecolor);
/// Remaps the window. To call after palette (last) changes.
void Remap_fileselector(T_IO_Context *context);
/// Generic cleanup done on end of loading (ex: color-conversion from the temporary 24b buffer)
//void Post_load(T_IO_Context *context);
/// Query the color of a pixel (to save)
byte Get_pixel(T_IO_Context *context, short x, short y);
/// Set the color of a pixel (on load)
void Set_pixel(T_IO_Context *context, short x, short y, byte c);
/// Set the color of a 24bit pixel (on load)
void Set_pixel_24b(T_IO_Context *context, short x, short y, byte r, byte g, byte b);
/// Function to call when need to switch layers.
void Set_layer(T_IO_Context *context, byte layer);
// =================================================================
// What follows here are the definitions of functions and data
// useful for fileformats.c, miscfileformats.c etc.
@@ -96,6 +195,7 @@ typedef struct
// Data for 24bit loading
/*
typedef void (* Func_24b_display) (short,short,byte,byte,byte);
extern int Image_24b;
@@ -104,14 +204,16 @@ extern Func_24b_display Pixel_load_24b;
void Init_preview_24b(short width,short height,long size,int format);
void Pixel_load_in_24b_preview(short x_pos,short y_pos,byte r,byte g,byte b);
*/
//
void Set_file_error(int value);
void Init_preview(short width,short height,long size,int format,enum PIXEL_RATIO ratio);
/*
void Init_preview(short width,short height,long size,int format,enum PIXEL_RATIO ratio);
*/
void Init_write_buffer(void);
void Write_one_byte(FILE *file, byte b);
void End_write(FILE *file);
void Remap_fileselector(void);
#endif