/*
nofading.c
The tool to remove fading from an image
-06.11.2022
+11.11.2022
Copyright (C) 2015, 2022 Balthasar Szczepański
#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)
*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)
}
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)
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