From 1ccdf24dfa83ca6e01153950640ee875b75ebb6a Mon Sep 17 00:00:00 2001 From: Yves Rizoud Date: Thu, 20 Aug 2009 23:13:41 +0000 Subject: [PATCH] Brush container (Issue 135) now working with color brushes too. git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@1001 416bcca6-2ee7-4201-b75f-2eb2f807beb1 --- brush.c | 138 ++++++++++++++++++++++++++++++------------------------ brush.h | 8 ++++ buttons.c | 64 +++++++++++++++++++++---- 3 files changed, 140 insertions(+), 70 deletions(-) diff --git a/brush.c b/brush.c index 54d0f323..c6fe95ca 100644 --- a/brush.c +++ b/brush.c @@ -124,7 +124,7 @@ void Display_paintbrush(short x,short y,byte color,byte is_preview) byte * temp; if (is_preview==0 || Mouse_K==0) // pas de curseur si on est en preview et - // en train de cliquer + // en train de cliquer switch (Paintbrush_shape) { case PAINTBRUSH_SHAPE_POINT : // !!! TOUJOURS EN PREVIEW !!! @@ -446,12 +446,12 @@ void Display_paintbrush(short x,short y,byte color,byte is_preview) temp_color=Read_pixel_from_current_screen(x_pos,y_pos); position=(counter_y*Smear_brush_width)+counter_x; if ( (Paintbrush_sprite[(MAX_PAINTBRUSH_SIZE*counter_y)+counter_x] != 0) - // Le pinceau sert de masque pour dire quels pixels on doit traiter dans le rectangle - && (counter_y=Smear_min_Y) && (counter_x>=Smear_min_X) - // On clippe l'effet smear entre Smear_Min et Smear_Max - ) - Display_pixel(x_pos,y_pos,Smear_brush[position]); + // Le pinceau sert de masque pour dire quels pixels on doit traiter dans le rectangle + && (counter_y=Smear_min_Y) && (counter_x>=Smear_min_X) + // On clippe l'effet smear entre Smear_Min et Smear_Max + ) + Display_pixel(x_pos,y_pos,Smear_brush[position]); Smear_brush[position]=temp_color; } Update_part_of_screen(start_x, start_y, width, height); @@ -477,6 +477,69 @@ void Display_paintbrush(short x,short y,byte color,byte is_preview) } } +/// +/// Changes the Brush size, discarding its previous content. +/// @return 0 OK, 1 Failed +byte Realloc_brush(word new_brush_width, word new_brush_height) +{ + byte return_code=0; + + if ( (((long)Brush_height)*Brush_width) != + (((long)new_brush_height)*new_brush_width) ) + { + free(Brush); + Brush=(byte *)malloc(((long)new_brush_height)*new_brush_width); + if (Brush == NULL) + { + Error(0); + return_code=1; + + Brush=(byte *)malloc(1*1); + if(Brush == NULL) + { + Error(ERROR_MEMORY); + exit(ERROR_MEMORY); + } + new_brush_height=new_brush_width=1; + *Brush=Fore_color; + } + } + Brush_width=new_brush_width; + Brush_height=new_brush_height; + + free(Smear_brush); + Smear_brush_width=(Brush_width>MAX_PAINTBRUSH_SIZE)?Brush_width:MAX_PAINTBRUSH_SIZE; + Smear_brush_height=(Brush_height>MAX_PAINTBRUSH_SIZE)?Brush_height:MAX_PAINTBRUSH_SIZE; + Smear_brush=(byte *)malloc(((long)Smear_brush_height)*Smear_brush_width); + + if (Smear_brush == NULL) // Failed to allocate the smear brush + { + Error(0); + return_code=1; + + free(Brush); + Brush=(byte *)malloc(1*1); + if(Brush == NULL) + { + Error(ERROR_MEMORY); + exit(ERROR_MEMORY); + } + Brush_height=1; + Brush_width=1; + + Smear_brush=(byte *)malloc(MAX_PAINTBRUSH_SIZE*MAX_PAINTBRUSH_SIZE); + if(Smear_brush == NULL) + { + Error(ERROR_MEMORY); + exit(ERROR_MEMORY); + } + Smear_brush_height=MAX_PAINTBRUSH_SIZE; + Smear_brush_width=MAX_PAINTBRUSH_SIZE; + } + return return_code; +} + + // -- Effacer le pinceau -- // // void Hide_paintbrush(short x,short y) @@ -655,56 +718,7 @@ void Capture_brush(short start_x,short start_y,short end_x,short end_y,short cle if (start_y+new_brush_height>Main_image_height) new_brush_height=Main_image_height-start_y; - if ( (((long)Brush_height)*Brush_width) != - (((long)new_brush_height)*new_brush_width) ) - { - free(Brush); - Brush=(byte *)malloc(((long)new_brush_height)*new_brush_width); - if (Brush == NULL) - { - Error(0); - - Brush=(byte *)malloc(1*1); - if(Brush == NULL) - { - Error(ERROR_MEMORY); - exit(ERROR_MEMORY); - } - new_brush_height=new_brush_width=1; - *Brush=Fore_color; - } - } - Brush_width=new_brush_width; - Brush_height=new_brush_height; - - free(Smear_brush); - Smear_brush_width=(Brush_width>MAX_PAINTBRUSH_SIZE)?Brush_width:MAX_PAINTBRUSH_SIZE; - Smear_brush_height=(Brush_height>MAX_PAINTBRUSH_SIZE)?Brush_height:MAX_PAINTBRUSH_SIZE; - Smear_brush=(byte *)malloc(((long)Smear_brush_height)*Smear_brush_width); - - if (Smear_brush == NULL) // On ne peut même pas allouer la brosse du smear! - { - Error(0); - - free(Brush); - Brush=(byte *)malloc(1*1); - if(Brush == NULL) - { - Error(ERROR_MEMORY); - exit(ERROR_MEMORY); - } - Brush_height=1; - Brush_width=1; - - Smear_brush=(byte *)malloc(MAX_PAINTBRUSH_SIZE*MAX_PAINTBRUSH_SIZE); - if(Smear_brush == NULL) - { - Error(ERROR_MEMORY); - exit(ERROR_MEMORY); - } - Smear_brush_height=MAX_PAINTBRUSH_SIZE; - Smear_brush_width=MAX_PAINTBRUSH_SIZE; - } + Realloc_brush(new_brush_width, new_brush_height); Copy_image_to_brush(start_x,start_y,Brush_width,Brush_height,Main_image_width); @@ -827,7 +841,7 @@ void Outline_brush(void) // On copie la brosse courante dans la nouvelle Copy_part_of_image_to_another(Brush, // source - 0, 0, Brush_width, Brush_height, Brush_width, + 0, 0, Brush_width, Brush_height, Brush_width, new_brush, // Destination 1, 1, width); @@ -858,8 +872,8 @@ void Outline_brush(void) } else if (state == 0) { - Pixel_in_brush(x_pos-1,y_pos,Fore_color); - state=1; + Pixel_in_brush(x_pos-1,y_pos,Fore_color); + state=1; } } // Cas du dernier pixel à droite de la ligne @@ -1088,7 +1102,7 @@ void Capture_brush_with_lasso(int vertices, short * points,short clear) Error(0); Brush=(byte *)malloc(1*1); - if(Brush==NULL) Error(ERROR_MEMORY); + if(Brush==NULL) Error(ERROR_MEMORY); new_brush_height=new_brush_width=1; *Brush=Fore_color; } @@ -1107,7 +1121,7 @@ void Capture_brush_with_lasso(int vertices, short * points,short clear) free(Brush); Brush=(byte *)malloc(1*1); - if(Brush==NULL) Error(ERROR_MEMORY); + if(Brush==NULL) Error(ERROR_MEMORY); Brush_height=1; Brush_width=1; diff --git a/brush.h b/brush.h index 02206059..437c43cc 100644 --- a/brush.h +++ b/brush.h @@ -105,4 +105,12 @@ void Nibble_brush(void); */ void Capture_brush_with_lasso(int vertices, short * points,short clear); + +/// +/// Changes the Brush size, discarding its previous content. +/// @return 0 OK, 1 Failed +byte Realloc_brush(word new_brush_width, word new_brush_height); + + + #endif diff --git a/buttons.c b/buttons.c index bcf9e1b4..d645acd5 100644 --- a/buttons.c +++ b/buttons.c @@ -5636,7 +5636,7 @@ void Button_Text() void Display_stored_brush_in_window(word x_pos,word y_pos,int index) { - if (Brush_container[index].Paintbrush_shape <= PAINTBRUSH_SHAPE_MISC) + if (Brush_container[index].Paintbrush_shape < PAINTBRUSH_SHAPE_MAX) { int x,y; int offset_x=0, offset_y=0; @@ -5655,14 +5655,19 @@ void Display_stored_brush_in_window(word x_pos,word y_pos,int index) // Draw up to 16x16 for (y=0; yBRUSH_CONTAINER_PREVIEW_WIDTH) brush_offset_x = (Paintbrush_width-BRUSH_CONTAINER_PREVIEW_WIDTH)/2; @@ -5698,6 +5703,36 @@ void Store_brush(int index) // Re-init the rest Brush_container[index].Transp_color=0; } + if (Paintbrush_shape == PAINTBRUSH_SHAPE_COLOR_BRUSH || + Paintbrush_shape == PAINTBRUSH_SHAPE_MONO_BRUSH) + { + Brush_container[index].Brush=(byte *)malloc(Brush_width*Brush_height); + if (Brush_container[index].Brush) + { + Brush_container[index].Paintbrush_shape=Paintbrush_shape; + Brush_container[index].Width=Brush_width; + Brush_container[index].Height=Brush_height; + + memcpy(Brush_container[index].Brush, Brush,Brush_height*Brush_width); + + // Scale for preview + if (Brush_width>BRUSH_CONTAINER_PREVIEW_WIDTH || + Brush_height>BRUSH_CONTAINER_PREVIEW_HEIGHT) + { + // Scale + Rescale(Brush, Brush_width, Brush_height, (byte *)(Brush_container[index].Thumbnail), BRUSH_CONTAINER_PREVIEW_WIDTH, BRUSH_CONTAINER_PREVIEW_HEIGHT, 0, 0); + } + else + { + // Direct copy + Copy_part_of_image_to_another(Brush, 0,0,Brush_width, Brush_height,Brush_width,(byte *)(Brush_container[index].Thumbnail),0,0,BRUSH_CONTAINER_PREVIEW_WIDTH); + } + } + else + { + Error(0); + } + } } byte Restore_brush(int index) @@ -5738,8 +5773,21 @@ byte Restore_brush(int index) Set_paintbrush_size(Paintbrush_width,Paintbrush_height); } } - + // Color brushes + if (shape == PAINTBRUSH_SHAPE_COLOR_BRUSH || + shape == PAINTBRUSH_SHAPE_MONO_BRUSH) + { + Paintbrush_shape=shape; + Realloc_brush(Brush_container[index].Width,Brush_container[index].Height); + // Realloc sets Brush_width and Brush_height to new size. + memcpy(Brush, Brush_container[index].Brush, Brush_height*Brush_width); + + Brush_offset_X=Brush_width>>1; + Brush_offset_Y=Brush_height>>1; + + } Change_paintbrush_shape(shape); + return 1; }