diff --git a/src/brush.c b/src/brush.c index bad40217..398cac01 100644 --- a/src/brush.c +++ b/src/brush.c @@ -494,10 +494,14 @@ byte Realloc_brush(word new_brush_width, word new_brush_height, byte *new_brush, { byte *new_smear_brush; + byte *new_brush_remapped; word new_smear_brush_width; word new_smear_brush_height; + byte new_brush_is_provided; - if (new_brush==NULL) + new_brush_is_provided = (new_brush!=NULL); + + if (!new_brush_is_provided) { new_brush=(byte *)malloc(((long)new_brush_height)*new_brush_width); if (new_brush == NULL) @@ -512,8 +516,8 @@ byte Realloc_brush(word new_brush_width, word new_brush_height, byte *new_brush, new_smear_brush_width=(new_brush_width>MAX_PAINTBRUSH_SIZE)?new_brush_width:MAX_PAINTBRUSH_SIZE; new_smear_brush_height=(new_brush_height>MAX_PAINTBRUSH_SIZE)?new_brush_height:MAX_PAINTBRUSH_SIZE; new_smear_brush=NULL; - if ( (((long)Brush_height)*Brush_width) != - (((long)new_brush_height)*new_brush_width) ) + if ( (((long)Smear_brush_height)*Smear_brush_width) != + (((long)new_smear_brush_width)*new_smear_brush_height) ) { new_smear_brush=(byte *)malloc(((long)new_smear_brush_height)*new_smear_brush_width); if (new_smear_brush == NULL) @@ -521,13 +525,33 @@ byte Realloc_brush(word new_brush_width, word new_brush_height, byte *new_brush, Error(0); if (old_brush) *old_brush=NULL; + if (!new_brush_is_provided) + free(new_brush); return 2; } } + new_brush_remapped=NULL; + if ( (((long)Brush_height)*Brush_width) != + (((long)new_brush_height)*new_brush_width) ) + { + new_brush_remapped=(byte *)malloc(((long)new_brush_height)*new_brush_width); + if (new_brush_remapped == NULL) + { + Error(0); + free(new_smear_brush); + if (old_brush) + *old_brush=NULL; + if (!new_brush_is_provided) + free(new_brush); + return 3; + } + } // All allocations successful: can replace globals Brush_width=new_brush_width; Brush_height=new_brush_height; + memcpy(Brush_original_palette, Main_palette,sizeof(T_Palette)); + Brush_original_back_color=Back_color; if (new_smear_brush) { @@ -539,12 +563,22 @@ byte Realloc_brush(word new_brush_width, word new_brush_height, byte *new_brush, // Save or free the old brush pixels if (old_brush) - *old_brush=Brush; + *old_brush=Brush_original_pixels; else free(old_brush); + Brush_original_pixels=new_brush; // Assign new brush - Brush=new_brush; - + if (new_brush_remapped) + { + free(Brush); + Brush=new_brush_remapped; + } + if (new_brush_is_provided) + { + // Copy from Brush_original_pixels to Brush, using the last defined colmap. + Remap_general_lowlevel(Brush_colormap,Brush_original_pixels,Brush,Brush_width,Brush_height,Brush_width); + //memcpy(Brush, Brush_original_pixels,(long)Brush_width*Brush_height); + } return 0; } @@ -742,6 +776,8 @@ void Capture_brush(short start_x,short start_y,short end_x,short end_y,short cle } Update_part_of_screen(start_x,start_y,Brush_width,Brush_height); } + // Copy without remap + memcpy(Brush_original_pixels, Brush, (long)Brush_height*Brush_width); // On centre la prise sur la brosse Brush_offset_X=(Brush_width>>1); @@ -759,10 +795,13 @@ void Rotate_90_deg(void) Error(0); return; } - Rotate_90_deg_lowlevel(old_brush,Brush,Brush_height,Brush_width); + Rotate_90_deg_lowlevel(old_brush,Brush_original_pixels,Brush_height,Brush_width); free(old_brush); + // Copy without remap + memcpy(Brush, Brush_original_pixels, (long)Brush_height*Brush_width); + // On centre la prise sur la brosse Brush_offset_X=(Brush_width>>1); Brush_offset_Y=(Brush_height>>1); @@ -773,34 +812,45 @@ void Remap_brush(void) { short x_pos; // Variable de balayage de la brosse short y_pos; // Variable de balayage de la brosse - byte used[256]; // Tableau de booléens "La couleur est utilisée" int color; // On commence par initialiser le tableau de booléens à faux for (color=0;color<=255;color++) - used[color]=0; + Brush_colormap[color]=0; // On calcule la table d'utilisation des couleurs for (y_pos=0;y_pos>1); @@ -997,7 +1049,9 @@ void Nibble_brush(void) } free(old_brush); - + // Copy without remap + memcpy(Brush_original_pixels, Brush, (long)Brush_height*Brush_width); + // On recentre la prise sur la brosse Brush_offset_X=(Brush_width>>1); Brush_offset_Y=(Brush_height>>1); @@ -1096,6 +1150,8 @@ void Capture_brush_with_lasso(int vertices, short * points,short clear) if (clear) Pixel_in_current_screen(x_pos,y_pos,Back_color,0); } + // Copy without remap + memcpy(Brush_original_pixels, Brush, (long)Brush_height*Brush_width); // On centre la prise sur la brosse Brush_offset_X=(Brush_width>>1); diff --git a/src/buttons.c b/src/buttons.c index cfeda7fe..731937a0 100644 --- a/src/buttons.c +++ b/src/buttons.c @@ -4991,7 +4991,11 @@ void Button_Text(void) Brush_offset_X=Brush_width>>1; Brush_offset_Y=Brush_height>>1; - + + // TODO: Import original palette + // Remap to current screen palette + Remap_brush(); + // Fermeture Close_window(); Unselect_button(BUTTON_TEXT); @@ -5283,8 +5287,9 @@ byte Restore_brush(int index) if (!Realloc_brush(Brush_container[index].Width,Brush_container[index].Height,NULL,NULL)) { // Realloc sets Brush_width and Brush_height to new size. - memcpy(Brush, Brush_container[index].Brush, Brush_height*Brush_width); - + memcpy(Brush_original_pixels, Brush_container[index].Brush, (long)Brush_height*Brush_width); + // Copy without remap + memcpy(Brush, Brush_original_pixels, (long)Brush_height*Brush_width); Brush_offset_X=Brush_width>>1; Brush_offset_Y=Brush_height>>1; } diff --git a/src/buttons_effects.c b/src/buttons_effects.c index 91e4fa00..2ab0bfdc 100644 --- a/src/buttons_effects.c +++ b/src/buttons_effects.c @@ -1082,6 +1082,8 @@ void Button_Sieve_menu(void) for (x_pos=0; x_pos>1); Brush_offset_Y=(Brush_height>>1); diff --git a/src/factory.c b/src/factory.c index fadea6a4..1a4bce26 100644 --- a/src/factory.c +++ b/src/factory.c @@ -165,9 +165,9 @@ int L_SetBrushSize(lua_State* L) { return luaL_error(L, "setbrushsize: Resize failed"); } - Brush_was_altered=1; // Fill with Back_color + memset(Brush_original_pixels,Back_color,(long)Brush_width*Brush_height); memset(Brush,Back_color,(long)Brush_width*Brush_height); // Center the handle Brush_offset_X=(Brush_width>>1); diff --git a/src/global.h b/src/global.h index bf2ed001..3746126b 100644 --- a/src/global.h +++ b/src/global.h @@ -430,8 +430,16 @@ GFX2_GLOBAL T_List_of_pages * Spare_backups; // -- Brush data -/// Pixel data of the current brush. +/// Pixel data of the current brush (remapped). GFX2_GLOBAL byte * Brush; +/// Pixel data of the current brush (before remap). +GFX2_GLOBAL byte * Brush_original_pixels; +/// Palette of the brush, from when it was grabbed. +GFX2_GLOBAL T_Palette Brush_original_palette; +/// Back_color used when the brush was grabbed +GFX2_GLOBAL byte Brush_original_back_color; +/// Color mapping from ::Brush_original_pixels to ::Brush +GFX2_GLOBAL byte Brush_colormap[256]; /// X coordinate of the brush's "hot spot". It is < ::Brush_width GFX2_GLOBAL word Brush_offset_X; /// Y coordinate of the brush's "hot spot". It is < ::Brush_height diff --git a/src/graph.c b/src/graph.c index fba1d713..7f8288c0 100644 --- a/src/graph.c +++ b/src/graph.c @@ -681,7 +681,7 @@ void Remap_spare(void) // qui craint un peu, on peut faire l'échange dans la brosse de toutes les // teintes. for (layer=0; layerPages->Nb_layers; layer++) - Remap_general_lowlevel(used,Spare_backups->Pages->Image[layer],Spare_image_width,Spare_image_height,Spare_image_width); + Remap_general_lowlevel(used,Spare_backups->Pages->Image[layer],Spare_backups->Pages->Image[layer],Spare_image_width,Spare_image_height,Spare_image_width); // Change transparent color index Spare_backups->Pages->Transparent_color=used[Spare_backups->Pages->Transparent_color]; diff --git a/src/loadsave.c b/src/loadsave.c index fe6e9059..1867fe04 100644 --- a/src/loadsave.c +++ b/src/loadsave.c @@ -780,6 +780,8 @@ void Load_image(T_IO_Context *context) File_error=3; free(context->Buffer_image); } + memcpy(Brush_original_palette, context->Palette, sizeof(T_Palette)); + context->Buffer_image = NULL; } else if (context->Type == CONTEXT_SURFACE) diff --git a/src/main.c b/src/main.c index ff01d3c2..a5c22663 100644 --- a/src/main.c +++ b/src/main.c @@ -754,8 +754,11 @@ int Init_program(int argc,char * argv[]) // On initialise la brosse initiale à 1 pixel blanc: Brush_width=1; Brush_height=1; + for (temp=0;temp<256;temp++) + Brush_colormap[temp]=temp; Capture_brush(0,0,0,0,0); *Brush=MC_White; + *Brush_original_pixels=MC_White; // Test de recuperation de fichiers sauvés switch (Check_recovery()) diff --git a/src/misc.c b/src/misc.c index aaae4e61..6644c1fa 100644 --- a/src/misc.c +++ b/src/misc.c @@ -340,7 +340,7 @@ void Rotate_270_deg_lowlevel(byte * source, byte * dest, short width, short heig // Replace une couleur par une autre dans un buffer -void Remap_general_lowlevel(byte * conversion_table,byte * buffer,short width,short height,short buffer_width) +void Remap_general_lowlevel(byte * conversion_table,byte * in_buffer, byte *out_buffer,short width,short height,short buffer_width) { int dx,cx; @@ -350,10 +350,12 @@ void Remap_general_lowlevel(byte * conversion_table,byte * buffer,short width,sh // Pour chaque pixel for(cx=width;cx>0;cx--) { - *buffer = conversion_table[*buffer]; - buffer++; + *out_buffer = conversion_table[*in_buffer]; + in_buffer++; + out_buffer++; } - buffer += buffer_width-width; + in_buffer += buffer_width-width; + out_buffer += buffer_width-width; } } diff --git a/src/misc.h b/src/misc.h index 8948f23a..063125c8 100644 --- a/src/misc.h +++ b/src/misc.h @@ -33,7 +33,7 @@ #define SWAP_PBYTES(a,b) { byte * c=a; a=b; b=c;} void Copy_image_to_brush(short start_x,short start_y,short Brush_width,short Brush_height,word image_width); -void Remap_general_lowlevel(byte * conversion_table,byte * buffer,short width,short height,short buffer_width); +void Remap_general_lowlevel(byte * conversion_table,byte * in_buffer, byte *out_buffer,short width,short height,short buffer_width); void Scroll_picture(byte * main_src, byte * main_dest, short x_offset,short y_offset); void Wait_end_of_click(void); void Set_color(byte color, byte red, byte green, byte blue); diff --git a/src/palette.c b/src/palette.c index 253856ec..91bad838 100644 --- a/src/palette.c +++ b/src/palette.c @@ -250,12 +250,12 @@ void Remap_image_highlevel(byte * conversion_table) int layer; // Remap the flatenned image view - Remap_general_lowlevel(conversion_table,Main_screen, + Remap_general_lowlevel(conversion_table,Main_screen,Main_screen, Main_image_width,Main_image_height,Main_image_width); // Remap all layers for (layer=0; layerPages->Nb_layers; layer++) - Remap_general_lowlevel(conversion_table,Main_backups->Pages->Image[layer],Main_image_width,Main_image_height,Main_image_width); + Remap_general_lowlevel(conversion_table,Main_backups->Pages->Image[layer],Main_backups->Pages->Image[layer],Main_image_width,Main_image_height,Main_image_width); // Remap transparent color Main_backups->Pages->Transparent_color =