]> bicyclesonthemoon.info Git - ott/enhance/commitdiff
Fading enhancement actually works now!
authorb <rowerynaksiezycu@gmail.com>
Sat, 12 Nov 2022 00:41:59 +0000 (01:41 +0100)
committerb <rowerynaksiezycu@gmail.com>
Sat, 12 Nov 2022 00:41:59 +0000 (01:41 +0100)
core.c
core.h
nofading.c
testimg/fadebike.png [new file with mode: 0644]
testimg/fadebike_ind.png [new file with mode: 0644]

diff --git a/core.c b/core.c
index 3be95f298f556d95e3efeff3f5d957fd8149ad20..faf7dcce9378c5a9ba9b3528deb6e3c88dadad2e 100644 (file)
--- 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 4e31d1f537b62a472f006fa422141d6a9f5c3336..0eff00212c12b11d6eb4ce7fc699b2d9648b9543 100644 (file)
--- 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;
index 7d3afd69704973a68091380a7a4f2fde38ab655c..535acae30bb067903b4669fab33d74cc13211e63 100644 (file)
@@ -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 (file)
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 (file)
index 0000000..fe80e37
Binary files /dev/null and b/testimg/fadebike_ind.png differ