Issue 285: For skins and fonts, replaced calls to SDL_image with our own image-loading functions. Also fixed the generic loader that was updating the screen palette even when not useful.

git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@1247 416bcca6-2ee7-4201-b75f-2eb2f807beb1
This commit is contained in:
Yves Rizoud 2010-01-15 01:48:33 +00:00
parent 3226961fb5
commit 84ac0df49c
9 changed files with 143 additions and 53 deletions

View File

@ -106,8 +106,7 @@ void Load_IMG(T_IO_Context * context)
if (File_error==0) if (File_error==0)
{ {
memcpy(context->Palette,IMG_header.Palette,sizeof(T_Palette)); memcpy(context->Palette,IMG_header.Palette,sizeof(T_Palette));
Set_palette(context->Palette); Palette_loaded(context);
Remap_fileselector(context);
context->Width=IMG_header.Width; context->Width=IMG_header.Width;
context->Height=IMG_header.Height; context->Height=IMG_header.Height;
@ -595,8 +594,7 @@ void Load_LBM(T_IO_Context * context)
if (Image_HAM) if (Image_HAM)
Adapt_palette_HAM(context); Adapt_palette_HAM(context);
Palette_64_to_256(context->Palette); Palette_64_to_256(context->Palette);
Set_palette(context->Palette); Palette_loaded(context);
Remap_fileselector(context);
// On lit l'octet de padding du CMAP si la taille est impaire // On lit l'octet de padding du CMAP si la taille est impaire
if (nb_colors&1) if (nb_colors&1)
@ -1160,8 +1158,7 @@ void Load_BMP(T_IO_Context * context)
context->Palette[index].G=local_palette[index][1]; context->Palette[index].G=local_palette[index][1];
context->Palette[index].B=local_palette[index][0]; context->Palette[index].B=local_palette[index][0];
} }
Set_palette(context->Palette); Palette_loaded(context);
Remap_fileselector(context);
context->Width=header.Width; context->Width=header.Width;
context->Height=header.Height; context->Height=header.Height;
@ -1811,7 +1808,6 @@ void Load_GIF(T_IO_Context * context)
for (color_index=0;color_index<nb_colors;color_index++) for (color_index=0;color_index<nb_colors;color_index++)
Read_byte(GIF_file,&(context->Palette[color_index].B)); Read_byte(GIF_file,&(context->Palette[color_index].B));
} }
Set_palette(context->Palette);
} }
// On lit un indicateur de block // On lit un indicateur de block
@ -1924,10 +1920,9 @@ void Load_GIF(T_IO_Context * context)
for (color_index=0;color_index<nb_colors;color_index++) for (color_index=0;color_index<nb_colors;color_index++)
Read_byte(GIF_file,&(context->Palette[color_index].B)); Read_byte(GIF_file,&(context->Palette[color_index].B));
} }
Set_palette(context->Palette);
} }
Remap_fileselector(context); Palette_loaded(context);
value_clr =nb_colors+0; value_clr =nb_colors+0;
value_eof =nb_colors+1; value_eof =nb_colors+1;
@ -2648,8 +2643,7 @@ void Load_PCX(T_IO_Context * context)
} }
} }
} }
Set_palette(context->Palette); Palette_loaded(context);
Remap_fileselector(context);
// Maintenant qu'on a lu la palette que ces crétins sont allés foutre // Maintenant qu'on a lu la palette que ces crétins sont allés foutre
// à la fin, on retourne juste après le header pour lire l'image. // à la fin, on retourne juste après le header pour lire l'image.
@ -3057,8 +3051,7 @@ void Load_SCx(T_IO_Context * context)
Palette_64_to_256(SCx_Palette); Palette_64_to_256(SCx_Palette);
memcpy(context->Palette,SCx_Palette,size); memcpy(context->Palette,SCx_Palette,size);
Set_palette(context->Palette); Palette_loaded(context);
Remap_fileselector(context);
context->Width=SCx_header.Width; context->Width=SCx_header.Width;
context->Height=SCx_header.Height; context->Height=SCx_header.Height;
@ -3384,10 +3377,10 @@ void Load_PNG(T_IO_Context * context)
} }
free(palette); free(palette);
} }
if (color_type != PNG_COLOR_TYPE_RGB && color_type != PNG_COLOR_TYPE_RGB_ALPHA) if (color_type != PNG_COLOR_TYPE_RGB && color_type != PNG_COLOR_TYPE_RGB_ALPHA)
{ {
Set_palette(context->Palette); Palette_loaded(context);
Remap_fileselector(context);
} }
context->Width=info_ptr->width; context->Width=info_ptr->width;
@ -3441,6 +3434,7 @@ void Load_PNG(T_IO_Context * context)
break; break;
case CONTEXT_MAIN_IMAGE: case CONTEXT_MAIN_IMAGE:
case CONTEXT_BRUSH: case CONTEXT_BRUSH:
case CONTEXT_SURFACE:
// It's loading an actual image // It's loading an actual image
// We'll save memory and time by writing directly into // We'll save memory and time by writing directly into
// our pre-allocated 24bit buffer // our pre-allocated 24bit buffer

BIN
gfx2.gif

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -74,7 +74,7 @@
; bar according to the resolution| d'outils suivant la résolution ; bar according to the resolution| d'outils suivant la résolution
; 2: Slightly adapt the ratio of | 2: Adapter légèrement les proportions ; 2: Slightly adapt the ratio of | 2: Adapter légèrement les proportions
; the menus and tool-bar | des menus et de la barre d'outils ; the menus and tool-bar | des menus et de la barre d'outils
Menu_ratio = 1 ; (default 1) Menu_ratio = 254 ; (default -2)
[FILE_SELECTOR] # [SELECTEUR_DE_FICHIERS] [FILE_SELECTOR] # [SELECTEUR_DE_FICHIERS]

4
init.c
View File

@ -819,7 +819,7 @@ T_Gui_skin * Load_graphics(const char * skin_file)
strcat(filename,"skins" PATH_SEPARATOR); strcat(filename,"skins" PATH_SEPARATOR);
strcat(filename,skin_file); strcat(filename,skin_file);
gui=IMG_Load(filename); gui=Load_surface(filename);
if (!gui) if (!gui)
{ {
sprintf(Gui_loading_error_message, "Unable to load the skin image (missing? not an image file?)\n"); sprintf(Gui_loading_error_message, "Unable to load the skin image (missing? not an image file?)\n");
@ -895,7 +895,7 @@ byte * Load_font(const char * font_name)
// Read the file containing the image // Read the file containing the image
sprintf(filename,"%sskins%s%s", Data_directory, PATH_SEPARATOR, font_name); sprintf(filename,"%sskins%s%s", Data_directory, PATH_SEPARATOR, font_name);
image=IMG_Load(filename); image=Load_surface(filename);
if (!image) if (!image)
{ {
sprintf(Gui_loading_error_message, "Unable to load the skin image (missing? not an image file?)\n"); sprintf(Gui_loading_error_message, "Unable to load the skin image (missing? not an image file?)\n");

5
io.c
View File

@ -274,12 +274,13 @@ void For_each_file(const char * directory_name, void Callback(const char *))
void Get_full_filename(char * output_name, char * file_name, char * directory_name) void Get_full_filename(char * output_name, char * file_name, char * directory_name)
{ {
strcpy(output_name,directory_name); strcpy(output_name,directory_name);
if (output_name[0] != '\0')
{
// Append a separator at the end of path, if there isn't one already. // Append a separator at the end of path, if there isn't one already.
// This handles the case of directory variables which contain one, // This handles the case of directory variables which contain one,
// as well as directories like "/" on Unix. // as well as directories like "/" on Unix.
if (output_name[strlen(output_name)-1]!=PATH_SEPARATOR[0]) if (output_name[strlen(output_name)-1]!=PATH_SEPARATOR[0])
strcat(output_name,PATH_SEPARATOR); strcat(output_name,PATH_SEPARATOR);
}
strcat(output_name,file_name); strcat(output_name,file_name);
} }

View File

@ -211,13 +211,31 @@ void Set_pixel(T_IO_Context *context, short x_pos, short y_pos, byte color)
break; break;
// Load pixels in a SDL_Surface
case CONTEXT_SURFACE:
if (x_pos>=0 && y_pos>=0 && x_pos<context->Surface->w && y_pos<context->Surface->h)
*(((byte *)(context->Surface->pixels)) + context->Surface->pitch * y_pos + x_pos) = color;
break;
} }
} }
void Remap_fileselector(T_IO_Context *context) void Palette_loaded(T_IO_Context *context)
{ {
// Update the current screen to the loaded palette
switch (context->Type)
{
case CONTEXT_MAIN_IMAGE:
case CONTEXT_PREVIEW:
Set_palette(context->Palette);
break;
case CONTEXT_BRUSH:
case CONTEXT_SURFACE:
break;
}
switch (context->Type) switch (context->Type)
{ {
case CONTEXT_PREVIEW: case CONTEXT_PREVIEW:
@ -268,10 +286,11 @@ void Remap_fileselector(T_IO_Context *context)
*/ */
Remap_screen_after_menu_colors_change(); Remap_screen_after_menu_colors_change();
break; break;
case CONTEXT_MAIN_IMAGE: case CONTEXT_MAIN_IMAGE:
case CONTEXT_BRUSH: case CONTEXT_BRUSH:
case CONTEXT_SURFACE:
break; break;
} }
} }
@ -288,6 +307,7 @@ void Set_pixel_24b(T_IO_Context *context, short x_pos, short y_pos, byte r, byte
{ {
case CONTEXT_MAIN_IMAGE: case CONTEXT_MAIN_IMAGE:
case CONTEXT_BRUSH: case CONTEXT_BRUSH:
case CONTEXT_SURFACE:
{ {
int index; int index;
@ -465,6 +485,17 @@ void Pre_load(T_IO_Context *context, short width, short height, long file_size,
context->Target_address=context->Buffer_image; context->Target_address=context->Buffer_image;
break; break;
case CONTEXT_SURFACE:
context->Surface = SDL_CreateRGBSurface(SDL_SWSURFACE|SDL_SRCCOLORKEY, width, height, 8, 0, 0, 0, 0);
if (! context->Surface)
{
File_error=1;
return;
}
//context->Pitch = context->Surface->pitch;
//context->Target_address = context->Surface->pixels;
break;
} }
if (File_error) if (File_error)
@ -479,6 +510,7 @@ void Pre_load(T_IO_Context *context, short width, short height, long file_size,
{ {
case CONTEXT_MAIN_IMAGE: case CONTEXT_MAIN_IMAGE:
case CONTEXT_BRUSH: case CONTEXT_BRUSH:
case CONTEXT_SURFACE:
// Allocate 24bit buffer // Allocate 24bit buffer
context->Buffer_image_24b= context->Buffer_image_24b=
(T_Components *)malloc(width*height*sizeof(T_Components)); (T_Components *)malloc(width*height*sizeof(T_Components));
@ -495,8 +527,7 @@ void Pre_load(T_IO_Context *context, short width, short height, long file_size,
case CONTEXT_PREVIEW: case CONTEXT_PREVIEW:
// Load palette // Load palette
Set_palette_fake_24b(context->Palette); Set_palette_fake_24b(context->Palette);
Set_palette(context->Palette); Palette_loaded(context);
Remap_fileselector(context);
break; break;
} }
} }
@ -635,7 +666,7 @@ void Load_image(T_IO_Context *context)
File_error=2; File_error=2;
else else
{ {
Set_palette(context->Palette); Palette_loaded(context);
} }
break; break;
@ -653,6 +684,11 @@ void Load_image(T_IO_Context *context)
// nothing to do // nothing to do
break; break;
case CONTEXT_SURFACE:
if (Convert_24b_bitmap_to_256(context->Surface->pixels,context->Buffer_image_24b,context->Width,context->Height,context->Palette))
File_error=1;
break;
} }
} }
free(context->Buffer_image_24b); free(context->Buffer_image_24b);
@ -727,6 +763,23 @@ void Load_image(T_IO_Context *context)
if (!Smear_brush) if (!Smear_brush)
File_error=3; File_error=3;
} }
else if (context->Type == CONTEXT_SURFACE)
{
if (File_error == 0)
{
// Copy the palette
SDL_Color colors[256];
int i;
for (i=0; i<256; i++)
{
colors[i].r=context->Palette[i].R;
colors[i].g=context->Palette[i].G;
colors[i].b=context->Palette[i].B;
}
SDL_SetColors(context->Surface, colors, 0, 256);
}
}
} }
@ -758,6 +811,9 @@ void Save_image(T_IO_Context *context)
case CONTEXT_PREVIEW: case CONTEXT_PREVIEW:
break; break;
case CONTEXT_SURFACE:
break;
} }
format = Get_fileformat(context->Format); format = Get_fileformat(context->Format);
@ -807,8 +863,6 @@ void Load_SDL_Image(T_IO_Context *context)
if (surface->format->palette) if (surface->format->palette)
{ {
Get_SDL_Palette(surface->format->palette, context->Palette); Get_SDL_Palette(surface->format->palette, context->Palette);
Set_palette(context->Palette);
Remap_fileselector(context);
} }
for (y_pos=0; y_pos<context->Height; y_pos++) for (y_pos=0; y_pos<context->Height; y_pos++)
@ -844,6 +898,24 @@ void Load_SDL_Image(T_IO_Context *context)
SDL_FreeSurface(surface); SDL_FreeSurface(surface);
} }
/// Load an arbitrary SDL_Surface.
SDL_Surface * Load_surface(char *full_name)
{
SDL_Surface * bmp=NULL;
T_IO_Context context;
Init_context_surface(&context, full_name, "");
Load_image(&context);
if (context.Surface)
bmp=context.Surface;
Destroy_context(&context);
return bmp;
}
/// Saves an image. /// Saves an image.
/// This routine will only be called when all hope is lost, memory thrashed, etc /// 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 /// It's the last chance to save anything, but the code has to be extremely
@ -1007,6 +1079,25 @@ void Init_context_brush(T_IO_Context * context, char *file_name, char *file_dire
} }
// Setup for loading an image into a new SDL surface.
void Init_context_surface(T_IO_Context * context, char *file_name, char *file_directory)
{
memset(context, 0, sizeof(T_IO_Context));
context->Type = CONTEXT_SURFACE;
context->File_name = file_name;
context->File_directory = file_directory;
context->Format = DEFAULT_FILEFORMAT;
// context->Palette
// context->Width
// context->Height
context->Nb_layers = 1;
context->Transparent_color=-1;
context->Ratio=PIXEL_SIMPLE;
//context->Target_address
//context->Pitch
}
/// Function to call when need to switch layers. /// Function to call when need to switch layers.
void Set_layer(T_IO_Context *context, byte layer) void Set_layer(T_IO_Context *context, byte layer)
{ {

View File

@ -33,6 +33,7 @@ enum CONTEXT_TYPE {
CONTEXT_MAIN_IMAGE, CONTEXT_MAIN_IMAGE,
CONTEXT_BRUSH, CONTEXT_BRUSH,
CONTEXT_PREVIEW, CONTEXT_PREVIEW,
CONTEXT_SURFACE,
}; };
@ -77,6 +78,9 @@ typedef struct
short Preview_pos_X; short Preview_pos_X;
short Preview_pos_Y; short Preview_pos_Y;
// Internal: returned surface for SDL_Surface case
SDL_Surface * Surface;
} T_IO_Context; } T_IO_Context;
/// Type of a function that can be called for a T_IO_Context. Kind of a method. /// Type of a function that can be called for a T_IO_Context. Kind of a method.
@ -98,6 +102,8 @@ void Init_context_flat_image(T_IO_Context * context, char *file_name, char *file
void Init_context_brush(T_IO_Context * context, char *file_name, char *file_directory); 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. // 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); void Init_context_history_step(T_IO_Context * context, T_Page *page);
// Setup for loading an image into a new SDL surface.
void Init_context_surface(T_IO_Context * context, char *file_name, char *file_directory);
// Cleans up resources (currently: the 24bit buffer) // Cleans up resources (currently: the 24bit buffer)
void Destroy_context(T_IO_Context *context); void Destroy_context(T_IO_Context *context);
@ -141,6 +147,10 @@ extern T_Format File_formats[];
/// called in case of SIGSEGV. /// called in case of SIGSEGV.
void Image_emergency_backup(void); void Image_emergency_backup(void);
/// Load an arbitrary SDL_Surface.
SDL_Surface * Load_surface(char *full_name);
/* /*
/// Pixel ratio of last loaded image: one of :PIXEL_SIMPLE, :PIXEL_WIDE or :PIXEL_TALL /// Pixel ratio of last loaded image: one of :PIXEL_SIMPLE, :PIXEL_WIDE or :PIXEL_TALL
extern enum PIXEL_RATIO Ratio_of_loaded_image; extern enum PIXEL_RATIO Ratio_of_loaded_image;
@ -162,7 +172,7 @@ T_Format * Get_fileformat(byte format);
/// Generic allocation and similar stuff, done at beginning of image load, as soon as size is known. /// 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); 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. /// Remaps the window. To call after palette (last) changes.
void Remap_fileselector(T_IO_Context *context); void Palette_loaded(T_IO_Context *context);
/// Generic cleanup done on end of loading (ex: color-conversion from the temporary 24b buffer) /// Generic cleanup done on end of loading (ex: color-conversion from the temporary 24b buffer)
//void Post_load(T_IO_Context *context); //void Post_load(T_IO_Context *context);

7
main.c
View File

@ -509,16 +509,19 @@ int Init_program(int argc,char * argv[])
char icon_path[MAX_PATH_CHARACTERS]; char icon_path[MAX_PATH_CHARACTERS];
SDL_Surface * icon; SDL_Surface * icon;
sprintf(icon_path, "%s%s", Data_directory, "gfx2.gif"); sprintf(icon_path, "%s%s", Data_directory, "gfx2.gif");
icon = IMG_Load(icon_path); icon = Load_surface(icon_path);
if (icon) if (icon)
{ {
byte *icon_mask; byte *icon_mask;
int x,y; int x,y;
Uint32 pink;
pink = SDL_MapRGB(icon->format, 255, 0, 255);
icon_mask=malloc(128); icon_mask=malloc(128);
memset(icon_mask,0,128); memset(icon_mask,0,128);
for (y=0;y<32;y++) for (y=0;y<32;y++)
for (x=0;x<32;x++) for (x=0;x<32;x++)
if (((byte *)(icon->pixels))[(y*32+x)] != 255) if (((byte *)(icon->pixels))[(y*32+x)] != pink)
icon_mask[(y*32+x)/8] |=0x80>>(x&7); icon_mask[(y*32+x)/8] |=0x80>>(x&7);
SDL_WM_SetIcon(icon,icon_mask); SDL_WM_SetIcon(icon,icon_mask);
free(icon_mask); free(icon_mask);

View File

@ -110,8 +110,7 @@ void Load_PAL(T_IO_Context * context)
{ {
Palette_64_to_256(palette_64); Palette_64_to_256(palette_64);
memcpy(context->Palette, palette_64, sizeof(T_Palette)); memcpy(context->Palette, palette_64, sizeof(T_Palette));
Set_palette(context->Palette); Palette_loaded(context);
Remap_fileselector(context);
// On dessine une preview de la palette (si chargement = preview) // On dessine une preview de la palette (si chargement = preview)
Draw_palette_preview(context); Draw_palette_preview(context);
@ -139,8 +138,7 @@ void Load_PAL(T_IO_Context * context)
context->Palette[i].G = g; context->Palette[i].G = g;
context->Palette[i].B = b; context->Palette[i].B = b;
Set_palette(context->Palette); Palette_loaded(context);
Remap_fileselector(context);
// On dessine une preview de la palette (si chargement = preview) // On dessine une preview de la palette (si chargement = preview)
Draw_palette_preview(context); Draw_palette_preview(context);
@ -378,8 +376,7 @@ void Load_PKM(T_IO_Context * context)
// Palette lue en 64 // Palette lue en 64
memcpy(context->Palette,header.Palette,sizeof(T_Palette)); memcpy(context->Palette,header.Palette,sizeof(T_Palette));
Palette_64_to_256(context->Palette); Palette_64_to_256(context->Palette);
Set_palette(context->Palette); Palette_loaded(context);
Remap_fileselector(context);
Compteur_de_donnees_packees=0; Compteur_de_donnees_packees=0;
Compteur_de_pixels=0; Compteur_de_pixels=0;
@ -1106,8 +1103,7 @@ void Load_KCF(T_IO_Context * context)
context->Palette[index].B=context->Palette[index+16].B; context->Palette[index].B=context->Palette[index+16].B;
} }
Set_palette(context->Palette); Palette_loaded(context);
Remap_fileselector(context);
} }
else else
File_error=1; File_error=1;
@ -1166,8 +1162,7 @@ void Load_KCF(T_IO_Context * context)
context->Palette[index].B=context->Palette[index+16].B; context->Palette[index].B=context->Palette[index+16].B;
} }
Set_palette(context->Palette); Palette_loaded(context);
Remap_fileselector(context);
} }
else else
File_error=1; File_error=1;
@ -1445,8 +1440,7 @@ void Load_PI1(T_IO_Context * context)
if (Config.Clear_palette) if (Config.Clear_palette)
memset(context->Palette,0,sizeof(T_Palette)); memset(context->Palette,0,sizeof(T_Palette));
PI1_decode_palette(buffer+2,(byte *)context->Palette); PI1_decode_palette(buffer+2,(byte *)context->Palette);
Set_palette(context->Palette); Palette_loaded(context);
Remap_fileselector(context);
context->Width=320; context->Width=320;
context->Height=200; context->Height=200;
@ -1771,8 +1765,7 @@ void Load_PC1(T_IO_Context * context)
if (Config.Clear_palette) if (Config.Clear_palette)
memset(context->Palette,0,sizeof(T_Palette)); memset(context->Palette,0,sizeof(T_Palette));
PI1_decode_palette(buffercomp+2,(byte *)context->Palette); PI1_decode_palette(buffercomp+2,(byte *)context->Palette);
Set_palette(context->Palette); Palette_loaded(context);
Remap_fileselector(context);
context->Width=320; context->Width=320;
context->Height=200; context->Height=200;
@ -1954,8 +1947,7 @@ void Load_NEO(T_IO_Context * context)
memset(context->Palette,0,sizeof(T_Palette)); memset(context->Palette,0,sizeof(T_Palette));
// on saute la résolution et le flag, chacun 2 bits // on saute la résolution et le flag, chacun 2 bits
PI1_decode_palette(buffer+4,(byte *)context->Palette); PI1_decode_palette(buffer+4,(byte *)context->Palette);
Set_palette(context->Palette); Palette_loaded(context);
Remap_fileselector(context);
context->Width=320; context->Width=320;
context->Height=200; context->Height=200;
@ -2224,8 +2216,7 @@ void Load_C64(T_IO_Context * context)
} }
memcpy(context->Palette,pal,48); // this set the software palette for grafx2 memcpy(context->Palette,pal,48); // this set the software palette for grafx2
Set_palette(context->Palette); // this set the hardware palette for SDL Palette_loaded(context); // Always call it if you change the palette
Remap_fileselector(context); // Always call it if you change the palette
if (file_size>9002) if (file_size>9002)
width=160; width=160;