/*
core.c
The tool with multiple enhancements and manipulations of pictures
-08.11.2022
+09.11.2022
Copyright (C) 2014, 2015, 2022 Balthasar Szczepański
ILint f;
ILint frames;
struct PixelInfo p;
- int r;
+ int r = 0;
p.flags = flags & ~(IS_MULTIPLE);
for (
f = 0;
- f < frames;
+ (f < frames) && (r == 0);
++f
){
p.f_window = f;
p.f_pict = f;
ilBindImage(picture[id].handle);
- ilActiveImage(z);
+ ilActiveImage(f);
p.data = ilGetData();
p.flags &= ~(IS_GRAY|IS_INDEXED|IS_OVER_8BIT|IS_PALETTE_ONLY);
- p.alpha_offset = -1;
- p.value_offset = -1;
- p.index_offset = -1;
- p.red_offset = -1;
- p.green_offset = -1;
- p.blue_offset = -1;
+ p.alpha_offset = 0;
+ p.value_offset = 0;
+ p.index_offset = 0;
+ p.red_offset = 0;
+ p.green_offset = 0;
+ p.blue_offset = 0;
switch (p.info.image_format)
{
break;
case IL_RGBA:
p.flags |= HAS_ALPHA;
- p.alpha.offset = 3 * p.info.image_bpc;
+ p.alpha_offset = 3 * p.info.image_bpc;
case IL_RGB:
default:
p.red_offset = 0 * p.info.image_bpc;
p.blue_offset = 2 * p.info.image_bpc;
break;
}
- if (p.info.image_bpc > 1);
+ if (p.info.image_bpc > 1)
p.flags |= IS_OVER_8BIT;
if ((p.flags & IS_INDEXED) && (p.flags & OK_PALETTE_ONLY))
{
p.flags |= IS_PALETTE_ONLY;
- /* TODO */
+ for (
+ p.index = 0, p.pal_offset=0;
+ (p.index < p.info.palette_num_cols) && (r == 0);
+ ++p.index, p.pal_offset += p.info.palette_bpp
+ ){
+ if (!(p.flags & NOT_READABLE))
+ {
+ p.red = (ILuint)(*((ILubyte*)(p.palette + p.pal_offset + p.red_offset)));
+ p.green = (ILuint)(*((ILubyte*)(p.palette + p.pal_offset + p.green_offset)));
+ p.blue = (ILuint)(*((ILubyte*)(p.palette + p.pal_offset + p.blue_offset)));
+ if (p.flags & HAS_ALPHA)
+ {
+ if (p.flags & CANNOT_HAVE_ALPHA)
+ p.alpha = 0xFF;
+ else
+ p.alpha = (ILuint)(*((ILubyte*)(p.palette + p.pal_offset + p.alpha_offset)));
+ }
+ else if (p.flags & MUST_HAVE_ALPHA)
+ p.alpha = 0xFF;
+ if (p.flags & MUST_BE_GRAY)
+ p.value = (p.red + p.green + p.blue) / 3;
+ }
+
+ r = function(1, &p, data);
+ if (r)
+ break;
+
+ if (!(p.flags & NOT_READABLE))
+ {
+ if (p.flags & MUST_BE_GRAY)
+ {
+ p.red = p.value;
+ p.green = p.value;
+ p.blue = p.value;
+ }
+ if (p.flags & HAS_ALPHA)
+ {
+ if (p.flags & CANNOT_HAVE_ALPHA)
+ p.alpha = 0xFF;
+ *((ILubyte*)(p.palette + p.pal_offset + p.alpha_offset)) = (ILubyte)p.alpha;
+ }
+ *((ILubyte*)(p.palette + p.pal_offset + p.red_offset)) = (ILubyte)p.red;
+ *((ILubyte*)(p.palette + p.pal_offset + p.green_offset)) = (ILubyte)p.green;
+ *((ILubyte*)(p.palette + p.pal_offset + p.blue_offset)) = (ILubyte)p.blue;
+ }
+ }
}
else
{
p.line_bytes = p.info.image_bytes_per_pixel * p.info.image_width;
p.frame_bytes = p.line_bytes * p.info.image_height;
- if (info.origin_mode == IL_ORIGIN_LOWER_LEFT)
+ if (p.info.origin_mode == IL_ORIGIN_LOWER_LEFT)
{
p.line_start = p.frame_bytes - p.line_bytes;
p.line_bytes = 0 - p.line_bytes;
for (
p.y_window = 0, p.y_pict = y0, p.line_offset = p.line_start + (p.y_pict * p.line_bytes);
- p.y_window < height,
+ (p.y_window < height) && (r == 0);
++p.y_window, ++p.y_pict, p.line_offset += p.line_bytes
){
if ((p.y_pict >= 0) && (p.y_pict < p.info.image_height))
{
for (
p.x_window = 0, p.x_pict = x0, p.pixel_offset = p.line_offset + (p.x_pict * p.info.image_bytes_per_pixel);
- p.x_window < width;
+ (p.x_window < width) && (r == 0);
++p.x_window, ++p.x_pict, p.pixel_offset += p.info.image_bytes_per_pixel
){
if ((p.x_pict >=0) && (p.x_pict < p.info.image_width))
{
-
-
-
- switch (info.image_type)
+ if (!(p.flags & NOT_READABLE))
{
- case IL_INT:
- case IL_UNSIGNED_INT:
- if(flags & IS_INDEXED)
- i = (ILuint)(*((ILuint*)(data+pixel_offset+i_offset)));
- else
+ /* can this be done better? think about it */
+ switch (p.info.image_type)
{
- if(flags & IS_GRAY)
- v = (ILuint)(*((ILuint*)(data+pixel_offset+v_offset)));
+ case IL_INT:
+ case IL_UNSIGNED_INT:
+ if(p.flags & IS_INDEXED)
+ p.index = *((ILuint*)(p.data + p.pixel_offset + p.index_offset));
+ else
+ {
+ if(p.flags & IS_GRAY)
+ p.value = *((ILuint*)(p.data + p.pixel_offset + p.value_offset));
+ else
+ {
+ p.red = *((ILuint*)(p.data + p.pixel_offset + p.red_offset));
+ p.green = *((ILuint*)(p.data + p.pixel_offset + p.green_offset));
+ p.blue = *((ILuint*)(p.data + p.pixel_offset + p.blue_offset));
+ }
+ if(p.flags & HAS_ALPHA)
+ p.alpha = *((ILuint*)(p.data + p.pixel_offset + p.alpha_offset));
+ }
+ break;
+ case IL_SHORT:
+ case IL_UNSIGNED_SHORT:
+ if(p.flags & IS_INDEXED)
+ p.index = (ILuint)(*((ILushort*)(p.data + p.pixel_offset + p.index_offset)));
+ else
+ {
+ if(p.flags & IS_GRAY)
+ p.value = (ILuint)(*((ILushort*)(p.data + p.pixel_offset + p.value_offset)));
+ else
+ {
+ p.red = (ILuint)(*((ILushort*)(p.data + p.pixel_offset + p.red_offset)));
+ p.green = (ILuint)(*((ILushort*)(p.data + p.pixel_offset + p.green_offset)));
+ p.blue = (ILuint)(*((ILushort*)(p.data + p.pixel_offset + p.blue_offset)));
+ }
+ if(p.flags & HAS_ALPHA)
+ p.alpha = (ILuint)(*((ILushort*)(p.data + p.pixel_offset + p.alpha_offset)));
+ }
+ break;
+ case IL_BYTE:
+ case IL_UNSIGNED_BYTE:
+ if(p.flags & IS_INDEXED)
+ p.index = (ILuint)(*((ILubyte*)(p.data + p.pixel_offset + p.index_offset)));
else
{
- r = (ILuint)(*((ILuint*)(data+pixel_offset+r_offset)));
- g = (ILuint)(*((ILuint*)(data+pixel_offset+g_offset)));
- b = (ILuint)(*((ILuint*)(data+pixel_offset+b_offset)));
+ if(p.flags & IS_GRAY)
+ p.value = (ILuint)(*((ILubyte*)(p.data + p.pixel_offset + p.value_offset)));
+ else
+ {
+ p.red = (ILuint)(*((ILubyte*)(p.data + p.pixel_offset + p.red_offset)));
+ p.green = (ILuint)(*((ILubyte*)(p.data + p.pixel_offset + p.green_offset)));
+ p.blue = (ILuint)(*((ILubyte*)(p.data + p.pixel_offset + p.blue_offset)));
+ }
+ if(p.flags & HAS_ALPHA)
+ p.alpha = (ILuint)(*((ILubyte*)(p.data + p.pixel_offset + p.alpha_offset)));
}
- if(flags & HAS_ALPHA)
- a = (ILuint)(*((ILuint*)(data+pixel_offset+a_offset)));
+ break;
+ default:
+ break;
}
- break;
- case IL_SHORT:
- case IL_UNSIGNED_SHORT:
- if(flags & IS_INDEXED)
- i = (ILushort)(*((ILuint*)(data+pixel_offset+i_offset)));
- else
+
+ if ((p.flags & IS_INDEXED) && (p.flags & CANNOT_BE_INDEXED))
{
- if(flags & IS_GRAY)
- v = (ILushort)(*((ILuint*)(data+pixel_offset+v_offset)));
- else
+ if (p.index < p.info.palette_num_cols)
{
- r = (ILushort)(*((ILuint*)(data+pixel_offset+r_offset)));
- g = (ILushort)(*((ILuint*)(data+pixel_offset+g_offset)));
- b = (ILushort)(*((ILuint*)(data+pixel_offset+b_offset)));
+ p.pal_offset = p.index * p.info.palette_bpp;
+ p.red = (ILuint)(*((ILubyte*)(p.palette + p.pal_offset + p.red_offset)));
+ p.green = (ILuint)(*((ILubyte*)(p.palette + p.pal_offset + p.green_offset)));
+ p.blue = (ILuint)(*((ILubyte*)(p.palette + p.pal_offset + p.blue_offset)));
+ if (p.flags & HAS_ALPHA)
+ p.alpha = (ILuint)(*((ILubyte*)(p.palette + p.pal_offset + p.alpha_offset)));
}
- if(flags & HAS_ALPHA)
- a = (ILushort)(*((ILuint*)(data+pixel_offset+a_offset)));
}
- break;
- case IL_BYTE:
- case IL_UNSIGNED_BYTE:
- if(flags & IS_INDEXED)
- i = (ILushort)(*((ILubyte*)(data+pixel_offset+i_offset)));
- else
+
+ if ((p.flags & IS_GRAY) && (p.flags & CANNOT_BE_GRAY))
{
- if(flags & IS_GRAY)
- v = (ILushort)(*((ILubyte*)(data+pixel_offset+v_offset)));
- else
+ p.red = p.value;
+ p.green = p.value;
+ p.blue = p.value;
+ }
+ else if (!(p.flags & IS_GRAY) && (p.flags & MUST_BE_GRAY))
+ p.value = (p.red + p.green + p.blue) / 3;
+
+ if (((!(p.flags & HAS_ALPHA)) && (p.flags & MUST_HAVE_ALPHA)) || ((p.flags & HAS_ALPHA) && (p.flags & CANNOT_HAVE_ALPHA)))
+ {
+ switch (p.info.image_type)
{
- r = (ILushort)(*((ILubyte*)(data+pixel_offset+r_offset)));
- g = (ILushort)(*((ILubyte*)(data+pixel_offset+g_offset)));
- b = (ILushort)(*((ILubyte*)(data+pixel_offset+b_offset)));
+ case IL_INT:
+ case IL_UNSIGNED_INT:
+ p.alpha = 0xFFFFFFFF;
+ break;
+ case IL_SHORT:
+ case IL_UNSIGNED_SHORT:
+ p.alpha = 0xFFFF;
+ break;
+ case IL_BYTE:
+ case IL_UNSIGNED_BYTE:
+ p.alpha = 0xFF;
+ break;
+ default:
+ break;
}
- if(flags & HAS_ALPHA)
- a = (ILushort)(*((ILubyte*)(data+pixel_offset+a_offset)));
}
- break;
- default:
- break;
- }
- if ((flags & IS_INDEXED) && (flags & CANNOT_BE_INDEXED))
- {
- // TODO: i -> rgb
- }
- if ((flags & IS_GRAY) && (flags & CANNOT_BE_GRAY))
- {
- r = v;
- g = v;
- b = v;
}
- else if (!(flags & IS_GRAY) && (flags & MUST_BE_GRAY))
+
+ r = function(1, &p, data);
+ if (r)
+ break;
+
+ if (!(p.flags & NOT_WRITABLE))
{
- y = (r+g+b)/3;
+ if ((p.flags & HAS_ALPHA) && (p.flags & CANNOT_HAVE_ALPHA))
+ p.alpha = 0xFFFFFFFF;
+
+ else if (!(p.flags & IS_GRAY) && (p.flags & MUST_BE_GRAY))
+ {
+ p.red = p.value;
+ p.green = p.value;
+ p.blue = p.value;
+ }
+ else if ((p.flags & IS_GRAY) && (p.flags & CANNOT_BE_GRAY))
+ p.value = (p.red + p.green + p.blue) / 3;
+
+ switch (p.info.image_type)
+ {
+ case IL_INT:
+ case IL_UNSIGNED_INT:
+ if(p.flags & IS_INDEXED)
+ *((ILuint*)(p.data + p.pixel_offset + p.index_offset)) = p.index;
+ else
+ {
+ if(p.flags & IS_GRAY)
+ *((ILuint*)(p.data + p.pixel_offset + p.value_offset)) = p.value;
+ else
+ {
+ *((ILuint*)(p.data + p.pixel_offset + p.red_offset)) = p.red;
+ *((ILuint*)(p.data + p.pixel_offset + p.green_offset)) = p.green;
+ *((ILuint*)(p.data + p.pixel_offset + p.blue_offset)) = p.blue;
+ }
+ if(p.flags & HAS_ALPHA)
+ *((ILuint*)(p.data + p.pixel_offset + p.alpha_offset)) = (ILubyte)p.alpha;
+ }
+ break;
+ case IL_SHORT:
+ case IL_UNSIGNED_SHORT:
+ if(p.flags & IS_INDEXED)
+ *((ILushort*)(p.data + p.pixel_offset + p.index_offset)) = (ILushort)p.index;
+ else
+ {
+ if(p.flags & IS_GRAY)
+ *((ILushort*)(p.data + p.pixel_offset + p.value_offset)) = (ILushort)p.value;
+ else
+ {
+ *((ILushort*)(p.data + p.pixel_offset + p.red_offset)) = (ILushort)p.red;
+ *((ILushort*)(p.data + p.pixel_offset + p.green_offset)) = (ILushort)p.green;
+ *((ILushort*)(p.data + p.pixel_offset + p.blue_offset)) = (ILushort)p.blue;
+ }
+ if(p.flags & HAS_ALPHA)
+ *((ILushort*)(p.data + p.pixel_offset + p.alpha_offset)) = (ILushort)p.alpha;
+ }
+ break;
+ case IL_BYTE:
+ case IL_UNSIGNED_BYTE:
+ if(p.flags & IS_INDEXED)
+ *((ILubyte*)(p.data + p.pixel_offset + p.index_offset)) = (ILubyte)p.index;
+ else
+ {
+ if(p.flags & IS_GRAY)
+ *((ILubyte*)(p.data + p.pixel_offset + p.value_offset)) = (ILubyte)p.value;
+ else
+ {
+ *((ILubyte*)(p.data + p.pixel_offset + p.red_offset)) = (ILubyte)p.red;
+ *((ILubyte*)(p.data + p.pixel_offset + p.green_offset)) = (ILubyte)p.green;
+ *((ILubyte*)(p.data + p.pixel_offset + p.blue_offset)) = (ILubyte)p.blue;
+ }
+ if(p.flags & HAS_ALPHA)
+ *((ILubyte*)(p.data + p.pixel_offset + p.alpha_offset)) = (ILubyte)p.alpha;
+ }
+ break;
+ default:
+ break;
+ }
}
-
- re = function(x,y,z,&r,&g,&b,&a,&v,&i,flags,f_data);
- if (re)
- return re; //!!!!!!!!
}
}
}
}
}
}
- return 0;
+
+ return r;
}
int action(
ILuint n, struct PixelInfo *info, void *data
/*
core.h
The tool with multiple enhancements and manipulations of pictures
-08.11.2022
+09.11.2022
Copyright (C) 2022 Balthasar Szczepański
#include "IL/il.h"
-#define FLAG_TYPE uint_fast16_t
+#define FLAG_TYPE uint_fast32_t
-#define MUST_HAVE_ALPHA 0x0001
-#define CANNOT_HAVE_ALPHA 0x0002
-#define MUST_BE_GRAY 0x0004
-#define CANNOT_BE_GRAY 0x0008
-#define MUST_BE_INDEXED 0x0010
-#define CANNOT_BE_INDEXED 0x0020
+#define MUST_HAVE_ALPHA 0x00001
+#define CANNOT_HAVE_ALPHA 0x00002
+#define MUST_BE_GRAY 0x00004
+#define CANNOT_BE_GRAY 0x00008
+#define MUST_BE_INDEXED 0x00010
+#define CANNOT_BE_INDEXED 0x00020
-#define OK_PALETTE_ONLY 0x0040
-#define CAN_BE_MULTIPLE 0x0080
-#define CAN_BE_OVER_8BIT 0x0100
+#define OK_PALETTE_ONLY 0x00040
+#define CAN_BE_MULTIPLE 0x00080
+#define CAN_BE_OVER_8BIT 0x00100
-#define IN_WINDOW 0x0200
+#define IN_WINDOW 0x00200
-#define HAS_ALPHA 0x0400
-#define IS_GRAY 0x0800
-#define IS_INDEXED 0x1000
-#define IS_MULTIPLE 0x2000
-#define IS_OVER_8BIT 0x4000
-#define IS_PALETTE_ONLY 0x8000
+#define HAS_ALPHA 0x00400
+#define IS_GRAY 0x00800
+#define IS_INDEXED 0x01000
+#define IS_MULTIPLE 0x02000
+#define IS_OVER_8BIT 0x04000
+#define IS_PALETTE_ONLY 0x08000
+
+#define NOT_WRITABLE 0x10000
+#define NOT_READABLE 0x20000
extern char NO_STR[];
extern char INIT_FAILED[];
ILuint handle;
};
-struct PixelInfo
-{
- ILint x_window;
- ILint y_window;
- ILint f_window;
-
- ILint x_pict;
- ILint y_pict;
- ILint f_pict;
-
- ILuint red;
- ILuint green;
- ILuint blue;
- ILuint value;
- ILuint alpha;
- ILuint index;
-
- ILint red_offset;
- ILint green_offset;
- ILint blue_offset;
- ILint value_offset;
- ILint alpha_offset;
- ILint index_offset;
-
- ILint line_start;
- ILint line_bytes;
- ILint frame_bytes;
-
- ILint line_offset;
- ILint pixel_offset;
-
- FLAG_TYPE flags;
- void *data;
- void *palette;
-
- struct IL_full_info info;
-}
-
struct IL_full_info {
ILint active_image;
ILint active_layer;
ILint version_num;
};
+struct PixelInfo
+{
+ ILint x_window;
+ ILint y_window;
+ ILint f_window;
+
+ ILint x_pict;
+ ILint y_pict;
+ ILint f_pict;
+
+ ILuint red;
+ ILuint green;
+ ILuint blue;
+ ILuint value;
+ ILuint alpha;
+ ILuint index;
+
+ ILint red_offset;
+ ILint green_offset;
+ ILint blue_offset;
+ ILint value_offset;
+ ILint alpha_offset;
+ ILint index_offset;
+
+ ILint line_start;
+ ILint line_bytes;
+ ILint frame_bytes;
+
+ ILint line_offset;
+ ILint pixel_offset;
+ ILint pal_offset;
+
+ FLAG_TYPE flags;
+ void *data;
+ void *palette;
+
+ struct IL_full_info info;
+};
+
typedef int (ACTION_F)(
ILuint n, struct PixelInfo *info, void *data
);
);
-
ACTION_F action;
\ No newline at end of file