From: b Date: Sat, 12 Nov 2022 00:41:59 +0000 (+0100) Subject: Fading enhancement actually works now! X-Git-Url: http://bicyclesonthemoon.info/git-projects/?a=commitdiff_plain;h=2365ac033054f562b2f3f12b23bddcaca51b5191;p=ott%2Fenhance Fading enhancement actually works now! --- diff --git a/core.c b/core.c index 3be95f2..faf7dcc 100644 --- a/core.c +++ b/core.c @@ -1,7 +1,7 @@ /* core.c The tool with multiple enhancements and manipulations of pictures -09.11.2022 +11.11.2022 Copyright (C) 2014, 2015, 2022 Balthasar Szczepański @@ -38,7 +38,9 @@ struct Picture * picture; char INIT_FAILED[] = "Failae to set up library.\n"; char LOAD_FAILED[] = "Failed to load picture.\n"; +char SAVE_FAILED[] = "Failed to save picture.\n"; char CREATE_FAILED[] = "Failed to create picture(s).\n"; +char CONVERT_FAILED[] = "Failed to convert picture.\n"; char NO_STR[] = ""; int init (void) diff --git a/core.h b/core.h index 4e31d1f..0eff002 100644 --- a/core.h +++ b/core.h @@ -60,7 +60,9 @@ on Pentium III libdevil must be recompiled with extern char NO_STR[]; extern char INIT_FAILED[]; extern char LOAD_FAILED[]; +extern char SAVE_FAILED[]; extern char CREATE_FAILED[]; +extern char CONVERT_FAILED[]; struct Picture { uint_fast8_t open; diff --git a/nofading.c b/nofading.c index 7d3afd6..535acae 100644 --- a/nofading.c +++ b/nofading.c @@ -1,7 +1,7 @@ /* nofading.c The tool to remove fading from an image -06.11.2022 +11.11.2022 Copyright (C) 2015, 2022 Balthasar Szczepański @@ -33,16 +33,52 @@ on Pentium III libdevil must be recompiled with #include "core.h" #include "nofading.h" +#define UPDATE_RANGE(val, low, high) \ +{ \ + if ((val) < (low)) \ + (low) = (val); \ + if ((val) > (high)) \ + (high) = (val); \ +} + char NOFADING_MISSING_ARGS[] = "Missing parameters.\nnofading inPix outPix [a] [framesize]\n"; -int nofading (int argc, char **argv, char **err) +struct nofading_data { uint_fast8_t individual_channels; + ILuint red_low; + ILuint red_high; + ILuint green_low; + ILuint green_high; + ILuint blue_low; + ILuint blue_high; + ILuint max; +}; + +ILuint enhance_fading_1ch (ILuint val, ILuint low, ILuint high, ILuint max); + +int find_fading_range (ILuint n, struct PixelInfo *p, void *data); +int enhance_fading (ILuint n, struct PixelInfo *p, void *data); + + + +int nofading (int argc, char **argv, char **err) +{ uint_fast16_t frame_t = 0; uint_fast16_t frame_b = 0; uint_fast16_t frame_l = 0; uint_fast16_t frame_r = 0; - uint_fast8_t palette_only = 0; + ILint x0 = 0; + ILint y0 = 0; + ILint width = 0; + ILint height = 0; + ILuint handle; + + struct nofading_data data; + struct IL_full_info info; + + FLAG_TYPE flags = CAN_BE_MULTIPLE | CAN_BE_OVER_8BIT; + int r; if (argc<2) @@ -50,10 +86,11 @@ int nofading (int argc, char **argv, char **err) *err = NOFADING_MISSING_ARGS; return EINVAL; } + data.individual_channels = 0; if (argc>=3) { if(argv[2][0]=='a' || argv[3][0]=='A') - individual_channels = 1; + data.individual_channels = 1; } if (argc>=7) @@ -86,7 +123,9 @@ int nofading (int argc, char **argv, char **err) } if ((frame_t==0) && (frame_b==0) && (frame_l==0) && (frame_r==0)) - palette_only = OK_PALETTE_ONLY; + flags |= OK_PALETTE_ONLY; + else + flags |= CANNOT_BE_INDEXED | IN_WINDOW; r = create_pictures(1); if (r) @@ -95,24 +134,121 @@ int nofading (int argc, char **argv, char **err) return r; } - r = load_picture(0, argv[0], CAN_BE_MULTIPLE); + r = load_picture(0, argv[0], flags); if (r) { *err = LOAD_FAILED; return r; } + data.red_low = 0xFFFFFFFF; + data.green_low = 0xFFFFFFFF; + data.blue_low = 0xFFFFFFFF; + data.red_high = 0x00000000; + data.green_high= 0x00000000; + data.blue_high = 0x00000000; + + get_info(0, &info, 0); + + data.max = (1<<(info.image_bpc<<3))-1; + if (flags & IN_WINDOW) + { + x0 = frame_l; + y0 = frame_t; + width = info.image_width - frame_l - frame_r; + height = info.image_height - frame_t - frame_b; + } + + r = action_1picture ( + 0, + x0, y0, width, height, + &find_fading_range, + flags | NOT_WRITABLE, + &data + ); + r = action_1picture ( 0, - 0,0,0,0, - &action, - CAN_BE_MULTIPLE, - NULL + x0, y0, width, height, + &enhance_fading, + flags, + &data ); + handle = get_handle(0); + ilBindImage(handle); + + if(!ilSaveImage(argv[1])) + { + *err = SAVE_FAILED; + return EIO; + } + return 0; } +int find_fading_range (ILuint n, struct PixelInfo *p, void *data) +{ + struct nofading_data *d; + + d = data; + + if (p->flags & IS_GRAY) + { + UPDATE_RANGE(p->value, d->green_low, d->green_high); + } + else if (d->individual_channels) + { + UPDATE_RANGE(p->red, d->red_low, d->red_high); + UPDATE_RANGE(p->green, d->green_low, d->green_high); + UPDATE_RANGE(p->blue, d->blue_low, d->blue_high); + } + else + { + UPDATE_RANGE(p->red, d->green_low, d->green_high); + UPDATE_RANGE(p->green, d->green_low, d->green_high); + UPDATE_RANGE(p->blue, d->green_low, d->green_high); + } + + return 0; +} + +ILuint enhance_fading_1ch (ILuint val, ILuint low, ILuint high, ILuint max) +{ + ILint64 y; + + if (low == high) + return val; + + y = (((ILint64)val) - low) * max / (high - low); + return (ILuint)((y>max)?max:((y<0)?0:y)); +} + +int enhance_fading (ILuint n, struct PixelInfo *p, void *data) +{ + struct nofading_data *d; + + d = data; + + if (p->flags & IS_GRAY) + p->value = enhance_fading_1ch(p->value, d->green_low, d->green_high, d->max); + else if (d->individual_channels) + { + p->red = enhance_fading_1ch(p->red , d->red_low, d->red_high, d->max); + p->green = enhance_fading_1ch(p->green, d->green_low, d->green_high, d->max); + p->blue = enhance_fading_1ch(p->blue , d->blue_low, d->blue_high, d->max); + } + else + { + p->red = enhance_fading_1ch(p->red , d->green_low, d->green_high, d->max); + p->green = enhance_fading_1ch(p->green, d->green_low, d->green_high, d->max); + p->blue = enhance_fading_1ch(p->blue , d->green_low, d->green_high, d->max); + } + + return 0; +} + + /* #define INPIX_MUSTARD 1 diff --git a/testimg/fadebike.png b/testimg/fadebike.png new file mode 100644 index 0000000..af45311 Binary files /dev/null and b/testimg/fadebike.png differ diff --git a/testimg/fadebike_ind.png b/testimg/fadebike_ind.png new file mode 100644 index 0000000..fe80e37 Binary files /dev/null and b/testimg/fadebike_ind.png differ