probably better

git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@571 b9cfdab3-6d41-4d17-bbe4-086880011989
This commit is contained in:
NishiOwO
2025-11-02 20:08:42 +00:00
parent 971ef1b827
commit 95771fddd9
11 changed files with 67 additions and 91 deletions

View File

@@ -6,15 +6,26 @@ MwWidget cpicker;
MwWidget window; MwWidget window;
MwWidget button; MwWidget button;
void color_callback(MwRGB rgb) { void color_callback(MwWidget handle, void* user_data, void* call_data) {
char hexColor[8]; char hexColor[8];
MwRGB* rgb = call_data;
sprintf(hexColor, "#%02X%02X%02X", rgb.r, rgb.g, rgb.b); (void)handle;
(void)user_data;
sprintf(hexColor, "#%02X%02X%02X", rgb->red, rgb->green, rgb->blue);
MwSetText(window, MwNbackground, hexColor); MwSetText(window, MwNbackground, hexColor);
} }
void color_picker(MwWidget handle, void* user_data, void* call_data) { void color_picker(MwWidget handle, void* user_data, void* call_data) {
MwWidget cpicker = MwColorPicker(window, "cpicker", color_callback); MwWidget cpicker = MwColorPicker(window, "cpicker");
(void)handle;
(void)user_data;
(void)call_data;
MwAddUserHandler(cpicker, MwNcolorChosenHandler, color_callback, NULL);
MwSetText(cpicker, MwNbackground, MwDefaultBackground); MwSetText(cpicker, MwNbackground, MwDefaultBackground);
} }

View File

@@ -11,6 +11,7 @@ typedef struct _MwRect MwRect;
typedef struct _MwSizeHints MwSizeHints; typedef struct _MwSizeHints MwSizeHints;
typedef struct _MwFont MwFont; typedef struct _MwFont MwFont;
typedef struct _MwCursor MwCursor; typedef struct _MwCursor MwCursor;
typedef struct _MwRGB MwRGB;
typedef unsigned char MwBool; typedef unsigned char MwBool;
#define MwTRUE ((MwBool)1) #define MwTRUE ((MwBool)1)
@@ -104,4 +105,10 @@ struct _MwFont {
unsigned char data[16]; unsigned char data[16];
}; };
struct _MwRGB {
MwU32 red;
MwU32 green;
MwU32 blue;
};
#endif #endif

View File

@@ -14,17 +14,13 @@
extern "C" { extern "C" {
#endif #endif
typedef struct _MwRGB MwRGB; /*!
* @brief Creates a color picker
struct _MwRGB { * @param handle Widget
MwU32 r; * @param title Title text
MwU32 g; * @return Widget
MwU32 b; */
}; MWDECL MwWidget MwColorPicker(MwWidget handle, const char* title);
typedef void (*MwColorPickerCallback)(MwRGB rgb);
MWDECL MwWidget MwColorPicker(MwWidget handle, const char* title, MwColorPickerCallback cb);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -92,7 +92,7 @@ MWDECL void MwLLPolygon(MwLL handle, MwPoint* points, int points_count, MwLLColo
MWDECL void MwLLLine(MwLL handle, MwPoint* points, MwLLColor color); MWDECL void MwLLLine(MwLL handle, MwPoint* points, MwLLColor color);
MWDECL MwLLColor MwLLAllocColor(MwLL handle, int r, int g, int b); MWDECL MwLLColor MwLLAllocColor(MwLL handle, int r, int g, int b);
MWDECL void MwLLColorUpdate(MwLL handle, int r, int g, int b, MwLLColor c); MWDECL void MwLLColorUpdate(MwLL handle, MwLLColor c, int r, int g, int b);
MWDECL void MwLLFreeColor(MwLLColor color); MWDECL void MwLLFreeColor(MwLLColor color);
MWDECL void MwLLGetXYWH(MwLL handle, int* x, int* y, unsigned int* w, unsigned int* h); MWDECL void MwLLGetXYWH(MwLL handle, int* x, int* y, unsigned int* w, unsigned int* h);

View File

@@ -53,5 +53,6 @@
#define MwNfocusInHandler "CfocusIn" /* NULL */ #define MwNfocusInHandler "CfocusIn" /* NULL */
#define MwNfocusOutHandler "CfocusOut" /* NULL */ #define MwNfocusOutHandler "CfocusOut" /* NULL */
#define MwNfileChosenHandler "CfileChosen" /* char* */ #define MwNfileChosenHandler "CfileChosen" /* char* */
#define MwNcolorChosenHandler "CcolorChosen" /* MwRGB* */
#endif #endif

View File

@@ -208,32 +208,11 @@ void MwLLLine(MwLL handle, MwPoint* points, MwLLColor color) {
MwLLColor MwLLAllocColor(MwLL handle, int r, int g, int b) { MwLLColor MwLLAllocColor(MwLL handle, int r, int g, int b) {
MwLLColor c = malloc(sizeof(*c)); MwLLColor c = malloc(sizeof(*c));
XColor xc; MwLLColorUpdate(handle, c, r, g, b);
if(handle->red_mask == 0) {
if(r > 255) r = 255;
if(g > 255) g = 255;
if(b > 255) b = 255;
if(r < 0) r = 0;
if(g < 0) g = 0;
if(b < 0) b = 0;
xc.red = 256 * r;
xc.green = 256 * g;
xc.blue = 256 * b;
XAllocColor(handle->display, handle->colormap, &xc);
c->pixel = xc.pixel;
} else {
c->pixel = generate_color(handle, r, g, b);
}
c->red = r;
c->green = g;
c->blue = b;
return c; return c;
} }
void MwLLColorUpdate(MwLL handle, int r, int g, int b, MwLLColor c) { void MwLLColorUpdate(MwLL handle, MwLLColor c, int r, int g, int b) {
XColor xc; XColor xc;
if(handle->red_mask == 0) { if(handle->red_mask == 0) {
@@ -455,7 +434,6 @@ MwLLPixmap MwLLCreatePixmap(MwLL handle, unsigned char* data, int width, int hei
MwLLPixmap r = malloc(sizeof(*r)); MwLLPixmap r = malloc(sizeof(*r));
char* di = malloc(4 * width * height); char* di = malloc(4 * width * height);
char* dm = malloc(4 * width * height); char* dm = malloc(4 * width * height);
int y, x;
int evbase, erbase; int evbase, erbase;
XWindowAttributes attr; XWindowAttributes attr;
@@ -481,22 +459,20 @@ MwLLPixmap MwLLCreatePixmap(MwLL handle, unsigned char* data, int width, int hei
void MwLLPixmapUpdate(MwLL handle, MwLLPixmap r) { void MwLLPixmapUpdate(MwLL handle, MwLLPixmap r) {
int y, x; int y, x;
MwLLColor c = NULL;
for(y = 0; y < r->height; y++) { for(y = 0; y < r->height; y++) {
for(x = 0; x < r->width; x++) { for(x = 0; x < r->width; x++) {
unsigned char* px = &r->data_buf[(y * r->width + x) * 4]; unsigned char* px = &r->data_buf[(y * r->width + x) * 4];
if(c == NULL) { MwLLColor c = NULL;
unsigned long p;
c = MwLLAllocColor(handle, px[0], px[1], px[2]); c = MwLLAllocColor(handle, px[0], px[1], px[2]);
} else { p = c->pixel;
MwLLColorUpdate(handle, px[0], px[1], px[2], c); MwLLFreeColor(c);
}
unsigned long p = c->pixel;
XPutPixel(r->image, x, y, p); XPutPixel(r->image, x, y, p);
*(unsigned long*)(&r->data[(y * r->width + x) * sizeof(unsigned long)]) = (px[3] << 24) | p; *(unsigned long*)(&r->data[(y * r->width + x) * sizeof(unsigned long)]) = (px[3] << 24) | p;
} }
} }
MwLLFreeColor(c);
for(y = 0; y < r->height; y++) { for(y = 0; y < r->height; y++) {
for(x = 0; x < r->width; x++) { for(x = 0; x < r->width; x++) {

View File

@@ -65,19 +65,4 @@ struct _MwLLPixmap {
XImage* mask; XImage* mask;
}; };
#ifdef HAS_FREETYPE
void print_ft_error(void* ftLib, FT_Error err);
#define MwFreeTypeFontSize 181388
extern FT_Byte MwFreeTypeFontData[MwFreeTypeFontSize];
#define FT_WITH_FUNC(handle, func, block) \
{ \
_##func = dlsym(handle->ftLib, #func); \
if(_##func != NULL) { \
block \
} else \
printf("[WARNING] Unable to resolve function " #func ".\n"); \
}
#endif
#endif #endif

View File

@@ -5,7 +5,9 @@
#include <Mw/LowLevelMath.h> #include <Mw/LowLevelMath.h>
static void hsv2rgb(MwU32 h, MwU32 s, MwU32 v, MwU32* r, MwU32* g, MwU32* b) { static void hsv2rgb(MwU32 h, MwU32 s, MwU32 v, MwU32* r, MwU32* g, MwU32* b) {
uint8_t sextant = h >> 8; MwU8 sextant = h >> 8;
MwU16 ww;
MwU32 h_fraction, d;
#define HSV_SWAPPTR(a, b) \ #define HSV_SWAPPTR(a, b) \
do { \ do { \
@@ -39,14 +41,12 @@ static void hsv2rgb(MwU32 h, MwU32 s, MwU32 v, MwU32* r, MwU32* g, MwU32* b) {
* Bottom level: v * (1.0 - s) * Bottom level: v * (1.0 - s)
* --> (v * (255 - s) + error_corr + 1) / 256 * --> (v * (255 - s) + error_corr + 1) / 256
*/ */
uint16_t ww; // Intermediate result
ww = v * (255 - (s)); // We don't use ~s to prevent size-promotion side effects ww = v * (255 - (s)); // We don't use ~s to prevent size-promotion side effects
ww += 1; // Error correction ww += 1; // Error correction
ww += ww >> 8; // Error correction ww += ww >> 8; // Error correction
*b = ww >> 8; *b = ww >> 8;
MwU32 h_fraction = h & 0xff; // 0...255 h_fraction = h & 0xff; // 0...255
MwU32 d; // Intermediate result
if(!(sextant & 1)) { if(!(sextant & 1)) {
// *r = ...slope_up...; // *r = ...slope_up...;
@@ -62,17 +62,17 @@ static void hsv2rgb(MwU32 h, MwU32 s, MwU32 v, MwU32* r, MwU32* g, MwU32* b) {
static void color_picker_image_update(color_picker* picker) { static void color_picker_image_update(color_picker* picker) {
int y, x; int y, x;
double h;
for(y = 0; y < PICKER_SIZE; y++) { for(y = 0; y < PICKER_SIZE; y++) {
for(x = 0; x < PICKER_SIZE; x++) { for(x = 0; x < PICKER_SIZE; x++) {
int i = ((y * PICKER_SIZE) + x) * 4; int i = ((y * PICKER_SIZE) + x) * 4;
int _x = x - (PICKER_SIZE / 2); int _x = x - (PICKER_SIZE / 2);
int _y = y - (PICKER_SIZE / 2); int _y = y - (PICKER_SIZE / 2);
double dist;
if(picker->dist_table[y][x] == 0) { if(picker->dist_table[y][x] == 0) {
picker->dist_table[y][x] = sqrt(_x * _x + _y * _y); picker->dist_table[y][x] = sqrt(_x * _x + _y * _y);
} }
double dist = picker->dist_table[y][x]; dist = picker->dist_table[y][x];
if(dist >= 180.) { if(dist >= 180.) {
picker->color_picker_image_data[i] = 0; picker->color_picker_image_data[i] = 0;
@@ -101,11 +101,11 @@ static void color_picker_image_update(color_picker* picker) {
hsv_v.v = HSV_VAL_MAX - (picker->value * HSV_VAL_MAX); hsv_v.v = HSV_VAL_MAX - (picker->value * HSV_VAL_MAX);
MwRGB color; MwRGB color;
hsv2rgb(hsv_v.h, hsv_v.s, hsv_v.v, &color.r, &color.g, &color.b); hsv2rgb(hsv_v.h, hsv_v.s, hsv_v.v, &color.red, &color.green, &color.blue);
picker->color_picker_image_data[i] = color.r; picker->color_picker_image_data[i] = color.red;
picker->color_picker_image_data[i + 1] = color.g; picker->color_picker_image_data[i + 1] = color.green;
picker->color_picker_image_data[i + 2] = color.b; picker->color_picker_image_data[i + 2] = color.blue;
picker->color_picker_image_data[i + 3] = 255; picker->color_picker_image_data[i + 3] = 255;
} }
@@ -137,15 +137,15 @@ static void color_picker_click(MwWidget handle, void* user, void* call) {
i = ((mouse->point.y * PICKER_SIZE) + mouse->point.x) * 4; i = ((mouse->point.y * PICKER_SIZE) + mouse->point.x) * 4;
picker->chosen_color.r = picker->color_picker_image_data[i]; picker->chosen_color.red = picker->color_picker_image_data[i];
picker->chosen_color.g = picker->color_picker_image_data[i + 1]; picker->chosen_color.green = picker->color_picker_image_data[i + 1];
picker->chosen_color.b = picker->color_picker_image_data[i + 2]; picker->chosen_color.blue = picker->color_picker_image_data[i + 2];
sprintf(hexColor, "#%02X%02X%02X", picker->chosen_color.r, picker->chosen_color.g, picker->chosen_color.b); sprintf(hexColor, "#%02X%02X%02X", picker->chosen_color.red, picker->chosen_color.green, picker->chosen_color.blue);
fr = picker->chosen_color.r > 128 ? 0 : 255; fr = picker->chosen_color.red > 128 ? 0 : 255;
fg = picker->chosen_color.g > 128 ? 0 : 255; fg = picker->chosen_color.green > 128 ? 0 : 255;
fb = picker->chosen_color.b > 128 ? 0 : 255; fb = picker->chosen_color.blue > 128 ? 0 : 255;
sprintf(fgColor, "#%02X%02X%02X", fr, fg, fb); sprintf(fgColor, "#%02X%02X%02X", fr, fg, fb);
MwSetText(picker->color_display, MwNbackground, hexColor); MwSetText(picker->color_display, MwNbackground, hexColor);
@@ -172,16 +172,14 @@ static void color_picker_on_change_value(MwWidget handle, void* user,
static void color_picker_destroy(color_picker* picker) { static void color_picker_destroy(color_picker* picker) {
free(picker->color_picker_image_data); free(picker->color_picker_image_data);
MwLLDestroyPixmap(picker->color_picker_pixmap); MwLLDestroyPixmap(picker->color_picker_pixmap);
MwDestroyWidget(picker->color_display_text);
MwDestroyWidget(picker->color_display);
MwDestroyWidget(picker->value_slider);
MwDestroyWidget(picker->color_picker_img);
} }
static void color_picker_close(MwWidget handle, void* user, static void color_picker_close(MwWidget handle, void* user,
void* call) { void* call) {
color_picker* picker = (color_picker*)user; color_picker* picker = (color_picker*)user;
(void)call;
color_picker_destroy(picker); color_picker_destroy(picker);
MwDestroyWidget(handle); MwDestroyWidget(handle);
} }
@@ -190,7 +188,9 @@ static void color_picker_finish(MwWidget handle, void* user,
void* call) { void* call) {
color_picker* picker = (color_picker*)user; color_picker* picker = (color_picker*)user;
picker->cb(picker->chosen_color); (void)call;
MwDispatchUserHandler(picker->parent, MwNcolorChosenHandler, &picker->chosen_color);
color_picker_destroy(picker); color_picker_destroy(picker);
MwDestroyWidget(handle->parent); MwDestroyWidget(handle->parent);
@@ -264,8 +264,9 @@ color_picker* color_picker_setup(MwWidget parent, int w, int h) {
memset(picker->hue_table, 0, sizeof(picker->hue_table)); memset(picker->hue_table, 0, sizeof(picker->hue_table));
return picker; return picker;
}; }
MwWidget MwColorPicker(MwWidget handle, const char* title, MwColorPickerCallback cb) {
MwWidget MwColorPicker(MwWidget handle, const char* title) {
MwPoint p; MwPoint p;
color_picker* wheel; color_picker* wheel;
MwWidget window; MwWidget window;
@@ -282,7 +283,5 @@ MwWidget MwColorPicker(MwWidget handle, const char* title, MwColorPickerCallback
MwLLDetach(window->lowlevel, &p); MwLLDetach(window->lowlevel, &p);
MwLLMakePopup(window->lowlevel, handle->lowlevel); MwLLMakePopup(window->lowlevel, handle->lowlevel);
wheel->cb = cb;
return window; return window;
} }

View File

@@ -48,7 +48,6 @@ struct _color_picker {
MwPoint point; MwPoint point;
double dist_table[PICKER_SIZE][PICKER_SIZE]; double dist_table[PICKER_SIZE][PICKER_SIZE];
MwHSV hue_table[PICKER_SIZE][PICKER_SIZE]; MwHSV hue_table[PICKER_SIZE][PICKER_SIZE];
MwColorPickerCallback cb;
MwRGB chosen_color; MwRGB chosen_color;
}; };

View File

@@ -68,6 +68,7 @@ MAKE_DEFAULT_TABLE(i16, MwI16);
MAKE_DEFAULT_TABLE(i32, MwI32); MAKE_DEFAULT_TABLE(i32, MwI32);
MAKE_DEFAULT_TABLE(i64, MwI64); MAKE_DEFAULT_TABLE(i64, MwI64);
#if 0
static MwLLMathVTable* defaultMultiTable[MwLLVecType_Max] = { static MwLLMathVTable* defaultMultiTable[MwLLVecType_Max] = {
&table_u8, /*MwLLVecTypeU8*/ &table_u8, /*MwLLVecTypeU8*/
&table_u16, /*MwLLVecTypeU16*/ &table_u16, /*MwLLVecTypeU16*/
@@ -78,6 +79,7 @@ static MwLLMathVTable* defaultMultiTable[MwLLVecType_Max] = {
&table_i32, /*MwLLVecTypeI32*/ &table_i32, /*MwLLVecTypeI32*/
&table_i64, /*MwLLVecTypeI64*/ &table_i64, /*MwLLVecTypeI64*/
}; };
#endif
void default_apply(MwLLVec* v) { void default_apply(MwLLVec* v) {
switch(v->ty) { switch(v->ty) {
case MwLLVecTypeU8: case MwLLVecTypeU8:

View File

@@ -8,7 +8,7 @@
#define DO_MMX_INTRINSIC(intrin, _ty, _cty) \ #define DO_MMX_INTRINSIC(intrin, _ty, _cty) \
int i = 0; \ int i = 0; \
for(; i < a->size; i += 8) { \ for(; i < a->size; i += 8) { \
__m64 m = intrin(*(__m64*)&a->buffer[i], *(__m64*)&b->buffer[i]); \ __m64 m = intrin(((__m64*)a->buffer)[i], ((__m64*)b->buffer)[i]); \
memcpy(&((_cty*)out->buffer)[i], &m, sizeof(m)); \ memcpy(&((_cty*)out->buffer)[i], &m, sizeof(m)); \
} }