GrafX2 2.9.3227
The ultimate 256-color painting program
loadsave.c File Reference
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <png.h>
#include "gfx2log.h"
#include "gfx2mem.h"
#include "buttons.h"
#include "const.h"
#include "errors.h"
#include "global.h"
#include "keycodes.h"
#include "io.h"
#include "loadsave.h"
#include "loadsavefuncs.h"
#include "misc.h"
#include "osdep.h"
#include "graph.h"
#include "op_c.h"
#include "pages.h"
#include "palette.h"
#include "screen.h"
#include "struct.h"
#include "windows.h"
#include "engine.h"
#include "brush.h"
#include "setup.h"
#include "filesel.h"
#include "unicode.h"
#include "fileformats.h"
#include "bitcount.h"
+ Include dependency graph for loadsave.c:

Data Structures

struct  T_String_list
 

Functions

void Load_Recoil_Image (T_IO_Context *)
 
static void Load_ClipBoard_Image (T_IO_Context *)
 
static void Save_ClipBoard_Image (T_IO_Context *)
 
unsigned int Nb_known_formats (void)
 Total number of known file formats. More...
 
void Set_pixel (T_IO_Context *context, short x_pos, short y_pos, byte color)
 Set the color of a pixel (on load) More...
 
void Fill_canvas (T_IO_Context *context, byte color)
 Fill the entire current layer/frame of an image being loaded with a color. More...
 
void Set_pixel_24b (T_IO_Context *context, short x_pos, short y_pos, byte r, byte g, byte b)
 Chargement des pixels dans le buffer 24b. More...
 
void Set_palette_fake_24b (T_Palette palette)
 
void Set_frame_duration (T_IO_Context *context, int duration)
 Function to call when loading an image's duration. More...
 
int Get_frame_duration (T_IO_Context *context)
 Function to call to get an image's duration for saving. More...
 
void Set_image_mode (T_IO_Context *context, enum IMAGE_MODES mode)
 Function to set a specific image mode. More...
 
enum IMAGE_MODES Get_image_mode (T_IO_Context *context)
 get the current image mode More...
 
void Pre_load (T_IO_Context *context, short width, short height, long file_size, int format, enum PIXEL_RATIO ratio, byte bpp)
 Generic allocation and similar stuff, done at beginning of image load, as soon as size is known. More...
 
void Load_image (T_IO_Context *context)
 High-level picture loading function. More...
 
void Save_image (T_IO_Context *context)
 High-level picture saving function. More...
 
T_GFX2_SurfaceLoad_surface (const char *filename, const char *directory, T_Gradient_array *gradients)
 Load an arbitrary Surface. More...
 
void Emergency_backup (const char *fname, byte *source, int width, int height, T_Palette *palette)
 Saves an image. More...
 
void Image_emergency_backup ()
 Function which attempts to save backups of the images (main and spare), called in case of SIGSEGV. More...
 
const T_FormatGet_fileformat (byte format)
 
byte Get_pixel (T_IO_Context *context, short x, short y)
 Query the color of a pixel (to save) More...
 
void Destroy_context (T_IO_Context *context)
 Cleans up resources. More...
 
void Init_context_preview (T_IO_Context *context, const char *file_name, const char *file_directory)
 Setup for loading a preview in fileselector. More...
 
void Init_context_backup_image (T_IO_Context *context, const char *file_name, const char *file_directory)
 
void Init_context_layered_image (T_IO_Context *context, const char *file_name, const char *file_directory)
 Setup for loading/saving the current main image. More...
 
void Init_context_brush (T_IO_Context *context, const char *file_name, const char *file_directory)
 Setup for loading/saving the flattened version of current main image. More...
 
void Init_context_surface (T_IO_Context *context, const char *file_name, const char *file_directory)
 
void Set_saving_layer (T_IO_Context *context, int layer)
 Function to call when need to switch layers. More...
 
void Set_loading_layer (T_IO_Context *context, int layer)
 Function to call when need to switch layers. More...
 
static void Add_backup_file (const char *full_name, const char *file_name)
 Adds a file to Backups_main or Backups_spare lists, if it's a backup. More...
 
int String_compare (const void *a, const void *b)
 String comparer for sorting. More...
 
byte Process_backups (T_String_list **list)
 Reload safety backups, by loading several files in the right order. More...
 
int Check_recovery (void)
 Checks if there are any pending safety backups, and then opens them. More...
 
void Rotate_safety_backups (void)
 Makes a safety backup periodically. More...
 
void Delete_safety_backups (void)
 Remove safety backups. Need to call on normal program exit. More...
 

Variables

const T_Format File_formats []
 Array of the known file formats. More...
 
T_String_listBackups_main = NULL
 A list of files, used for scanning a directory. More...
 
T_String_listBackups_spare = NULL
 A list of files, used for scanning a directory. More...
 
const int Rotation_safety_backup = 8
 
const int Min_interval_for_safety_backup = 30000
 
const int Min_edits_for_safety_backup = 10
 
const int Max_interval_for_safety_backup = 60000
 
const int Max_edits_for_safety_backup = 30
 
byte Safety_backup_active = 0
 Global indicator that tells if the safety backup system is active. More...
 

Function Documentation

◆ Load_Recoil_Image()

◆ Load_ClipBoard_Image()

◆ Save_ClipBoard_Image()

◆ Nb_known_formats()

unsigned int Nb_known_formats ( void  )

Total number of known file formats.

References File_formats.

Referenced by Button_Load_or_Save(), Get_fileformat(), and Load_image().

◆ Set_pixel()

◆ Fill_canvas()

◆ Set_pixel_24b()

◆ Set_palette_fake_24b()

void Set_palette_fake_24b ( T_Palette  palette)

Referenced by Load_image().

◆ Set_frame_duration()

void Set_frame_duration ( T_IO_Context context,
int  duration 
)

◆ Get_frame_duration()

int Get_frame_duration ( T_IO_Context context)

Function to call to get an image's duration for saving.

References T_Document::backups, CONTEXT_MAIN_IMAGE, T_IO_Context::Current_layer, T_Image::Duration, T_Page::Image, Main, T_List_of_pages::Pages, and T_IO_Context::Type.

Referenced by Save_GIF().

◆ Set_image_mode()

◆ Get_image_mode()

enum IMAGE_MODES Get_image_mode ( T_IO_Context context)

◆ Pre_load()

void Pre_load ( T_IO_Context context,
short  width,
short  height,
long  file_size,
int  format,
enum PIXEL_RATIO  ratio,
byte  bpp 
)

Generic allocation and similar stuff, done at beginning of image load, as soon as size is known.

References T_IO_Context::Background_transparent, Backup_new_image(), T_IO_Context::bpp, T_IO_Context::Buffer_image, T_IO_Context::Buffer_image_24b, T_IO_Context::Comment, Compute_optimal_menu_colors(), Config, CONTEXT_BRUSH, CONTEXT_MAIN_IMAGE, CONTEXT_PALETTE, CONTEXT_PREVIEW, CONTEXT_PREVIEW_PALETTE, CONTEXT_SURFACE, T_Document::current_layer, File_error, T_IO_Context::Format, format, Get_fileformat(), GFX2_malloc, T_IO_Context::Height, T_Document::layers_visible, Main, T_Config::Maximize_preview, MC_Black, MC_Light, Menu_factor_X, Menu_factor_Y, Message_out_of_memory(), T_IO_Context::Nb_layers, New_GFX2_Surface(), Num2str, T_IO_Context::Original_height, T_IO_Context::Original_width, T_IO_Context::Palette, T_Format::Palette_only, T_IO_Context::Pitch, Pixel_ratio, PIXEL_TALL, PIXEL_TALL2, PIXEL_TALL3, PIXEL_WIDE, PIXEL_WIDE2, T_IO_Context::Preview_bitmap, T_IO_Context::Preview_factor_X, T_IO_Context::Preview_factor_Y, PREVIEW_HEIGHT, T_IO_Context::Preview_pos_X, T_IO_Context::Preview_pos_Y, PREVIEW_WIDTH, Print_in_window(), T_IO_Context::Ratio, Round_div_max(), Set_loading_layer(), T_IO_Context::Surface, T_IO_Context::Target_address, T_IO_Context::Transparent_color, T_IO_Context::Type, Update_window_area, T_IO_Context::Width, Window_pos_X, Window_pos_Y, and Window_rectangle().

Referenced by Load_2GS(), Load_BMP(), Load_C64(), Load_CA1(), Load_CEL(), Load_ClipBoard_Image(), Load_CM5(), Load_FLI(), Load_GIF(), Load_GOS(), Load_GPX(), Load_GRB(), Load_HGR(), Load_ICO(), Load_IFF(), Load_IMG(), Load_INFO(), Load_MOTO(), Load_MSX(), Load_NEO(), Load_PC1(), Load_PCX(), Load_PI1(), Load_PKM(), Load_PNG_Sub(), Load_PPH(), Load_PRG(), Load_Recoil_Image(), Load_SCR(), Load_SCx(), Load_SGX(), Load_TIFF_Sub(), and Load_TNY().

◆ Load_image()

void Load_image ( T_IO_Context context)

High-level picture loading function.

References T_Components::B, Back_color, T_IO_Context::Background_transparent, T_Page::Background_transparent, Backup_layers(), T_Document::backups, T_IO_Context::bpp, Brush_original_palette, T_IO_Context::Buffer_image, T_IO_Context::Buffer_image_24b, T_IO_Context::Color_cycles, T_IO_Context::Comment, T_Page::Comment, Compute_optimal_menu_colors(), Config, CONTEXT_BRUSH, CONTEXT_MAIN_IMAGE, CONTEXT_PALETTE, CONTEXT_PREVIEW, CONTEXT_PREVIEW_PALETTE, CONTEXT_SURFACE, Convert_24b_bitmap_to_256(), Count_used_colors(), T_Document::current_layer, Cursor_shape, CURSOR_SHAPE_HOURGLASS, T_IO_Context::Cycle_range, DEFAULT_FILEFORMAT, Display_cursor(), Download_infos_page_main(), T_Color_cycle::End, T_Gradient_range::End, Error, Favorite_GUI_color(), T_IO_Context::File_directory, T_Page::File_directory, File_error, File_formats, T_IO_Context::File_name, T_IO_Context::File_name_unicode, T_Document::fileformat, T_Page::Filename, T_Page::Filename_unicode, Flush_update(), T_IO_Context::Format, format, FORMAT_ALL_FILES, FORMAT_CLIPBOARD, T_Components::G, Get_fileformat(), GFX2_ERROR, GFX2_Log(), GFX2_WARNING, T_Page::Gradients, T_IO_Context::Height, Hide_cursor, T_Page::Image, T_Document::image_height, T_Document::image_is_modified, T_Page::Image_mode, IMAGE_MODE_ANIMATION, T_Document::image_width, T_Color_cycle::Inverse, T_Gradient_range::Inverse, LAYER_NONE, T_Document::layers_visible, Load_ClipBoard_Image(), Load_Recoil_Image(), Main, MC_Black, MC_Light, MC_Window, Menu_factor_X, Nb_known_formats(), T_IO_Context::Nb_layers, NULL, Open_file_read(), T_IO_Context::Original_file_directory, T_IO_Context::Original_file_name, T_List_of_pages::Pages, T_GFX2_Surface::palette, T_IO_Context::Palette, T_Page::Palette, T_Document::palette, T_Format::Palette_only, Pixel, Pixel_ratio, PIXEL_TALL, PIXEL_TALL2, PIXEL_TALL3, PIXEL_WIDE, PIXEL_WIDE2, T_GFX2_Surface::pixels, T_Image::Pixels, T_IO_Context::Preview_bitmap, T_IO_Context::Preview_factor_X, T_IO_Context::Preview_factor_Y, PREVIEW_HEIGHT, T_IO_Context::Preview_pos_X, T_IO_Context::Preview_pos_Y, T_IO_Context::Preview_usage, PREVIEW_WIDTH, Print_in_window(), T_Components::R, T_Gradient_array::Range, T_IO_Context::Ratio, Realloc_brush(), Remap_brush(), Remap_screen_after_menu_colors_change(), T_Config::Safety_colors, Set_palette(), Set_palette_fake_24b(), T_Color_cycle::Speed, T_Gradient_range::Speed, T_Color_cycle::Start, T_Gradient_range::Start, T_IO_Context::Surface, T_IO_Context::Transparent_color, T_Page::Transparent_color, T_IO_Context::Type, Unicode_strdup(), Update_window_area, T_IO_Context::Width, and Window_rectangle().

Referenced by Button_Load_or_Save(), Button_Reload(), Init_program(), Load_picture(), Load_surface(), Main_handler(), and Process_backups().

◆ Save_image()

◆ Load_surface()

T_GFX2_Surface * Load_surface ( const char *  filename,
const char *  directory,
T_Gradient_array gradients 
)

Load an arbitrary Surface.

Parameters
filenamefile to load.
directorypath of the file to load. if NULL, filename have to be a full path name
gradientsPass the address of a target T_Gradient_array if you want the gradients, NULL otherwise

References T_IO_Context::Color_cycles, T_IO_Context::Cycle_range, Destroy_context(), T_Color_cycle::End, T_Gradient_range::End, Init_context_surface(), T_Color_cycle::Inverse, T_Gradient_range::Inverse, Load_image(), NULL, T_Gradient_array::Range, T_Color_cycle::Speed, T_Gradient_range::Speed, T_Color_cycle::Start, T_Gradient_range::Start, and T_IO_Context::Surface.

Referenced by Define_icon(), Load_font(), Load_graphics(), and Render_text_SFont().

◆ Emergency_backup()

void Emergency_backup ( const char *  fname,
byte source,
int  width,
int  height,
T_Palette palette 
)

Saves an image.

This routine will only be called when all hope is lost, memory thrashed, etc It's the last chance to save anything, but the code has to be extremely careful, anything could happen. The chosen format is IMG since it's extremely simple, difficult to make it create an unusable image.

References Config_directory, File_error, Filepath_append_to_dir(), T_IMG_Header::Filler1, T_IMG_Header::Filler2, NULL, Write_byte(), Write_bytes(), and Write_word_le().

Referenced by Image_emergency_backup().

◆ Image_emergency_backup()

void Image_emergency_backup ( void  )

Function which attempts to save backups of the images (main and spare), called in case of SIGSEGV.

It will save an image only if it has just one layer... otherwise, the risk of flattening a layered image (or saving just one detail layer) is too high.

References BACKUP_FILE_EXTENSION, T_Document::backups, Emergency_backup(), T_Bitmap::Image, T_Document::image_height, T_Document::image_width, Main, Main_screen, T_Page::Nb_layers, T_List_of_pages::Pages, T_Document::palette, SAFETYBACKUP_PREFIX_A, SAFETYBACKUP_PREFIX_B, Spare, and T_Document::visible_image.

◆ Get_fileformat()

◆ Get_pixel()

◆ Destroy_context()

◆ Init_context_preview()

void Init_context_preview ( T_IO_Context context,
const char *  file_name,
const char *  file_directory 
)

Setup for loading a preview in fileselector.

References CONTEXT_PREVIEW, T_IO_Context::File_directory, T_IO_Context::File_name, NULL, and T_IO_Context::Type.

Referenced by Button_Load_or_Save().

◆ Init_context_backup_image()

void Init_context_backup_image ( T_IO_Context context,
const char *  file_name,
const char *  file_directory 
)

◆ Init_context_layered_image()

◆ Init_context_brush()

◆ Init_context_surface()

◆ Set_saving_layer()

◆ Set_loading_layer()

◆ Add_backup_file()

static void Add_backup_file ( const char *  full_name,
const char *  file_name 
)
static

Adds a file to Backups_main or Backups_spare lists, if it's a backup.

References Backups_main, Backups_spare, GFX2_malloc, Main, T_String_list::Next, T_Document::safety_backup_prefix, Spare, and T_String_list::String.

Referenced by Check_recovery(), and Delete_safety_backups().

◆ String_compare()

int String_compare ( const void *  a,
const void *  b 
)

String comparer for sorting.

Referenced by Process_backups().

◆ Process_backups()

◆ Check_recovery()

int Check_recovery ( void  )

Checks if there are any pending safety backups, and then opens them.

Returns
0 if no problem, -1 if the backup system cannot be activated, >=1 if some backups are restored

References Add_backup_file(), Backups_main, Backups_spare, Button_Page(), BUTTON_PAGE, Compute_limits(), Compute_paintbrush_coordinates(), Config_directory, Create_lock_file(), For_each_file(), Main, NULL, T_Document::offset_X, T_Document::offset_Y, Process_backups(), and Safety_backup_active.

Referenced by Init_program().

◆ Rotate_safety_backups()

◆ Delete_safety_backups()

void Delete_safety_backups ( void  )

Variable Documentation

◆ File_formats

const T_Format File_formats[]

Array of the known file formats.

Referenced by Button_Load_or_Save(), Get_fileformat(), Load_image(), and Nb_known_formats().

◆ Backups_main

T_String_list* Backups_main = NULL

A list of files, used for scanning a directory.

Referenced by Add_backup_file(), Check_recovery(), and Delete_safety_backups().

◆ Backups_spare

T_String_list* Backups_spare = NULL

A list of files, used for scanning a directory.

Referenced by Add_backup_file(), Check_recovery(), and Delete_safety_backups().

◆ Rotation_safety_backup

const int Rotation_safety_backup = 8

Referenced by Rotate_safety_backups().

◆ Min_interval_for_safety_backup

const int Min_interval_for_safety_backup = 30000

Referenced by Rotate_safety_backups().

◆ Min_edits_for_safety_backup

const int Min_edits_for_safety_backup = 10

Referenced by Rotate_safety_backups().

◆ Max_interval_for_safety_backup

const int Max_interval_for_safety_backup = 60000

Referenced by Rotate_safety_backups().

◆ Max_edits_for_safety_backup

const int Max_edits_for_safety_backup = 30

Referenced by Rotate_safety_backups().

◆ Safety_backup_active

byte Safety_backup_active = 0

Global indicator that tells if the safety backup system is active.

Referenced by Check_recovery(), Delete_safety_backups(), and Rotate_safety_backups().