]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
set only valid coord
authorBastian Dehn <hhaalo@arcor.de>
Tue, 7 Oct 2025 18:56:34 +0000 (20:56 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Tue, 7 Oct 2025 18:56:34 +0000 (20:56 +0200)
src/kds_s2000w_handler.c
src/kds_s2000w_handler.h
src/kds_s2000w_handler_opts.c

index 4d8c5c234e569deee50f3df14108128cc2eff352..a657edfd02be4b890d327c4488e3acf63deaba85 100644 (file)
@@ -66,6 +66,7 @@ handler* kds_s2000w_handler_init()
        h->current_scan_status = malloc(sizeof(scanstatus));
        h->read_info = malloc(sizeof(readinfo));
        h->image = malloc(sizeof(imagedata));
+       h->coord = malloc(sizeof(coord_t));
 
        h->profile = 0;
        h->sessionid = 0;
@@ -80,6 +81,10 @@ handler* kds_s2000w_handler_init()
        h->read_info->read_size = 0;
        h->image->size = 0;
        h->image->data = NULL;
+       h->coord->offset_x = 0;
+       h->coord->offset_y = 0;
+       h->coord->width = 10;
+       h->coord->height = 10;
 
        return h;
 }
@@ -106,6 +111,8 @@ void kds_s2000w_handler_free(handler* h)
        h->image->data = NULL;
        free(h->image);
        h->image = NULL;
+       free(h->coord);
+       h->coord = NULL;
        free(h);
        h = NULL;
 }
index 9a155bfea32739f57fab5be98ff80e240d01e290..9bdeb2b536b3255bd273cfd43809af4a985d49a1 100644 (file)
@@ -35,6 +35,13 @@ typedef struct {
        uint32_t depth;
 } metadata;
 
+typedef struct {
+       int32_t offset_x;
+       int32_t offset_y;
+       int32_t width;
+       int32_t height;
+} coord_t;
+
 typedef struct {
        uint8_t current_image_number;
        uint8_t available_images;
@@ -57,6 +64,7 @@ typedef struct {
        scanstatus* current_scan_status;
        readinfo* read_info;
        imagedata* image;
+       coord_t* coord;
 } handler;
 
 handler* kds_s2000w_handler_init();
index c75698f34a11b3f61e281026fc7c57917a518ddc..04ed8ecf87b3e3b06df1af11ece3a7bfa30dceac 100644 (file)
@@ -8,6 +8,8 @@
 
 #define AUTOSTART_ON 1
 #define GUI_DPI 300
+#define MAX_WIDTH_INCH 85
+#define MAX_HEIGHT_INCH 140
 
 void _kds_s2000w_handler_opts_write_value_to_json(json_object* value_object, SANE_Value_Type value_type, void* value)
 {
@@ -37,6 +39,25 @@ void _kds_s2000w_handler_opts_write_value(json_object* value_object, SANE_Value_
        }
 }
 
+void _kds_s2000w_handler_opts_set_valid_coord(handler* h)
+{
+       bool valid = h->coord->offset_x + h->coord->width < MAX_WIDTH_INCH;
+       valid = valid && h->coord->offset_y + h->coord->height < MAX_HEIGHT_INCH;
+
+       if (!valid)
+               return;
+
+       json_object* config = json_object_object_get(h->current_scanner_config, "Configuration");
+       json_object* value_object = json_object_object_get(config, IMAGE_OFFSET_X);
+       json_object_set_int(value_object, h->coord->offset_x);
+       value_object = json_object_object_get(config, IMAGE_OFFSET_Y);
+       json_object_set_int(value_object, h->coord->offset_y);
+       value_object = json_object_object_get(config, IMAGE_WIDTH);
+       json_object_set_int(value_object, h->coord->width);
+       value_object = json_object_object_get(config, IMAGE_HEIGHT);
+       json_object_set_int(value_object, h->coord->height);
+}
+
 void _kds_s2000w_handler_opts_load_options(handler* h)
 {
        json_object_put(h->current_scanner_config);
@@ -433,7 +454,13 @@ void kds_s2000w_handler_opts_set_option(handler* h, uint32_t option, void* value
                || strcmp(descriptor->config_name, IMAGE_OFFSET_Y) == 0) {
                int32_t* int_value = (int32_t*) value;
                uint32_t tenth_inch = kds_s2000w_pixel_converter_pixel_to_tenth_inch(GUI_DPI, *int_value);
-               json_object_set_int(value_object, tenth_inch);
+               h->coord->offset_x = tenth_inch;
+       }
+
+       if (strcmp(descriptor->config_name, IMAGE_OFFSET_Y) == 0) {
+               int32_t* int_value = (int32_t*) value;
+               uint32_t tenth_inch = kds_s2000w_pixel_converter_pixel_to_tenth_inch(GUI_DPI, *int_value);
+               h->coord->offset_x = tenth_inch;
        }
 
        if (strcmp(descriptor->config_name, IMAGE_WIDTH) == 0) {
@@ -443,8 +470,7 @@ void kds_s2000w_handler_opts_set_option(handler* h, uint32_t option, void* value
                uint32_t offset_x_pixel = kds_s2000w_pixel_converter_tenth_inch_to_pixel(GUI_DPI, tenth_inch_offset_x);
                uint32_t width_pixel = *int_value - offset_x_pixel;
                uint32_t tenth_inch_width = kds_s2000w_pixel_converter_pixel_to_tenth_inch(GUI_DPI, width_pixel);
-               value_object = json_object_object_get(config, descriptor->config_name);
-               json_object_set_int(value_object, tenth_inch_width);
+               h->coord->width = tenth_inch_width;
        }
 
        if (strcmp(descriptor->config_name, IMAGE_HEIGHT) == 0) {
@@ -454,10 +480,11 @@ void kds_s2000w_handler_opts_set_option(handler* h, uint32_t option, void* value
                uint32_t offset_y_pixel = kds_s2000w_pixel_converter_tenth_inch_to_pixel(GUI_DPI, tenth_inch_offset_y);
                uint32_t height_pixel = *int_value - offset_y_pixel;
                uint32_t tenth_inch_height = kds_s2000w_pixel_converter_pixel_to_tenth_inch(GUI_DPI, height_pixel);
-               value_object = json_object_object_get(config, descriptor->config_name);
-               json_object_set_int(value_object, tenth_inch_height);
+               h->coord->height = tenth_inch_height;
        }
 
+       _kds_s2000w_handler_opts_set_valid_coord(h);
+
        if (strcmp(descriptor->config_name, RESET) == 0) {
                _kds_s2000w_handler_opts_set_option_to_default(h);
                config = json_object_object_get(h->current_scanner_config, "Configuration");