]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
change only load one image into handler
authorBastian Dehn <hhaalo@arcor.de>
Sat, 21 Dec 2024 08:45:08 +0000 (09:45 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Sat, 21 Dec 2024 08:49:42 +0000 (09:49 +0100)
src/kds_s2000w_handler.c
src/kds_s2000w_handler.h
src/kds_s2000w_net.c
tests/kds_s2000w_net_read_tests.c

index ee3bc32b2b6b2c01432bc608f922850d95304a6e..028e15a3faf042dbfca6e2ec661532b6a55354fa 100644 (file)
@@ -21,8 +21,11 @@ void _get_current_metadata(handler* h)
        json_object* config = json_object_object_get(h->current_scanner_config, "Configuration");
        json_object* metadata = json_object_object_get(config, "ColorMode");
        const char* color_value = json_object_get_string(metadata);
-       blobdata* scanner_image = (blobdata*) h->scanner_image;
-       blobdata* pnm_image = (blobdata*) h->pnm_image;
+       blobdata* image = (blobdata*) h->image;
+       blobdata* scanner_image = malloc(sizeof(blobdata));
+       scanner_image->data = malloc(sizeof(char) *  image->size);
+       scanner_image->size = image->size;
+       memcpy(scanner_image->data,  image->data,  image->size);
 
        h->current_metadata->valid = 0;
        h->current_metadata->channels = 1;
@@ -46,13 +49,13 @@ void _get_current_metadata(handler* h)
 
        if (h->current_metadata->depth > 8) {
                h->current_metadata->depth = 8;
-               kds_s2000w_convert_jpg_to_pnm_with_depth(scanner_image, pnm_image, h->current_metadata->depth);
+               kds_s2000w_convert_jpg_to_pnm_with_depth(scanner_image, image, h->current_metadata->depth);
        } else {
-               kds_s2000w_convert_tiff_to_pnm(scanner_image, pnm_image);
+               kds_s2000w_convert_tiff_to_pnm(scanner_image, image);
        }
 
 #ifndef NODEBUG
-       debug_printf_int(DEBUG, "size of pnm image", h->pnm_image->size);
+       debug_printf_int(DEBUG, "size of pnm image", image->size);
 #endif
 
        if (h->current_metadata->depth == 1)
@@ -65,6 +68,10 @@ void _get_current_metadata(handler* h)
        metadata = NULL;
        free(mdata);
        mdata = NULL;
+       free(scanner_image->data);
+       scanner_image->data = NULL;
+       free(scanner_image);
+       scanner_image = NULL;
 }
 
 void _delete_current_image(handler* h)
@@ -91,11 +98,9 @@ void _download_current_image(handler* h)
        debug_printf(ALL, "download_current_image");
 #endif
 
-       if (h->scanner_image->size > 0) {
-               free(h->scanner_image->data);
-               h->scanner_image->data = NULL;
-               h->scanner_image->size = 0;
-       }
+       free(h->image->data);
+       h->image->data = NULL;
+       h->image->size = 0;
 
        response* resp = kds_s2000w_client_response_init();
        kds_s2000w_client_get_image(h->sessionid, h->current_scan_status->current_image_number, resp);
@@ -103,9 +108,9 @@ void _download_current_image(handler* h)
        if (resp->error_size > 0)
                fprintf(stderr, "%s\n", resp->error_status);
 
-       h->scanner_image->size = resp->size;
-       h->scanner_image->data = malloc(sizeof(char) * resp->size);
-       memcpy(h->scanner_image->data, resp->data, resp->size);
+       h->image->size = resp->size;
+       h->image->data = malloc(sizeof(char) * resp->size);
+       memcpy(h->image->data, resp->data, resp->size);
 
        kds_s2000w_client_response_free(resp);
        resp = NULL;
@@ -158,8 +163,7 @@ handler* init_handler()
        h->current_scan_status = malloc(sizeof(scanstatus));
        h->current_metadata = malloc(sizeof(metadata));
        h->read_info = malloc(sizeof(readinfo));
-       h->scanner_image = malloc(sizeof(imagedata));
-       h->pnm_image = malloc(sizeof(imagedata));
+       h->image = malloc(sizeof(imagedata));
 
        h->sessionid = 0;
        h->state = NOTCONNECTED;
@@ -182,10 +186,8 @@ handler* init_handler()
        h->read_info->read_size = 0;
        h->read_info->readed_bytes_per_line = 0;
        h->read_info->readed_lines = 0;
-       h->scanner_image->size = 0;
-       h->scanner_image->data = NULL;
-       h->pnm_image->size = 0;
-       h->pnm_image->data = NULL;
+       h->image->size = 0;
+       h->image->data = NULL;
 
        return h;
 }
@@ -204,12 +206,10 @@ void free_handler(handler* h)
        h->current_metadata = NULL;
        free(h->read_info);
        h->read_info = NULL;
-       free(h->scanner_image->data);
-       h->scanner_image->data = NULL;
-       free(h->scanner_image);
-       h->scanner_image = NULL;
-       free(h->pnm_image);
-       h->pnm_image = NULL;
+       free(h->image->data);
+       h->image->data = NULL;
+       free(h->image);
+       h->image = NULL;
        free(h);
        h = NULL;
 }
@@ -220,10 +220,8 @@ void reset_handler(handler* h)
        debug_printf(ALL, "reset handler");
 #endif
 
-       free(h->pnm_image->data);
-       h->pnm_image->data = NULL;
-       free(h->scanner_image->data);
-       h->scanner_image->data = NULL;
+       free(h->image->data);
+       h->image->data = NULL;
 
        h->state = NOTCONNECTED;
        h->current_scan_status->load_options = 0;
@@ -336,10 +334,9 @@ void kds_s2000w_handler_open(const char* devicename, void** handle)
                return;
        }
 
-       h->scanner_image->size = 0;
-       h->scanner_image->data = NULL;
-       h->pnm_image->size = 0;
-       h->pnm_image->data = NULL;
+
+       h->image->size = 0;
+       h->image->data = NULL;
 
        h->current_scanner_config = json_tokener_parse(resp->data);
        json_object* value_object = NULL;
index 237cd3d4076b6b4a6880f38d795e41972053c269..bb06a8276a9b0443a8b78356d080a636cd277bf9 100644 (file)
@@ -53,8 +53,7 @@ typedef struct {
        scanstatus* current_scan_status;
        metadata* current_metadata;
        readinfo* read_info;
-       imagedata* scanner_image;
-       imagedata* pnm_image;
+       imagedata* image;
 } handler;
 
 handler* init_handler();
index 2d3236ea18fcac629aeff67def7005881ec4c39f..fcc2949bb7453a0f4d1e1548a227110cd93ecf00 100644 (file)
@@ -215,7 +215,7 @@ SANE_Status _sane_kds_s2000w_net_start(SANE_Handle handle)
                        break;
        }
 
-       if (h->current_metadata->valid == 0 || h->scanner_image->size == 0) {
+       if (h->current_metadata->valid == 0 || h->image->size == 0) {
                kds_s2000w_handler_recreate_session(h);
                return SANE_STATUS_NO_DOCS;
        }
@@ -258,29 +258,25 @@ SANE_Status _sane_kds_s2000w_net_read(SANE_Handle handle, SANE_Byte* data,
        }
 
        // calc max length
-       *length = h->pnm_image->size - h->read_info->read_size;
+       *length = h->image->size - h->read_info->read_size;
        if (*length > max_length)
                *length = max_length;
 
        // read image
-       if (h->pnm_image->data != NULL && h->read_info->read_size <= 0)
-               h->read_info->read_size += _sane_kds_s2000w_net_find_first_data_byte(h->pnm_image);
+       if (h->image->data != NULL && h->read_info->read_size <= 0)
+               h->read_info->read_size += _sane_kds_s2000w_net_find_first_data_byte(h->image);
 
-       if (h->read_info->read_size < h->pnm_image->size) {
-               memcpy(data, h->pnm_image->data + h->read_info->read_size, *length);
+       if (h->read_info->read_size < h->image->size) {
+               memcpy(data, h->image->data + h->read_info->read_size, *length);
                h->read_info->read_size += *length;
 
                return SANE_STATUS_GOOD;
        }
 
        // last frame
-       free(h->scanner_image->data);
-       h->scanner_image->data = NULL;
-       h->scanner_image->size = 0;
-
-       free(h->pnm_image->data);
-       h->pnm_image->data = NULL;
-       h->pnm_image->size = 0;
+       free(h->image->data);
+       h->image->data = NULL;
+       h->image->size = 0;
 
        *length = 0;
        return SANE_STATUS_EOF;
index 81fa72e96c83da30132268edd8c25cd84bfc7a28..3a11696178f11cc8eb66f2bec14766cc5d3c438e 100644 (file)
@@ -64,16 +64,16 @@ void sane_kds_s2000w_net_read_empty_image()
 {
        int length = 0;
        handler* h = init_handler();
-       h->pnm_image->data = malloc(sizeof(char));
-       h->pnm_image->size = 0;
+       h->image->data = malloc(sizeof(char));
+       h->image->size = 0;
        char* dataptr = malloc(sizeof(char));
 
        SANE_Status status = _sane_kds_s2000w_net_read(h, (void*) dataptr, 65536, &length);
 
        assert_int_equal(status, SANE_STATUS_EOF);
 
-       free(h->pnm_image->data);
-       h->pnm_image->data = NULL;
+       free(h->image->data);
+       h->image->data = NULL;
        free(dataptr);
        dataptr = NULL;
        free_handler(h);
@@ -83,9 +83,9 @@ void sane_kds_s2000w_net_read_empty_image()
 void sane_kds_s2000w_net_read_without_header_test()
 {
        handler* h = init_handler();
-       h->pnm_image->size = 65536;
-       h->pnm_image->data = malloc(sizeof(char) * 65536);
-       char* imagedata = (char*) h->pnm_image->data;
+       h->image->size = 65536;
+       h->image->data = malloc(sizeof(char) * 65536);
+       char* imagedata = (char*) h->image->data;
        for (int i = 0; i < 65536; i++) {
                imagedata[i] = 0xff;
        }
@@ -97,7 +97,7 @@ void sane_kds_s2000w_net_read_without_header_test()
        SANE_Status status = _sane_kds_s2000w_net_read(h, (void*) dataptr, maxlen, length);
 
        assert_int_equal(*length, 0);
-       assert_null(h->pnm_image->data);
+       assert_null(h->image->data);
        assert_int_equal(h->read_info->read_size, 65536);
        assert_int_equal(status, SANE_STATUS_EOF);
 
@@ -105,8 +105,8 @@ void sane_kds_s2000w_net_read_without_header_test()
        dataptr = NULL;
        free(length);
        length = NULL;
-       free(h->pnm_image->data);
-       h->pnm_image->data = NULL;
+       free(h->image->data);
+       h->image->data = NULL;
        free_handler(h);
        h = NULL;
 }
@@ -114,9 +114,9 @@ void sane_kds_s2000w_net_read_without_header_test()
 void sane_kds_s2000w_net_read_test()
 {
        handler* h = init_handler();
-       h->pnm_image->size = 65539;
-       h->pnm_image->data = malloc(sizeof(char) * 65539);
-       char* imagedata = (char*) h->pnm_image->data;
+       h->image->size = 65539;
+       h->image->data = malloc(sizeof(char) * 65539);
+       char* imagedata = (char*) h->image->data;
        for (int i = 0; i < 3; i++) {
                imagedata[i] = 0x0a;
        }
@@ -131,15 +131,15 @@ void sane_kds_s2000w_net_read_test()
        _sane_kds_s2000w_net_read(h, (void*) dataptr, maxlen, length);
 
        assert_int_equal(*length, 65536);
-       assert_memory_equal(dataptr, h->pnm_image->data + 3, 65536);
+       assert_memory_equal(dataptr, h->image->data + 3, 65536);
        assert_int_equal(h->read_info->read_size, 65539);
 
        free(dataptr);
        dataptr = NULL;
        free(length);
        length = NULL;
-       free(h->pnm_image->data);
-       h->pnm_image->data = NULL;
+       free(h->image->data);
+       h->image->data = NULL;
        free_handler(h);
        h = NULL;
 }
@@ -147,9 +147,9 @@ void sane_kds_s2000w_net_read_test()
 void sane_kds_s2000w_net_read_bw_test()
 {
        handler* h = init_handler();
-       h->pnm_image->size = 655541;
-       h->pnm_image->data = malloc(sizeof(char) * 65541);
-       char* imagedata = (char*) h->pnm_image->data;
+       h->image->size = 655541;
+       h->image->data = malloc(sizeof(char) * 65541);
+       char* imagedata = (char*) h->image->data;
        imagedata[0] = 'P';
        imagedata[1] = '4';
        for (int i = 2; i < 4; i++) {
@@ -166,15 +166,15 @@ void sane_kds_s2000w_net_read_bw_test()
        _sane_kds_s2000w_net_read(h, (void*) dataptr, maxlen, length);
 
        assert_int_equal(*length, 65536);
-       assert_memory_equal(dataptr, h->pnm_image->data + 5, 65536);
+       assert_memory_equal(dataptr, h->image->data + 5, 65536);
        assert_int_equal(h->read_info->read_size, 65540);
 
        free(dataptr);
        dataptr = NULL;
        free(length);
        length = NULL;
-       free(h->pnm_image->data);
-       h->pnm_image->data = NULL;
+       free(h->image->data);
+       h->image->data = NULL;
        free_handler(h);
        h = NULL;
 }
@@ -185,9 +185,9 @@ void sane_kds_s2000w_net_read_bytes_per_line_bigger_test()
        h->read_info->cancel = 0;
        h->read_info->read_size = 0;
        h->current_metadata->format = 0;
-       h->pnm_image->size = 95003;
-       h->pnm_image->data = malloc(sizeof(char) * 95003);
-       char* image = (char*) h->pnm_image->data;
+       h->image->size = 95003;
+       h->image->data = malloc(sizeof(char) * 95003);
+       char* image = (char*) h->image->data;
        for (int i = 0; i < 3; i++) {
                image[i] = 0x0a;
        }
@@ -206,8 +206,8 @@ void sane_kds_s2000w_net_read_bytes_per_line_bigger_test()
        dataptr = NULL;
        free(length);
        length = NULL;
-       free(h->pnm_image->data);
-       h->pnm_image->data = NULL;
+       free(h->image->data);
+       h->image->data = NULL;
        free_handler(h);
        h = NULL;
 }
\ No newline at end of file