From 40e022525bb8c6ddb034e2fd6aa52cbef8aae550 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Wed, 5 Dec 2018 12:23:28 +0100 Subject: [PATCH] convert image to HGR when enabling IMAGE_MODE_HGR --- src/buttons_effects.c | 85 +++++++++++++++++++++++++++++++++++++++++++ src/graph.c | 4 +- src/graph.h | 6 +++ 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/src/buttons_effects.c b/src/buttons_effects.c index c83f6f8c..4ac4a88c 100644 --- a/src/buttons_effects.c +++ b/src/buttons_effects.c @@ -283,6 +283,75 @@ static int Check_block_constraints(enum IMAGE_MODES mode) return error_count; } +/// convert a picture to the HGR mode +/// +/// Recognize monochrome pictures. +/// Color pictures should use the 8 first colors. +static void Convert_to_hgr(void) +{ + int i; + word count, x, y; + dword usage[256]; + + count = Count_used_colors(usage); + if (count <= 1) // blank picture, nothing to do :) + return; + if (count == 2) // monochrome ! + { + byte bg, fg; + i = 0; + while (usage[i] == 0 && i < 256) + i++; + bg = (byte)i; + i++; + while (usage[i] == 0 && i < 256) + i++; + fg = (byte)i; + GFX2_Log(GFX2_DEBUG, "Convert_to_hgr() monochrome bg=%u fg=%u\n", bg, fg); + if (!(bg == 0 && fg == 3) && !(bg == 4 && fg == 7)) + { + // convert to B&W + for (y = 0; y < Main.image_height; y++) + { + for (x = 0; x < Main.image_width; x++) + { + byte c = Read_pixel_from_layer(0, x, y); + Pixel_in_layer(0, x, y, (c == fg) ? 3 : 0); + } + } + } + } + else + { + // "convert" color picture to B&W + for (y = 0; y < Main.image_height; y++) + { + for (x = 0; x < Main.image_width; x++) + { + byte c = Read_pixel_from_layer(0, x, y); + switch (c & 3) + { + case 0: // black + case 3: // white + Pixel_in_layer(0, x, y, c); + break; + case 1: // green/orange + case 2: // purple/blue + Pixel_in_layer(0, x, y, (c & 4) | (((c & 1) ^ (x & 1) ^ 1) * 3)); + } + } + } + } + // update color layer + for (y = 0; y < Main.image_height; y++) + { + for (x = 0; x < Main.image_width; x++) + { + Update_color_hgr_pixel(x, y, 0); + } + } +} + /// Constaint enforcer/checker /// @@ -332,6 +401,22 @@ void Button_Constraint_mode(void) } } break; + case IMAGE_MODE_HGR: + case IMAGE_MODE_DHGR: + // switch to layer mode if needed + if (Main.backups->Pages->Image_mode != IMAGE_MODE_LAYERED) + Switch_layer_mode(IMAGE_MODE_LAYERED); + // auto-create extra layers + while (Main.backups->Pages->Nb_layers < 2) + if (Add_layer(Main.backups, Main.backups->Pages->Nb_layers)) + { + Verbose_message("Error!", "Failed to create the 2 layers needed by Emulation of Apple II HGR or DHGR."); + return; + } + if (Selected_Constraint_Mode == IMAGE_MODE_HGR) + Convert_to_hgr(); + /// @todo conversion to DHGR + break; default: Check_block_constraints(Selected_Constraint_Mode); } diff --git a/src/graph.c b/src/graph.c index 6de1f41e..8508fd56 100644 --- a/src/graph.c +++ b/src/graph.c @@ -3568,7 +3568,9 @@ static void Pixel_in_screen_overlay_with_opt_preview(word x,word y,byte color,in } /// generate color pixels in layer 2 from the monochrome layer 1 -static void Update_color_hgr_pixel(word x, word y, int preview) +/// +/// For Apple II HGR mode ::IMAGE_MODE_HGR +void Update_color_hgr_pixel(word x, word y, int preview) { byte b2, b1, b0, pal; diff --git a/src/graph.h b/src/graph.h index 0e652292..ebbf597b 100644 --- a/src/graph.h +++ b/src/graph.h @@ -25,6 +25,9 @@ /// Graphic functions that target the screen and/or image. ////////////////////////////////////////////////////////////////////////////// +#ifndef GRAPH_H__ +#define GRAPH_H__ + void Shade_list_to_lookup_tables(word * list, short step, byte mode, byte * table_inc, byte * table_dec ); @@ -150,3 +153,6 @@ extern Func_pixel_opt_preview Pixel_in_current_screen_with_opt_preview; /// through ::Pixel_in_current_screen_with_opt_preview void Update_pixel_renderer(void); +void Update_color_hgr_pixel(word x, word y, int preview); + +#endif