diff --git a/huagao/CTwainUI.cpp b/huagao/CTwainUI.cpp index f56c077e..8cce69a1 100644 --- a/huagao/CTwainUI.cpp +++ b/huagao/CTwainUI.cpp @@ -34,6 +34,10 @@ CTwainUI::CTwainUI(TwGlue glue, GScanCap caps, std::string confirmtitle, std::st CTwainUI::~CTwainUI() { + m_pageBrightness.reset(); + m_pageBasic.reset(); + m_pageFeedPaper.reset(); + m_pageImageProc.reset(); } void CTwainUI::DoDataExchange(CDataExchange* pDX) @@ -655,3 +659,11 @@ void CTwainUI::OnClose() // TODO: 在此添加消息处理程序代码和/或调用默认值 m_glue.m_cancel(); } + + +LRESULT CTwainUI::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) +{ + // TODO: 在此添加专用代码和/或调用基类 + + return CDialogEx::WindowProc(message, wParam, lParam); +} diff --git a/huagao/CTwainUI.h b/huagao/CTwainUI.h index c3ddb044..5ccead96 100644 --- a/huagao/CTwainUI.h +++ b/huagao/CTwainUI.h @@ -211,4 +211,5 @@ private: std::unique_ptr settings; std::string m_confirmtitle; + virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam); }; diff --git a/huagao/Device/GScanO200.cpp b/huagao/Device/GScanO200.cpp index e93185a7..ff24bd28 100644 --- a/huagao/Device/GScanO200.cpp +++ b/huagao/Device/GScanO200.cpp @@ -299,7 +299,7 @@ std::string GScanO200::GetSerialNum() if (m_usb.get() && m_usb->is_connected()) { std::lock_guard lck(m_imgLocker); - if (SerialNum.empty()) { + //if (SerialNum.empty()) { #ifndef G200 SerialNum.resize(14); #else // !G200 @@ -309,7 +309,7 @@ std::string GScanO200::GetSerialNum() m_usb->write_bulk(&usbcb, sizeof(usbcb)); m_usb->read_bulk(&SerialNum[0], SerialNum.size()); - } + //} return SerialNum; } return ""; diff --git a/huagao/Device/ImageMatQueue.cpp b/huagao/Device/ImageMatQueue.cpp index 7cdbc918..8eaae9b9 100644 --- a/huagao/Device/ImageMatQueue.cpp +++ b/huagao/Device/ImageMatQueue.cpp @@ -92,10 +92,13 @@ ImageMatQueue::~ImageMatQueue(void) } } +static int paperIndex = 0; void ImageMatQueue::pushMat(std::shared_ptr data) { m_rawBuffs.Put(data); atm_orgin_image_remains++; + //string paperindexinfo = "Get the index of "+to_string(++paperIndex)+" Paper"; + //FileTools::write_log("D:\\1.txt", paperindexinfo); } std::shared_ptr> ImageMatQueue::popBmpdata() @@ -117,6 +120,8 @@ void ImageMatQueue::clear() void ImageMatQueue::setparam(const GScanCap& param) { + paperIndex = 0; + //FileTools::write_log("D:\\1.txt", "RESET PAPER COUNT"); scanParam = param; //scanParam.is_duplex = 0; //scanParam.imageRotateDegree = 90.0f; @@ -289,14 +294,13 @@ void ImageMatQueue::proc() mat.release(); #else // G200 //resize(mat, mat, cv::Size(), 1.001, 1.0070);//ܻСƥ ϵ׶ - //cv::imwrite("D:\\bb\\"+to_string(++index)+".jpg", mat); - mats.push_back(mat); + //cv::imwrite("D:\\org"+to_string(++index)+".jpg", mat); Mat front = mat(Rect(0, 0, mat.cols / 2, mat.rows)); Mat back = mat(Rect(mat.cols / 2, 0, mat.cols / 2, mat.rows)); - if (scanParam.imageRotateDegree != 0.0&&scanParam.imageRotateDegree!=180.0) { - cv::flip(front, front, 0); - cv::flip(front, front, 1); - } + //if (scanParam.imageRotateDegree != 0.0&&scanParam.imageRotateDegree!=180.0) { + // cv::flip(front, front, 0); + // cv::flip(front, front, 1); + //} mats.push_back(front); mats.push_back(back); #endif @@ -315,6 +319,7 @@ void ImageMatQueue::proc() for (int j = 0; j < m_iaList.size(); j++) { m_iaList[j]->apply(mats, scanParam.is_duplex); } + for (int i = 0; i < mats.size(); i++) { if (!scanParam.is_duplex && i == 1) { diff --git a/huagao/Device/PublicFunc.h b/huagao/Device/PublicFunc.h index aaa4f99a..bce587b5 100644 --- a/huagao/Device/PublicFunc.h +++ b/huagao/Device/PublicFunc.h @@ -148,7 +148,7 @@ enum PaperAlign :byte { typedef enum Enchace_Color :short { - Enhance_None, + Enhance_None=0, Enhance_Red, Enhance_Green, Enhance_Blue diff --git a/huagao/TiffG4Compression.cpp b/huagao/TiffG4Compression.cpp deleted file mode 100644 index 93f7704c..00000000 --- a/huagao/TiffG4Compression.cpp +++ /dev/null @@ -1,516 +0,0 @@ -#include "TiffG4Compression.h" -#include -#include -#include - -#ifdef TIME -struct timeval t1, t2; -struct timezone tz; -#endif - -TiffG4Compression::TiffG4Compression() -{ -} - - -TiffG4Compression::~TiffG4Compression() -{ -} - -int TiffG4Compression::G4Compress(unsigned char * indata, int inbytes, int width, int height, unsigned char * outdata, int * outbytes) -{ - struct uncompressed_descriptor uncompressed; - struct compressed_descriptor compressed; - - uncompressed.pixels_per_line = width; - uncompressed.number_of_lines = height; - uncompressed.data = indata; - comp_alloc_flag = NOALLOC; - comp_write_init_flag = 1; - read_uncompressed_file_into_memory(&uncompressed); - compressed.data = outdata; - control_compression(&uncompressed, &compressed); - *outbytes = compressed.length_in_bytes; - return *outbytes; -} - -void TiffG4Compression::control_compression(uncompressed_descriptor * uncompressed, compressed_descriptor * compressed) -{ - struct parameters sole_parameters; - struct parameters *params = &sole_parameters; - -#ifdef TIME - SHORT i; - tz.tz_minuteswest = 0; - tz.tz_dsttime = 0; - gettimeofday(&t1, &tz); -#endif - - prepare_to_compress(uncompressed, compressed, params); - compress_image(uncompressed, compressed, params); - /* memory deallocation added by Michael D. Garris 2/26/90 */ - free(params->reference_line); - free(params->coding_line); -#ifdef TIME - gettimeofday(&t2, &tz); - printf("\ntime difference: %ld:%ld\n", t2.tv_sec - t1.tv_sec, - t2.tv_usec - t1.tv_usec); - for (i = 0; i < 5; i++) printf("%c", '\07'); */ -#endif -} - -void TiffG4Compression::read_uncompressed_file_into_memory(uncompressed_descriptor * uncompressed) -{ - int file_size; - - if (comp_alloc_flag) - { - file_size = uncompressed->pixels_per_line * uncompressed->number_of_lines - / Pixels_per_byte; - if ((uncompressed->data = (unsigned char *)calloc(file_size, sizeof(char))) == NULL) - { - printf("\nCannot allocate enough memory for uncomp file.\n"); - } - } - else if (uncompressed->data == NULL) - { - printf("\nNo memory allocated for input data!\n"); - } -} - -void TiffG4Compression::prepare_to_compress(uncompressed_descriptor * uncompressed, compressed_descriptor * compressed, parameters * params) -{ - params->max_pixel = uncompressed->pixels_per_line; - compressed->pixels_per_line = uncompressed->pixels_per_line; - compressed->number_of_lines = uncompressed->number_of_lines; - - set_up_first_line_c(params); - prepare_to_write_bits_c(compressed); -} - -void TiffG4Compression::compress_image(uncompressed_descriptor * uncompressed, compressed_descriptor * compressed, parameters * params) -{ - SHORT line; - for (line = 0; line < uncompressed->number_of_lines; line++) - { - make_array_of_changing_elements(params, uncompressed, line); - set_up_first_and_last_changing_elements_c(params); - compress_line(params); - prepare_to_compress_next_line(params); - } /* end for each line loop */ - write_bits_c(const_cast("000000000001000000000001")); - compressed->length_in_bytes = flush_buffer(); -} - -void TiffG4Compression::make_array_of_changing_elements(parameters * params, uncompressed_descriptor * uncompressed, SHORT line_number) -{ - SHORT bytes_per_line; - int line_offset; - SHORT byte_offset; - - bytes_per_line = params->max_pixel / Pixels_per_byte; - line_offset = bytes_per_line * line_number; - for (byte_offset = 0; byte_offset < bytes_per_line; byte_offset++) - { - process_char(*(uncompressed->data + line_offset + byte_offset), params); - } -} - -void TiffG4Compression::set_up_first_and_last_changing_elements_c(parameters * params) -{ - *(params->coding_line) = Invalid; - *(params->coding_line + ++params->index) = params->max_pixel; - *(params->coding_line + ++params->index) = params->max_pixel; - *(params->coding_line + ++params->index) = params->max_pixel; -} - -void TiffG4Compression::prepare_to_compress_next_line(parameters * params) -{ - SHORT *temp; - - /* swap the reference and unchanged coding lines */ - - temp = params->reference_line; - params->reference_line = params->coding_line; - params->coding_line = temp; - - params->pixel = 0; - params->index = 0; - params->previous_color = White; -} - -void TiffG4Compression::set_up_first_line_c(parameters * params) -{ - params->reference_line = - (SHORT *)malloc((params->max_pixel + Extra_positions) * sizeof(SHORT)); - params->coding_line = - (SHORT *)malloc((params->max_pixel + Extra_positions) * sizeof(SHORT)); - - *(params->reference_line + 0) = Invalid; - *(params->reference_line + 1) = params->max_pixel; - *(params->reference_line + 2) = params->max_pixel; - *(params->reference_line + 3) = params->max_pixel; - - /* initialize first changing element on coding line (A0 = -1) */ - *(params->coding_line) = Invalid; - - params->pixel = 0; - params->index = 0; - params->previous_color = White; -} - -void TiffG4Compression::compress_line(parameters * params) -{ -#if Debug - static SHORT line = 0; - printf("\nLINE %d. ", line); - line++; -#endif - - A_0 = Invalid; /* set A0 equal to imaginary first array element */ - A0_color = White; - A_1 = 1; - initialize_b1(params); - b2 = b1 + 1; - -#if Debug - printf("\nA0:%d A1:%d b1:%d b2:%d ", - A0, *(params->coding_line + A1), - *(params->reference_line + b1), *(params->reference_line + b2)); -#endif - - do { - if (*(params->reference_line + b2) < *(params->coding_line + A_1)) - { - pass_mode_c(params); - continue; - } - else - if (abs(*(params->coding_line + A_1) - *(params->reference_line + b1)) <= 3) - vertical_mode_c(params); - else - horizontal_mode_c(params); -#if Debug - printf("\nA0:%d A1:%d b1:%d b2:%d ", A0, *(params->coding_line + A1), - *(params->reference_line + b1), *(params->reference_line + b2)); -#endif - - } while (A_0 < params->max_pixel); -} - -void TiffG4Compression::initialize_b1(parameters * params) -{ - SHORT last_bit_of_b1; - - b1 = 1; - last_bit_of_b1 = b1 & Last_bit_mask; - - while (((*(params->reference_line + b1) <= A_0) || (A0_color == last_bit_of_b1)) - && (*(params->reference_line + b1) < params->max_pixel)) - { - b1++; - last_bit_of_b1 = b1 & Last_bit_mask; - } /* end while loop */ - -#if Debug - printf("\nb1:%d :%d, A0:%d", b1, *(params->reference_line + b1), A0); -#endif -} - -void TiffG4Compression::pass_mode_c(parameters * params) -{ - write_bits_c(const_cast("0001")); - -#if Debug - printf(" P "); -#endif - - /* - * Reset the value A0 points to to a'0 (the value that b2 points to). - */ - - A_0 = *(params->reference_line + b2); - - /* - * Since A0 is now greater than the pixel b1 points to, both b1 and b2 - * must be advanced twice to maintain the color difference between A0 and - * b1, and the positional requirement that b1 point to a pixel greater than - * the one A0 points to. - */ - - b1 += 2; - b2 += 2; - - /* - * Note that the b's can be advanced by two positions without fear of - * moving them beyond the last changing element because pass_mode cannot - * occur if b2 is already pointing to max_pixel. - */ -} - -void TiffG4Compression::vertical_mode_c(parameters * params) -{ - SHORT difference; - - difference = *(params->coding_line + A_1) - *(params->reference_line + b1); - A_0 = *(params->coding_line + A_1); - A0_color = !A0_color; - A_1++; - -#if Debug - printf(" V%d ", difference); -#endif - switch (difference) { - case 0: - write_bits_c(const_cast("1")); - if (*(params->reference_line + b1) != params->max_pixel) - { - b1++; - b2++; - } /* end if b1 is not on the last changing element */ - break; - - case 1: - write_bits_c(const_cast("011")); - b1++; - b2++; - if ((*(params->reference_line + b1) <= A_0) && - (*(params->reference_line + b1) != params->max_pixel)) - { - b1 += 2; - b2 += 2; - } - break; - - case -1: - write_bits_c(const_cast("010")); - if (*(params->reference_line + b1) != params->max_pixel) - { - b1++; - b2++; - } /* end if b1 is not on the last changing element */ - break; - - case 2: - write_bits_c(const_cast("000011")); - b1++; - b2++; - if ((*(params->reference_line + b1) <= A_0) && - (*(params->reference_line + b1) != params->max_pixel)) - { - b1 += 2; - b2 += 2; - } - break; - - case -2: - write_bits_c(const_cast("000010")); - if (*(params->reference_line + b1 - 1) > A_0) - { - b1--; - b2--; - } - else if (*(params->reference_line + b1) != params->max_pixel) - { - b1++; - b2++; - } - break; - - case 3: - write_bits_c(const_cast("0000011")); - b1++; - b2++; - while ((*(params->reference_line + b1) <= A_0) && - (*(params->reference_line + b1) != params->max_pixel)) - { - b1 += 2; - b2 += 2; - } - break; - - case -3: - write_bits_c(const_cast("0000010")); - if (*(params->reference_line + b1 - 1) > A_0) - { - b1--; - b2--; - } - else if (*(params->reference_line + b1) != params->max_pixel) - { - b1++; - b2++; - } - break; - - default: - printf("ERROR in vertical_mode_c() "); - - } /* end case of difference */ -} - -void TiffG4Compression::horizontal_mode_c(parameters * params) -{ - SHORT run_length; - -#if Debug - printf(" a2:%d H ", *(params->coding_line + a2)); -#endif - - a2 = A_1 + 1; - write_bits_c(const_cast("001")); - - if (A_0 == Invalid) /* on imaginary first pixel */ - run_length = *(params->coding_line + A_1); - else - run_length = *(params->coding_line + A_1) - A_0; - write_run_length(run_length, A0_color); - /* the last bit contains the color of the changing element */ - - run_length = *(params->coding_line + a2) - *(params->coding_line + A_1); - write_run_length(run_length, !A0_color); - - /* - * Must use !A0_color instead of A1 because in cases in which A1 occurs - * on max_pixel, its color is bogus. - */ - - /* NOTE: is the above statement true? if A1 were on max_pixel, you should - not get horizontal mode. */ - - - A_0 = *(params->coding_line + a2); - A_1 = a2 + 1; - - while ((*(params->reference_line + b1) <= *(params->coding_line + a2)) && - (*(params->reference_line + b1) < params->max_pixel)) - { - b1 += 2; /* must move ahead by 2 to maintain color difference with */ - b2 += 2; /* A0, whose color does not change in this mode. */ - } - -} - -void TiffG4Compression::prepare_to_write_bits_c(compressed_descriptor * compressed) -{ - if (comp_alloc_flag) { - compressed->data = (unsigned char*)calloc((compressed->pixels_per_line * - compressed->number_of_lines / Pixels_per_byte), sizeof(unsigned char)); - } - /* - * This allocation is usually very wasteful, but because there is no - * way of knowing how much space is needed, I decided to be generous. - */ - - if (compressed->data == NULL) { - printf("\nMemory allocation error for compressed output data.\n"); - //crash_c(); - } - output_area = (char*)compressed->data; -} - -void TiffG4Compression::write_bits_c(char * string_ptr) -{ - /* global switch added by Michael D. Garris 2/26/90 */ - if (comp_write_init_flag) - { - bit_place_mark = 0; - byte_place_mark = 0; - comp_write_init_flag = 0; - } - while (*string_ptr != '\0') - { - if (*string_ptr == '1') - *(output_area + byte_place_mark) |= write_one[bit_place_mark]; - else - *(output_area + byte_place_mark) &= write_zero[bit_place_mark]; - if (bit_place_mark == Last_bit_in_a_byte) - { - bit_place_mark = 0; - byte_place_mark++; - } /* end if byte is full */ - else - bit_place_mark++; - string_ptr++; - } /* end while */ -} - -unsigned int TiffG4Compression::flush_buffer() -{ - SHORT i; - - if (bit_place_mark != 0) { - for (i = bit_place_mark; i < Pixels_per_byte; i++) - *(output_area + byte_place_mark) &= write_zero[i]; - /* - * pad the rest of the last byte with '0' bits. - */ - ++byte_place_mark; - } - return byte_place_mark; -} - -void TiffG4Compression::write_run_length(SHORT length, SHORT color) -{ - SHORT multiples_of_largest_code, i, - make_up_code_index, remainder; - - multiples_of_largest_code = length / Largest_code; - length %= Largest_code; - for (i = 0; i < multiples_of_largest_code; i++) - write_bits_c(const_cast(largest_colorless_code)); - - remainder = length % Size_of_make_up_code_increments; - - /* remainder in the range 0 - 63 */ - - make_up_code_index = length / Size_of_make_up_code_increments; - - /* - * make_up_code_index in the range 0 - 39, and represents a run length - * of 64 times its value (i.e. 0 - 2496). To translate this value into - * an index into the arrays that store the bit sequence that represents - * the appropriate run length, 1 must be subtracted from make_up_code_ - * index. If this results in the value -1, no make up code should be - * written. - */ - - make_up_code_index--; - - if (make_up_code_index != Invalid) { - if (color == White) - write_bits_c(const_cast(white_make_up_code[make_up_code_index])); - else - write_bits_c(const_cast(black_make_up_code[make_up_code_index])); - } - - if (color == White) - write_bits_c(const_cast(white_terminating_code[remainder])); - else - write_bits_c(const_cast(black_terminating_code[remainder])); -} - -void TiffG4Compression::process_char(unsigned char data_byte, parameters * params) -{ - static char color = 0; - SHORT i = 0; - - color = -(data_byte & Last_bit_mask); - data_byte ^= params->previous_color; - - /* if the previous color is black - which is contrary to our assumptions - - * the bits in the byte must all be changed so that the result, when used - * as an index into the array 'bytes,' yields the correct result. In the - * above operation, if the previous color is black (11111111b), all bits - * are changed; if the previous color is white (00000000b), no bits are - * changed. */ - - while (table[data_byte].pixel[i] != Invalid) - *(params->coding_line + ++params->index) = - params->pixel + table[data_byte].pixel[i++]; - - params->pixel += Pixels_per_byte; - params->previous_color = color; - - /* 'color' is a temporary holding place for the value of previous color */ -} diff --git a/huagao/TiffG4Compression.h b/huagao/TiffG4Compression.h deleted file mode 100644 index f87afd92..00000000 --- a/huagao/TiffG4Compression.h +++ /dev/null @@ -1,668 +0,0 @@ -#pragma once -#define White 0 -#define Black 1 -#define Black_byte 255 // byte of all black bits: 11111111b - - -/* WARNING */ -/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ -/* !!!!!!!!!!!!!!!!!! Change Here. Redeclaration of Type. !!!!!!!!!!!!!!!! */ -#define SHORT int /* this type was just a regular old C "short". */ - /* In images with > 2^15 rows the 2 byte definition */ - /* gave garbage output because short overflowed. */ - /* Increasing all variables from 2 to 4 bytes seems */ - /* to fix it. I have used the macro SHORT here to show */ - /* where this change applies, so that it can be undone */ - /* if desired. Some variables of type "int" existed in */ - /* the code before this change, and the SHORT macro */ - /* allows reversal of just the correct ones. */ - /* Patrick Grother Dec 9 1994 */ -/* !!!!!!!!!!!!!!!!!! Change Here. Redeclaration of Type. !!!!!!!!!!!!!!!! */ -/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ - - -#define Largest_code 2560 -#define Size_of_make_up_code_increments 64 -#define Max_terminating_length 63 /* longest terminating code*/ -#define Number_of_different_bytes 256 - -#define Pixels_per_byte 8 -#define Bits_per_byte 8 -#define Last_bit_in_a_byte 7 /* assumes bits numbered from 0 - 7 */ -#define Last_bit_mask 1 /* masks the last (low magnitude) bit */ -#define Default_width_in_pixels 2560 /* default width of a scan line */ -#define Default_number_of_lines 3300 /* default length of an image */ - -#define Invalid -1 -#define Extra_positions 25 /* ensures extra room in allocations */ -#define Not_done_yet 0 - -#define VL3 -3 /* Vertical Left 3 mode */ -#define VL2 -2 /* Vertical Left 2 mode */ -#define VL1 -1 /* Vertical Left 1 mode */ -#define V0 0 /* Vertical mode */ -#define VR1 1 /* Vertical Right 1 mode */ -#define VR2 2 /* Vertical Right 2 mode */ -#define VR3 3 /* Vertical Right 3 mode */ -#define P 4 /* Pass mode */ -#define H 5 /* Horizontal mode */ -#define EOFB 6 /* End Of File Buffer */ - -#define No_offset 0 /* no offset during fseek() */ -#define End_of_file 2 /* start at EOF during fseek() */ -#define Start_of_file 0 /* start at SOF during fseek() */ - -/* -unsigned char *calloc(); -SHORT *malloc(); -*/ - -struct parameters { - SHORT previous_color; /* color of last run of pixels */ - SHORT index; /* indicates current position in "coding_line" */ - SHORT max_pixel; /* the number of pixels in a scan line */ - SHORT pixel; /* pixel number of the last changing element */ - SHORT *reference_line; /* array of changing elements on reference line */ - SHORT *coding_line; /* array of changing elements on coding line */ -}; - -struct compressed_descriptor { - unsigned char *data; /* pointer to compressed image */ - SHORT pixels_per_line; /* the number of pixels in a scan line */ - SHORT number_of_lines; /* the number of scan lines in the image */ - int length_in_bytes; /* length of the compressed image in bytes */ -}; - -struct uncompressed_descriptor { - unsigned char *data; /* pointer to uncompressed image */ - SHORT pixels_per_line; /* the number of pixels in a scan line */ - SHORT number_of_lines; /* the number of scan lines in the image */ -}; - -static const char write_one[Pixels_per_byte] = -{ - (char)0x80, - (char)0x40, - (char)0x20, - (char)0x10, - (char)0x8, - (char)0x4, - (char)0x2, - (char)0x1, -}; - -static const char write_zero[Pixels_per_byte] = -{ - (char)0x7F, - (char)0xBF, - (char)0xDF, - (char)0xEF, - (char)0xF7, - (char)0xFB, - (char)0xFD, - (char)0xFE, -}; - -static const char *white_terminating_code[64] = -{ - "00110101", - "000111", - "0111", - "1000", - "1011", - "1100", - "1110", - "1111", - "10011", - "10100", - "00111", - "01000", - "001000", - "000011", - "110100", - "110101", - "101010", - "101011", - "0100111", - "0001100", - "0001000", - "0010111", - "0000011", - "0000100", - "0101000", - "0101011", - "0010011", - "0100100", - "0011000", - "00000010", - "00000011", - "00011010", - "00011011", - "00010010", - "00010011", - "00010100", - "00010101", - "00010110", - "00010111", - "00101000", - "00101001", - "00101010", - "00101011", - "00101100", - "00101101", - "00000100", - "00000101", - "00001010", - "00001011", - "01010010", - "01010011", - "01010100", - "01010101", - "00100100", - "00100101", - "01011000", - "01011001", - "01011010", - "01011011", - "01001010", - "01001011", - "00110010", - "00110011", - "00110100", -};/* end array of white terminating code */ - - -static const char *black_terminating_code[64] = -{ - "0000110111", - "010", - "11", - "10", - "011", - "0011", - "0010", - "00011", - "000101", - "000100", - "0000100", - "0000101", - "0000111", - "00000100", - "00000111", - "000011000", - "0000010111", - "0000011000", - "0000001000", - "00001100111", - "00001101000", - "00001101100", - "00000110111", - "00000101000", - "00000010111", - "00000011000", - "000011001010", - "000011001011", - "000011001100", - "000011001101", - "000001101000", - "000001101001", - "000001101010", - "000001101011", - "000011010010", - "000011010011", - "000011010100", - "000011010101", - "000011010110", - "000011010111", - "000001101100", - "000001101101", - "000011011010", - "000011011011", - "000001010100", - "000001010101", - "000001010110", - "000001010111", - "000001100100", - "000001100101", - "000001010010", - "000001010011", - "000000100100", - "000000110111", - "000000111000", - "000000100111", - "000000101000", - "000001011000", - "000001011001", - "000000101011", - "000000101100", - "000001011010", - "000001100110", - "000001100111", -}; /* end black_terminating_array */ - - -static const char *white_make_up_code[40] = -{ - "11011", - "10010", - "010111", - "0110111", - "00110110", - "00110111", - "01100100", - "01100101", - "01101000", - "01100111", - "011001100", - "011001101", - "011010010", - "011010011", - "011010100", - "011010101", - "011010110", - "011010111", - "011011000", - "011011001", - "011011010", - "011011011", - "010011000", - "010011001", - "010011010", - "011000", - "010011011", - - /* - * from this line on, the codes are colorless and represnt runs from - * 1792 pixels to 2560 pixels. In other words, the longest run length - * codes have been added onto both the white make up codes and the black - * make up codes. This has been done to make the procedure - * "write_run_length()" easier to write and to understand. No other - * procedure in the compression algorithm is affected by this merging of - * different types of run length codes, and the compatibility of the - * program is in no way effected. - */ - - "00000001000", - "00000001100", - "00000001101", - "000000010010", - "000000010011", - "000000010100", - "000000010101", - "000000010110", - "000000010111", - "000000011100", - "000000011101", - "000000011110", - "000000011111", -}; /* end case of white makeup code */ - - -static const char *black_make_up_code[40] = -{ - "0000001111", - "000011001000", - "000011001001", - "000001011011", - "000000110011", - "000000110100", - "000000110101", - "0000001101100", - "0000001101101", - "0000001001010", - "0000001001011", - "0000001001100", - "0000001001101", - "0000001110010", - "0000001110011", - "0000001110100", - "0000001110101", - "0000001110110", - "0000001110111", - "0000001010010", - "0000001010011", - "0000001010100", - "0000001010101", - "0000001011010", - "0000001011011", - "0000001100100", - "0000001100101", - - /* - * from this line on, the codes are colorless and represnt runs from - * 1792 pixels to 2560 pixels. In other words, the longest run length - * codes have been added onto both the white make up codes and the black - * make up codes. This has been done to make the procedure - * "write_run_length()" easier to write and to understand. No other - * procedure in the compression algorithm is affected by this merging of - * different types of run length codes, and the compatibility of the - * program is in no way compromised. - */ - - "00000001000", - "00000001100", - "00000001101", - "000000010010", - "000000010011", - "000000010100", - "000000010101", - "000000010110", - "000000010111", - "000000011100", - "000000011101", - "000000011110", - "000000011111", -}; /* end black makeup code */ - - - - -struct byte_descriptor { - SHORT pixel[9]; -}; - - -static const struct byte_descriptor table[Number_of_different_bytes] = -{ - -1, -1, -1, -1, -1, -1, -1, -1, -1, - 7, -1, -1, -1, -1, -1, -1, -1, -1, - 6, 7, -1, -1, -1, -1, -1, -1, -1, - 6, -1, -1, -1, -1, -1, -1, -1, -1, - 5, 6, -1, -1, -1, -1, -1, -1, -1, - 5, 6, 7, -1, -1, -1, -1, -1, -1, - 5, 7, -1, -1, -1, -1, -1, -1, -1, - 5, -1, -1, -1, -1, -1, -1, -1, -1, - 4, 5, -1, -1, -1, -1, -1, -1, -1, - 4, 5, 7, -1, -1, -1, -1, -1, -1, - 4, 5, 6, 7, -1, -1, -1, -1, -1, - 4, 5, 6, -1, -1, -1, -1, -1, -1, - 4, 6, -1, -1, -1, -1, -1, -1, -1, - 4, 6, 7, -1, -1, -1, -1, -1, -1, - 4, 7, -1, -1, -1, -1, -1, -1, -1, - 4, -1, -1, -1, -1, -1, -1, -1, -1, - 3, 4, -1, -1, -1, -1, -1, -1, -1, - 3, 4, 7, -1, -1, -1, -1, -1, -1, - 3, 4, 6, 7, -1, -1, -1, -1, -1, - 3, 4, 6, -1, -1, -1, -1, -1, -1, - 3, 4, 5, 6, -1, -1, -1, -1, -1, - 3, 4, 5, 6, 7, -1, -1, -1, -1, - 3, 4, 5, 7, -1, -1, -1, -1, -1, - 3, 4, 5, -1, -1, -1, -1, -1, -1, - 3, 5, -1, -1, -1, -1, -1, -1, -1, - 3, 5, 7, -1, -1, -1, -1, -1, -1, - 3, 5, 6, 7, -1, -1, -1, -1, -1, - 3, 5, 6, -1, -1, -1, -1, -1, -1, - 3, 6, -1, -1, -1, -1, -1, -1, -1, - 3, 6, 7, -1, -1, -1, -1, -1, -1, - 3, 7, -1, -1, -1, -1, -1, -1, -1, - 3, -1, -1, -1, -1, -1, -1, -1, -1, - 2, 3, -1, -1, -1, -1, -1, -1, -1, - 2, 3, 7, -1, -1, -1, -1, -1, -1, - 2, 3, 6, 7, -1, -1, -1, -1, -1, - 2, 3, 6, -1, -1, -1, -1, -1, -1, - 2, 3, 5, 6, -1, -1, -1, -1, -1, - 2, 3, 5, 6, 7, -1, -1, -1, -1, - 2, 3, 5, 7, -1, -1, -1, -1, -1, - 2, 3, 5, -1, -1, -1, -1, -1, -1, - 2, 3, 4, 5, -1, -1, -1, -1, -1, - 2, 3, 4, 5, 7, -1, -1, -1, -1, - 2, 3, 4, 5, 6, 7, -1, -1, -1, - 2, 3, 4, 5, 6, -1, -1, -1, -1, - 2, 3, 4, 6, -1, -1, -1, -1, -1, - 2, 3, 4, 6, 7, -1, -1, -1, -1, - 2, 3, 4, 7, -1, -1, -1, -1, -1, - 2, 3, 4, -1, -1, -1, -1, -1, -1, - 2, 4, -1, -1, -1, -1, -1, -1, -1, - 2, 4, 7, -1, -1, -1, -1, -1, -1, - 2, 4, 6, 7, -1, -1, -1, -1, -1, - 2, 4, 6, -1, -1, -1, -1, -1, -1, - 2, 4, 5, 6, -1, -1, -1, -1, -1, - 2, 4, 5, 6, 7, -1, -1, -1, -1, - 2, 4, 5, 7, -1, -1, -1, -1, -1, - 2, 4, 5, -1, -1, -1, -1, -1, -1, - 2, 5, -1, -1, -1, -1, -1, -1, -1, - 2, 5, 7, -1, -1, -1, -1, -1, -1, - 2, 5, 6, 7, -1, -1, -1, -1, -1, - 2, 5, 6, -1, -1, -1, -1, -1, -1, - 2, 6, -1, -1, -1, -1, -1, -1, -1, - 2, 6, 7, -1, -1, -1, -1, -1, -1, - 2, 7, -1, -1, -1, -1, -1, -1, -1, - 2, -1, -1, -1, -1, -1, -1, -1, -1, - 1, 2, -1, -1, -1, -1, -1, -1, -1, - 1, 2, 7, -1, -1, -1, -1, -1, -1, - 1, 2, 6, 7, -1, -1, -1, -1, -1, - 1, 2, 6, -1, -1, -1, -1, -1, -1, - 1, 2, 5, 6, -1, -1, -1, -1, -1, - 1, 2, 5, 6, 7, -1, -1, -1, -1, - 1, 2, 5, 7, -1, -1, -1, -1, -1, - 1, 2, 5, -1, -1, -1, -1, -1, -1, - 1, 2, 4, 5, -1, -1, -1, -1, -1, - 1, 2, 4, 5, 7, -1, -1, -1, -1, - 1, 2, 4, 5, 6, 7, -1, -1, -1, - 1, 2, 4, 5, 6, -1, -1, -1, -1, - 1, 2, 4, 6, -1, -1, -1, -1, -1, - 1, 2, 4, 6, 7, -1, -1, -1, -1, - 1, 2, 4, 7, -1, -1, -1, -1, -1, - 1, 2, 4, -1, -1, -1, -1, -1, -1, - 1, 2, 3, 4, -1, -1, -1, -1, -1, - 1, 2, 3, 4, 7, -1, -1, -1, -1, - 1, 2, 3, 4, 6, 7, -1, -1, -1, - 1, 2, 3, 4, 6, -1, -1, -1, -1, - 1, 2, 3, 4, 5, 6, -1, -1, -1, - 1, 2, 3, 4, 5, 6, 7, -1, -1, - 1, 2, 3, 4, 5, 7, -1, -1, -1, - 1, 2, 3, 4, 5, -1, -1, -1, -1, - 1, 2, 3, 5, -1, -1, -1, -1, -1, - 1, 2, 3, 5, 7, -1, -1, -1, -1, - 1, 2, 3, 5, 6, 7, -1, -1, -1, - 1, 2, 3, 5, 6, -1, -1, -1, -1, - 1, 2, 3, 6, -1, -1, -1, -1, -1, - 1, 2, 3, 6, 7, -1, -1, -1, -1, - 1, 2, 3, 7, -1, -1, -1, -1, -1, - 1, 2, 3, -1, -1, -1, -1, -1, -1, - 1, 3, -1, -1, -1, -1, -1, -1, -1, - 1, 3, 7, -1, -1, -1, -1, -1, -1, - 1, 3, 6, 7, -1, -1, -1, -1, -1, - 1, 3, 6, -1, -1, -1, -1, -1, -1, - 1, 3, 5, 6, -1, -1, -1, -1, -1, - 1, 3, 5, 6, 7, -1, -1, -1, -1, - 1, 3, 5, 7, -1, -1, -1, -1, -1, - 1, 3, 5, -1, -1, -1, -1, -1, -1, - 1, 3, 4, 5, -1, -1, -1, -1, -1, - 1, 3, 4, 5, 7, -1, -1, -1, -1, - 1, 3, 4, 5, 6, 7, -1, -1, -1, - 1, 3, 4, 5, 6, -1, -1, -1, -1, - 1, 3, 4, 6, -1, -1, -1, -1, -1, - 1, 3, 4, 6, 7, -1, -1, -1, -1, - 1, 3, 4, 7, -1, -1, -1, -1, -1, - 1, 3, 4, -1, -1, -1, -1, -1, -1, - 1, 4, -1, -1, -1, -1, -1, -1, -1, - 1, 4, 7, -1, -1, -1, -1, -1, -1, - 1, 4, 6, 7, -1, -1, -1, -1, -1, - 1, 4, 6, -1, -1, -1, -1, -1, -1, - 1, 4, 5, 6, -1, -1, -1, -1, -1, - 1, 4, 5, 6, 7, -1, -1, -1, -1, - 1, 4, 5, 7, -1, -1, -1, -1, -1, - 1, 4, 5, -1, -1, -1, -1, -1, -1, - 1, 5, -1, -1, -1, -1, -1, -1, -1, - 1, 5, 7, -1, -1, -1, -1, -1, -1, - 1, 5, 6, 7, -1, -1, -1, -1, -1, - 1, 5, 6, -1, -1, -1, -1, -1, -1, - 1, 6, -1, -1, -1, -1, -1, -1, -1, - 1, 6, 7, -1, -1, -1, -1, -1, -1, - 1, 7, -1, -1, -1, -1, -1, -1, -1, - 1, -1, -1, -1, -1, -1, -1, -1, -1, - 0, 1, -1, -1, -1, -1, -1, -1, -1, - 0, 1, 7, -1, -1, -1, -1, -1, -1, - 0, 1, 6, 7, -1, -1, -1, -1, -1, - 0, 1, 6, -1, -1, -1, -1, -1, -1, - 0, 1, 5, 6, -1, -1, -1, -1, -1, - 0, 1, 5, 6, 7, -1, -1, -1, -1, - 0, 1, 5, 7, -1, -1, -1, -1, -1, - 0, 1, 5, -1, -1, -1, -1, -1, -1, - 0, 1, 4, 5, -1, -1, -1, -1, -1, - 0, 1, 4, 5, 7, -1, -1, -1, -1, - 0, 1, 4, 5, 6, 7, -1, -1, -1, - 0, 1, 4, 5, 6, -1, -1, -1, -1, - 0, 1, 4, 6, -1, -1, -1, -1, -1, - 0, 1, 4, 6, 7, -1, -1, -1, -1, - 0, 1, 4, 7, -1, -1, -1, -1, -1, - 0, 1, 4, -1, -1, -1, -1, -1, -1, - 0, 1, 3, 4, -1, -1, -1, -1, -1, - 0, 1, 3, 4, 7, -1, -1, -1, -1, - 0, 1, 3, 4, 6, 7, -1, -1, -1, - 0, 1, 3, 4, 6, -1, -1, -1, -1, - 0, 1, 3, 4, 5, 6, -1, -1, -1, - 0, 1, 3, 4, 5, 6, 7, -1, -1, - 0, 1, 3, 4, 5, 7, -1, -1, -1, - 0, 1, 3, 4, 5, -1, -1, -1, -1, - 0, 1, 3, 5, -1, -1, -1, -1, -1, - 0, 1, 3, 5, 7, -1, -1, -1, -1, - 0, 1, 3, 5, 6, 7, -1, -1, -1, - 0, 1, 3, 5, 6, -1, -1, -1, -1, - 0, 1, 3, 6, -1, -1, -1, -1, -1, - 0, 1, 3, 6, 7, -1, -1, -1, -1, - 0, 1, 3, 7, -1, -1, -1, -1, -1, - 0, 1, 3, -1, -1, -1, -1, -1, -1, - 0, 1, 2, 3, -1, -1, -1, -1, -1, - 0, 1, 2, 3, 7, -1, -1, -1, -1, - 0, 1, 2, 3, 6, 7, -1, -1, -1, - 0, 1, 2, 3, 6, -1, -1, -1, -1, - 0, 1, 2, 3, 5, 6, -1, -1, -1, - 0, 1, 2, 3, 5, 6, 7, -1, -1, - 0, 1, 2, 3, 5, 7, -1, -1, -1, - 0, 1, 2, 3, 5, -1, -1, -1, -1, - 0, 1, 2, 3, 4, 5, -1, -1, -1, - 0, 1, 2, 3, 4, 5, 7, -1, -1, - 0, 1, 2, 3, 4, 5, 6, 7, -1, - 0, 1, 2, 3, 4, 5, 6, -1, -1, - 0, 1, 2, 3, 4, 6, -1, -1, -1, - 0, 1, 2, 3, 4, 6, 7, -1, -1, - 0, 1, 2, 3, 4, 7, -1, -1, -1, - 0, 1, 2, 3, 4, -1, -1, -1, -1, - 0, 1, 2, 4, -1, -1, -1, -1, -1, - 0, 1, 2, 4, 7, -1, -1, -1, -1, - 0, 1, 2, 4, 6, 7, -1, -1, -1, - 0, 1, 2, 4, 6, -1, -1, -1, -1, - 0, 1, 2, 4, 5, 6, -1, -1, -1, - 0, 1, 2, 4, 5, 6, 7, -1, -1, - 0, 1, 2, 4, 5, 7, -1, -1, -1, - 0, 1, 2, 4, 5, -1, -1, -1, -1, - 0, 1, 2, 5, -1, -1, -1, -1, -1, - 0, 1, 2, 5, 7, -1, -1, -1, -1, - 0, 1, 2, 5, 6, 7, -1, -1, -1, - 0, 1, 2, 5, 6, -1, -1, -1, -1, - 0, 1, 2, 6, -1, -1, -1, -1, -1, - 0, 1, 2, 6, 7, -1, -1, -1, -1, - 0, 1, 2, 7, -1, -1, -1, -1, -1, - 0, 1, 2, -1, -1, -1, -1, -1, -1, - 0, 2, -1, -1, -1, -1, -1, -1, -1, - 0, 2, 7, -1, -1, -1, -1, -1, -1, - 0, 2, 6, 7, -1, -1, -1, -1, -1, - 0, 2, 6, -1, -1, -1, -1, -1, -1, - 0, 2, 5, 6, -1, -1, -1, -1, -1, - 0, 2, 5, 6, 7, -1, -1, -1, -1, - 0, 2, 5, 7, -1, -1, -1, -1, -1, - 0, 2, 5, -1, -1, -1, -1, -1, -1, - 0, 2, 4, 5, -1, -1, -1, -1, -1, - 0, 2, 4, 5, 7, -1, -1, -1, -1, - 0, 2, 4, 5, 6, 7, -1, -1, -1, - 0, 2, 4, 5, 6, -1, -1, -1, -1, - 0, 2, 4, 6, -1, -1, -1, -1, -1, - 0, 2, 4, 6, 7, -1, -1, -1, -1, - 0, 2, 4, 7, -1, -1, -1, -1, -1, - 0, 2, 4, -1, -1, -1, -1, -1, -1, - 0, 2, 3, 4, -1, -1, -1, -1, -1, - 0, 2, 3, 4, 7, -1, -1, -1, -1, - 0, 2, 3, 4, 6, 7, -1, -1, -1, - 0, 2, 3, 4, 6, -1, -1, -1, -1, - 0, 2, 3, 4, 5, 6, -1, -1, -1, - 0, 2, 3, 4, 5, 6, 7, -1, -1, - 0, 2, 3, 4, 5, 7, -1, -1, -1, - 0, 2, 3, 4, 5, -1, -1, -1, -1, - 0, 2, 3, 5, -1, -1, -1, -1, -1, - 0, 2, 3, 5, 7, -1, -1, -1, -1, - 0, 2, 3, 5, 6, 7, -1, -1, -1, - 0, 2, 3, 5, 6, -1, -1, -1, -1, - 0, 2, 3, 6, -1, -1, -1, -1, -1, - 0, 2, 3, 6, 7, -1, -1, -1, -1, - 0, 2, 3, 7, -1, -1, -1, -1, -1, - 0, 2, 3, -1, -1, -1, -1, -1, -1, - 0, 3, -1, -1, -1, -1, -1, -1, -1, - 0, 3, 7, -1, -1, -1, -1, -1, -1, - 0, 3, 6, 7, -1, -1, -1, -1, -1, - 0, 3, 6, -1, -1, -1, -1, -1, -1, - 0, 3, 5, 6, -1, -1, -1, -1, -1, - 0, 3, 5, 6, 7, -1, -1, -1, -1, - 0, 3, 5, 7, -1, -1, -1, -1, -1, - 0, 3, 5, -1, -1, -1, -1, -1, -1, - 0, 3, 4, 5, -1, -1, -1, -1, -1, - 0, 3, 4, 5, 7, -1, -1, -1, -1, - 0, 3, 4, 5, 6, 7, -1, -1, -1, - 0, 3, 4, 5, 6, -1, -1, -1, -1, - 0, 3, 4, 6, -1, -1, -1, -1, -1, - 0, 3, 4, 6, 7, -1, -1, -1, -1, - 0, 3, 4, 7, -1, -1, -1, -1, -1, - 0, 3, 4, -1, -1, -1, -1, -1, -1, - 0, 4, -1, -1, -1, -1, -1, -1, -1, - 0, 4, 7, -1, -1, -1, -1, -1, -1, - 0, 4, 6, 7, -1, -1, -1, -1, -1, - 0, 4, 6, -1, -1, -1, -1, -1, -1, - 0, 4, 5, 6, -1, -1, -1, -1, -1, - 0, 4, 5, 6, 7, -1, -1, -1, -1, - 0, 4, 5, 7, -1, -1, -1, -1, -1, - 0, 4, 5, -1, -1, -1, -1, -1, -1, - 0, 5, -1, -1, -1, -1, -1, -1, -1, - 0, 5, 7, -1, -1, -1, -1, -1, -1, - 0, 5, 6, 7, -1, -1, -1, -1, -1, - 0, 5, 6, -1, -1, -1, -1, -1, -1, - 0, 6, -1, -1, -1, -1, -1, -1, -1, - 0, 6, 7, -1, -1, -1, -1, -1, -1, - 0, 7, -1, -1, -1, -1, -1, -1, -1, - 0, -1, -1, -1, -1, -1, -1, -1, -1, -}; /* end of data for list of byte descriptors */ - -// Constants -#define NOALLOC 0 -#define ALLOC 1 - -class TiffG4Compression -{ -public: - TiffG4Compression(); - ~TiffG4Compression(); -public: - int G4Compress(unsigned char *indata, int inbytes, int width, int height, unsigned char *outdata, int *outbytes); -private: - void control_compression(struct uncompressed_descriptor *uncompressed, struct compressed_descriptor *compressed); - void read_uncompressed_file_into_memory(struct uncompressed_descriptor *uncompressed); - void prepare_to_compress(struct uncompressed_descriptor* uncompressed,struct compressed_descriptor* compressed,struct parameters* params); - void compress_image(struct uncompressed_descriptor* uncompressed, struct compressed_descriptor* compressed, struct parameters* params); - void make_array_of_changing_elements(struct parameters *params, struct uncompressed_descriptor* uncompressed, SHORT line_number); - void set_up_first_and_last_changing_elements_c(struct parameters *params); - void prepare_to_compress_next_line(struct parameters *params); - void set_up_first_line_c(struct parameters *params); - void compress_line(struct parameters *params); - void initialize_b1(struct parameters *params); - void pass_mode_c(struct parameters *params); - void vertical_mode_c(struct parameters *params); - void horizontal_mode_c(struct parameters *params); - void prepare_to_write_bits_c(struct compressed_descriptor *compressed); - void write_bits_c(char *string_ptr); - unsigned int flush_buffer(); - void write_run_length(SHORT length, SHORT color); - void process_char(unsigned char data_byte, struct parameters *params); -private: - const char* largest_colorless_code = - { - "000000011111" - }; - - int comp_alloc_flag = ALLOC; - int comp_write_init_flag; - char *output_area=nullptr; - int bit_place_mark; - int byte_place_mark; - SHORT A_0, A0_color, A_1, a2, b1, b2; -}; - diff --git a/huagao/g4codec.c b/huagao/g4codec.c deleted file mode 100644 index 4361d729..00000000 --- a/huagao/g4codec.c +++ /dev/null @@ -1,188 +0,0 @@ - -/* - * main program to drive CCITT Group 4 decompression - */ - -#include "g4codec.h" -#include -#include -#include -#include -#include - - #define _BYTE_ASCII - -#ifdef _BYTE_ASCII -#define _BYTE_ZERO 0x30 -#define _BYTE_ONE 0x31 -#else -#define _BYTE_ZERO 0x00 // _BYTE_BINARY -#define _BYTE_ONE 0x01 // _BYTE_BINARY -#endif - -/* - * This Program extracts a compressed image from the file pointer passed, - * stripping the file header and loads it into memory. It gets the - * dimensions of the resultant pixrect image from the compressed header - * and allocates the pixrect structure. It reverses the bits in the - * compressed image and calls decompress4() for decompression. The - * pixrect structure is used so the image can be displayed. - */ - -int convertFromByte(char *inData, char *outData, int total, int width, int height) -{ - int in, out, maxOut; - char outByte; - int inVal; - - maxOut = width * height; - outByte = 0; - out = 0; - for (in=0; in -#include -#include -#ifdef TIME -#include -#endif -#include "g4codec.h" -#include "grp4comp.h" - -/* Added by MDG in order have option of passing alloc responsibilities to caller */ -#define NOALLOC 0 -#define ALLOC 1 -int comp_alloc_flag = ALLOC; -int comp_write_init_flag; -#ifdef TIME - struct timeval t1, t2; - struct timezone tz; -#endif - - - -/*********************************************************************** -* grp4comp is the main routine of this file. It does pre- * -* liminary setup, calls routines, and does final processing. * -************************************************************************/ - -/*********************************************************************** -* Arguments * -* --------- * -* Passed in: * -* indata - buffer containing the uncompressed data. * -* inbytes - the number of bytes in indata. * -* width - Width in pixels of scan line in indata. * -* height - Number of lines in indata. * -* Returned: * -* outdata - buffer containing the compressed data. * -* outbytes - the number of bytes in outdata. * -************************************************************************/ - -int grp4comp(unsigned char* indata, - int inbytes, - int width, - int height, - unsigned char* outdata, - int *outbytes) -{ - struct uncompressed_descriptor uncompressed; - struct compressed_descriptor compressed; - - uncompressed.pixels_per_line = width; - uncompressed.number_of_lines = height; - uncompressed.data = indata; - comp_alloc_flag = NOALLOC; - comp_write_init_flag = 1; - read_uncompressed_file_into_memory( &uncompressed); - compressed.data = outdata; - control_compression( &uncompressed, &compressed ); - *outbytes = compressed.length_in_bytes; - return *outbytes; -/* printf("\ncompressed:lines: %d, pixels:%d, length:%d\n", - compressed.number_of_lines, compressed.pixels_per_line, - compressed.length_in_bytes); */ -} - - -/***************************** control_compression ************************** - - calls the functions that compress the image - -*****************************************************************************/ -/*********************************************************************** -* Arguments * -* --------- * -* Passed in: * -* uncompressed - structure containing the # of pixels * -* per line, the number of lines, and * -* the uncompressed data. * -* Returned: * -* compressed - structure containing the # of pixels * -* per line, the number of lines, and * -* the compressed data. * -************************************************************************/ -void control_compression(struct uncompressed_descriptor *uncompressed, - struct compressed_descriptor *compressed ) -{ - struct parameters sole_parameters; - struct parameters *params = &sole_parameters; - -#ifdef TIME - SHORT i; - tz.tz_minuteswest = 0; - tz.tz_dsttime = 0; - gettimeofday(&t1, &tz); -#endif - - prepare_to_compress( uncompressed, compressed, params ); - compress_image( uncompressed, compressed, params ); - /* memory deallocation added by Michael D. Garris 2/26/90 */ - free(params->reference_line); - free(params->coding_line); -#ifdef TIME - gettimeofday(&t2, &tz); - printf("\ntime difference: %ld:%ld\n", t2.tv_sec - t1.tv_sec, - t2.tv_usec - t1.tv_usec); - for(i=0; i<5; i++) printf("%c",'\07');*/ -#endif - -} - - -/************************ read_uncompressed_file_into_memory ******************* - - allocates memory for the uncompressed image. - -*****************************************************************************/ -/*********************************************************************** -* Arguments * -* --------- * -* Passed in: * -* uncompressed - structure containing the # of pixels * -* per line, the number of lines, and * -* the uncompressed data. * -* Returned: * -* uncompressed - structure containing the # of pixels * -* per line, the number of lines, and * -* the compressed data. * -************************************************************************/ -void read_uncompressed_file_into_memory(struct uncompressed_descriptor *uncompressed) -{ - int file_size; - - if(comp_alloc_flag) - { - file_size = uncompressed->pixels_per_line * uncompressed->number_of_lines - / Pixels_per_byte; - - if((uncompressed->data = (char *)calloc( file_size, sizeof(char)))== NULL) - { - printf("\nCannot allocate enough memory for uncomp file.\n"); - crash_c(); - } - } else if(uncompressed->data == NULL) - { - printf("\nNo memory allocated for input data!\n"); - crash_c(); - } -} /* end read_uncompressed_file_into_memory() */ - - - -/*************************** prepare_to_compress **************************** - - initializes variables in preperation for compression - -*****************************************************************************/ -/*********************************************************************** -* Arguments * -* --------- * -* Passed in: * -* uncompressed - structure containing the # of pixels * -* per line, the number of lines, and * -* the uncompressed data. * -* Returned: * -* compressed - structure containing the # of pixels * -* per line, the number of lines, and * -* the compressed data. * -* params - structure storing information needed for * -* comparison and other tasks. * -************************************************************************/ -void prepare_to_compress(struct uncompressed_descriptor *uncompressed, - struct compressed_descriptor *compressed, - struct parameters *params) -{ - - params->max_pixel = uncompressed->pixels_per_line; - compressed->pixels_per_line = uncompressed->pixels_per_line; - compressed->number_of_lines = uncompressed->number_of_lines; - - set_up_first_line_c( params ); - prepare_to_write_bits_c( compressed ); - -} /* end prepare_to_compress() */ - - - -/****************************** compress_image ******************************* - - compresses the image - -*****************************************************************************/ -/*********************************************************************** -* Arguments * -* --------- * -* Passed in: * -* uncompressed - structure containing the # of pixels * -* per line, the number of lines, and * -* the uncompressed data. * -* Returned: * -* compressed - structure containing the # of pixels * -* per line, the number of lines, and * -* the compressed data. * -* params - structure storing information need for * -* comparison and other tasks. * -************************************************************************/ -void compress_image(struct uncompressed_descriptor *uncompressed, - struct compressed_descriptor *compressed, - struct parameters *params) -{ - SHORT line; - - for(line = 0; line < uncompressed->number_of_lines; line++) - { - make_array_of_changing_elements( params, uncompressed, line ); - set_up_first_and_last_changing_elements_c( params ); - compress_line( params ); - prepare_to_compress_next_line( params ); - } /* end for each line loop */ - - write_bits_c("000000000001000000000001"); - compressed->length_in_bytes = flush_buffer(); -} - - - -/************************ make_array_of_changing_elements ********************* - - stores in a list pointed to by "params->coding_line" the pixel numbers - of all the changing elements in the coding line - -*****************************************************************************/ -/*********************************************************************** -* Arguments * -* --------- * -* Passed in: * -* uncompressed - structure containing the # of pixels * -* per line, the number of lines, and * -* the uncompressed data. * -* line_number - the number of the line in the image * -* Returned: * -* params - structure storing information need for * -* comparison and other tasks. * -************************************************************************/ -void make_array_of_changing_elements(struct parameters *params, - struct uncompressed_descriptor *uncompressed, - SHORT line_number) -{ - SHORT bytes_per_line; - int line_offset; - SHORT byte_offset; - - bytes_per_line = params->max_pixel / Pixels_per_byte; - line_offset = bytes_per_line * line_number; - for(byte_offset=0; byte_offset < bytes_per_line; byte_offset++) - { - process_char(*(uncompressed->data+line_offset+byte_offset),params); - } - -} /* end make_array_of_changing_elements() */ - - - -/******************* set_up_first_and_last_changing_elements_c ***************** - - initializes the first and last changing elements in the coding line - -******************************************************************************/ -/*********************************************************************** -* Arguments * -* --------- * -* Passed in: * -* params - structure storing information need for * -* comparison and other tasks. * -* Returned: * -* params - structure storing information need for * -* comparison and other tasks. * -************************************************************************/ -void set_up_first_and_last_changing_elements_c(struct parameters *params) -{ - *(params->coding_line) = Invalid; - *(params->coding_line + ++params->index) = params->max_pixel; - *(params->coding_line + ++params->index) = params->max_pixel; - *(params->coding_line + ++params->index) = params->max_pixel; - - /* the previous lines may be necessary if when searching for b1, you - skip some elements because you know that they are the wrong color */ -} - - -/************************ prepare_to_compress_next_line *********************** - - initializes variables in preperation for compressing another line - -******************************************************************************/ -/*********************************************************************** -* Arguments * -* --------- * -* Passed in: * -* params - structure storing information need for * -* comparison and other tasks. * -* Returned: * -* params - structure storing information need for * -* comparison and other tasks. * -************************************************************************/ -void prepare_to_compress_next_line(struct parameters *params) -{ - SHORT *temp; - - /* swap the reference and unchanged coding lines */ - - temp = params->reference_line; - params->reference_line = params->coding_line; - params->coding_line = temp; - - params->pixel = 0; - params->index = 0; - params->previous_color = White; - -} /* end prepare_to_read_next_line() */ - - - -/******************************* set_up_first_line_c *************************** - - initializes variables in preperation for compressing the first line - -******************************************************************************/ -/*********************************************************************** -* Arguments * -* --------- * -* Passed in: * -* params - structure storing information need for * -* comparison and other tasks. * -* Returned: * -* params - structure storing information need for * -* comparison and other tasks. * -************************************************************************/ -void set_up_first_line_c(struct parameters *params) -{ - - params->reference_line = - (SHORT *)malloc( (params->max_pixel + Extra_positions) * sizeof(SHORT) ); - params->coding_line = - (SHORT *)malloc( (params->max_pixel + Extra_positions) * sizeof(SHORT) ); - - *(params->reference_line + 0) = Invalid; - *(params->reference_line + 1) = params->max_pixel; - *(params->reference_line + 2) = params->max_pixel; - *(params->reference_line + 3) = params->max_pixel; - - /* initialize first changing element on coding line (A0 = -1) */ - *(params->coding_line) = Invalid; - - params->pixel = 0; - params->index = 0; - params->previous_color = White; - -} /* end set_up_first_line_c() */ - - - -/*********************************** crash_c *********************************** - - forces the program to crash and create a core file - -*****************************************************************************/ -void crash_c() -{ - FILE *crash_program = NULL; - fprintf(crash_program,"This will kill the program and create a core file"); -} - - -/***************************************************************************/ -/* Originally mode.c */ -/***************************************************************************/ - -static SHORT A0, A0_color, - A1, a2, - b1, b2; - - - -/******************************* compress_line ******************************** - - compresses a single line of the image - -*****************************************************************************/ -void compress_line(struct parameters *params) -{ - -#if Debug - static SHORT line = 0; - printf("\nLINE %d. ",line); - line++; -#endif - - A0 = Invalid; /* set A0 equal to imaginary first array element */ - A0_color = White; - A1 = 1; - initialize_b1(params); - b2 = b1 + 1; - -#if Debug - printf("\nA0:%d A1:%d b1:%d b2:%d ", - A0, *(params->coding_line+A1), - *(params->reference_line+b1),*(params->reference_line+b2)); -#endif - - do { - if (*(params->reference_line + b2) < *(params->coding_line + A1)) - { - pass_mode_c(params); - continue; - } else - if (abs(*(params->coding_line+A1)-*(params->reference_line+b1)) <=3) - vertical_mode_c(params); - else - horizontal_mode_c(params); -#if Debug - printf("\nA0:%d A1:%d b1:%d b2:%d ", A0, *(params->coding_line+A1), - *(params->reference_line+b1),*(params->reference_line+b2)); -#endif - - } while( A0 < params->max_pixel); - -} - - -/******************************* initialize_b1 ******************************** - - locates b1's first position in the reference line - -*****************************************************************************/ -void initialize_b1(struct parameters *params) -{ - SHORT last_bit_of_b1; - - b1 = 1; - last_bit_of_b1 = b1 & Last_bit_mask; - - while( ((*(params->reference_line +b1) <=A0) || (A0_color ==last_bit_of_b1)) - && (*(params->reference_line + b1) < params->max_pixel) ) - { - b1++; - last_bit_of_b1 = b1 & Last_bit_mask; - } /* end while loop */ - -#if Debug - printf("\nb1:%d :%d, A0:%d", b1, *(params->reference_line+b1), A0); -#endif - -} - - -/********************************** pass_mode_c ******************************** - - compresses a pass mode - -*****************************************************************************/ -void pass_mode_c(struct parameters *params) -{ - write_bits_c("0001"); - -#if Debug - printf(" P "); -#endif - - /* - * Reset the value A0 points to to a'0 (the value that b2 points to). - */ - - A0 = *(params->reference_line + b2); - - /* - * Since A0 is now greater than the pixel b1 points to, both b1 and b2 - * must be advanced twice to maintain the color difference between A0 and - * b1, and the positional requirement that b1 point to a pixel greater than - * the one A0 points to. - */ - - b1 += 2; - b2 += 2; - - /* - * Note that the b's can be advanced by two positions without fear of - * moving them beyond the last changing element because pass_mode cannot - * occur if b2 is already pointing to max_pixel. - */ - -} - - -/****************************** vertical_mode_c ******************************** - - compresses a vertical mode - -*****************************************************************************/ -void vertical_mode_c(struct parameters *params) -{ - SHORT difference; - - difference = *(params->coding_line + A1) - *(params->reference_line + b1); - A0 = *(params->coding_line + A1); - A0_color = !A0_color; - A1++; - -#if Debug - printf(" V%d ", difference); -#endif - - switch(difference) { - - case 0: - write_bits_c("1"); - if(*(params->reference_line + b1) != params->max_pixel ) - { - b1++; - b2++; - } /* end if b1 is not on the last changing element */ - break; - - case 1: - write_bits_c("011"); - b1++; - b2++; - if((*(params->reference_line + b1) <= A0) && - (*(params->reference_line + b1) != params->max_pixel) ) - { - b1 += 2; - b2 += 2; - } - break; - - case -1: - write_bits_c("010"); - if(*(params->reference_line + b1) != params->max_pixel ) - { - b1++; - b2++; - } /* end if b1 is not on the last changing element */ - break; - - case 2: - write_bits_c("000011"); - b1++; - b2++; - if((*(params->reference_line + b1) <= A0) && - (*(params->reference_line + b1) != params->max_pixel) ) - { - b1 += 2; - b2 += 2; - } - break; - - case -2: - write_bits_c("000010"); - if(*(params->reference_line + b1 - 1) > A0 ) - { - b1--; - b2--; - } else if(*(params->reference_line + b1) != params->max_pixel) - { - b1++; - b2++; - } - break; - - case 3: - write_bits_c("0000011"); - b1++; - b2++; - while ((*(params->reference_line + b1) <= A0) && - (*(params->reference_line + b1) != params->max_pixel) ) - { - b1 += 2; - b2 += 2; - } - break; - - case -3: - write_bits_c("0000010"); - if(*(params->reference_line + b1 - 1) > A0 ) - { - b1--; - b2--; - } else if(*(params->reference_line + b1) != params->max_pixel) - { - b1++; - b2++; - } - break; - - default: - printf("ERROR in vertical_mode_c() "); - - } /* end case of difference */ - -} - - -/**************************** horizontal_mode_c ******************************** - - compresses a horizontal mode - -*****************************************************************************/ -void horizontal_mode_c(struct parameters *params) -{ - SHORT run_length; - -#if Debug - printf(" a2:%d H ",*(params->coding_line + a2)); -#endif - - a2 = A1 + 1; - write_bits_c("001"); - - if(A0 == Invalid) /* on imaginary first pixel */ - run_length = *(params->coding_line + A1); - else - run_length = *(params->coding_line + A1) - A0; - write_run_length(run_length, A0_color ); - /* the last bit contains the color of the changing element */ - - run_length = *(params->coding_line + a2) - *(params->coding_line + A1); - write_run_length(run_length, !A0_color); - - /* - * Must use !A0_color instead of A1 because in cases in which A1 occurs - * on max_pixel, its color is bogus. - */ - - /* NOTE: is the above statement true? if A1 were on max_pixel, you should - not get horizontal mode. */ - - - A0 = *(params->coding_line + a2); - A1 = a2 + 1; - - while((*(params->reference_line + b1) <= *(params->coding_line + a2)) && - ( *(params->reference_line + b1) < params->max_pixel) ) - { - b1 += 2; /* must move ahead by 2 to maintain color difference with */ - b2 += 2; /* A0, whose color does not change in this mode. */ - } - -} - - -/***************************************************************************/ -/* Originally write_bits_c.c */ -/***************************************************************************/ - -static SHORT bit_place_mark; - -static int byte_place_mark; - -static unsigned char *output_area; - -static char write_one[Pixels_per_byte] = -{ - (char)0x80, - (char)0x40, - (char)0x20, - (char)0x10, - (char)0x8, - (char)0x4, - (char)0x2, - (char)0x1, -}; - -static char write_zero[Pixels_per_byte] = -{ - (char)0x7F, - (char)0xBF, - (char)0xDF, - (char)0xEF, - (char)0xF7, - (char)0xFB, - (char)0xFD, - (char)0xFE, -}; - - -/*************************** prepare_to_write_bits_c ************************** - - initializes variables in preperation for writing compressed images - -*****************************************************************************/ -void prepare_to_write_bits_c(struct compressed_descriptor *compressed) -{ - if(comp_alloc_flag){ - (char *)compressed->data = calloc( (compressed->pixels_per_line * - compressed->number_of_lines / Pixels_per_byte), sizeof(char) ); - } - /* - * This allocation is usually very wasteful, but because there is no - * way of knowing how much space is needed, I decided to be generous. - */ - - if (compressed->data == NULL){ - printf("\nMemory allocation error for compressed output data.\n"); - crash_c(); - } - output_area = compressed->data; -} - - - -/******************************** write_bits_c ********************************** - - writes a variable length series of bits represented by a string of '1's - and '0's, which it receives as a parameter - -*****************************************************************************/ -void write_bits_c(char *string_ptr) -{ - /* global switch added by Michael D. Garris 2/26/90 */ - if(comp_write_init_flag) - { - bit_place_mark = 0; - byte_place_mark = 0; - comp_write_init_flag = 0; - } - while(*string_ptr != '\0') - { - if(*string_ptr == '1') - *(output_area + byte_place_mark) |= write_one[bit_place_mark]; - else - *(output_area + byte_place_mark) &= write_zero[bit_place_mark]; - if(bit_place_mark == Last_bit_in_a_byte) - { - bit_place_mark = 0; - byte_place_mark++; - } /* end if byte is full */ - else - bit_place_mark++; - string_ptr++; - } /* end while */ - -} - - -/******************************** flush_buffer ******************************* - - writes to memory whatever bits are left in the bit buffer followed by - enough zero-bits to pad the compressed image out to a byte boundary. - -*****************************************************************************/ -unsigned int flush_buffer() -{ - SHORT i; - - if (bit_place_mark != 0) { - for (i=bit_place_mark; icoding_line" - -******************************************************************************/ -void process_char(unsigned char data_byte, - struct parameters *params) -{ - static char color = 0; - SHORT i = 0; - - color = -(data_byte & Last_bit_mask); - data_byte ^= params->previous_color; - -/* if the previous color is black - which is contrary to our assumptions - -* the bits in the byte must all be changed so that the result, when used -* as an index into the array 'bytes,' yields the correct result. In the -* above operation, if the previous color is black (11111111b), all bits -* are changed; if the previous color is white (00000000b), no bits are -* changed. */ - - while(table[data_byte].pixel[i] != Invalid) - *( params->coding_line + ++params->index ) = - params->pixel + table[data_byte].pixel[i++]; - - params->pixel += Pixels_per_byte; - params->previous_color = color; - - /* 'color' is a temporary holding place for the value of previous color */ -} diff --git a/huagao/grp4comp.h b/huagao/grp4comp.h deleted file mode 100644 index fcdafec2..00000000 --- a/huagao/grp4comp.h +++ /dev/null @@ -1,133 +0,0 @@ -/*********************************************************************/ -/* grp4comp.h */ -/* Originally compression.h */ -/*********************************************************************/ - -#include - - -/* WARNING */ -/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ -/* !!!!!!!!!!!!!!!!!! Change Here. Redeclaration of Type. !!!!!!!!!!!!!!!! */ -#define SHORT int /* this type was just a regular old C "short". */ - /* In images with > 2^15 rows the 2 byte definition */ - /* gave garbage output because short overflowed. */ - /* Increasing all variables from 2 to 4 bytes seems */ - /* to fix it. I have used the macro SHORT here to show */ - /* where this change applies, so that it can be undone */ - /* if desired. Some variables of type "int" existed in */ - /* the code before this change, and the SHORT macro */ - /* allows reversal of just the correct ones. */ - /* Patrick Grother Dec 9 1994 */ -/* !!!!!!!!!!!!!!!!!! Change Here. Redeclaration of Type. !!!!!!!!!!!!!!!! */ -/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ - - -#define Largest_code 2560 -#define Size_of_make_up_code_increments 64 -#define Max_terminating_length 63 /* longest terminating code*/ -#define Number_of_different_bytes 256 - -#define Pixels_per_byte 8 -#define Bits_per_byte 8 -#define Last_bit_in_a_byte 7 /* assumes bits numbered from 0 - 7 */ -#define Last_bit_mask 1 /* masks the last (low magnitude) bit */ -#define Default_width_in_pixels 2560 /* default width of a scan line */ -#define Default_number_of_lines 3300 /* default length of an image */ - -#define Invalid -1 -#define None 0 -#define Extra_positions 25 /* ensures extra room in allocations */ -#define Not_done_yet 0 - -#define VL3 -3 /* Vertical Left 3 mode */ -#define VL2 -2 /* Vertical Left 2 mode */ -#define VL1 -1 /* Vertical Left 1 mode */ -#define V0 0 /* Vertical mode */ -#define VR1 1 /* Vertical Right 1 mode */ -#define VR2 2 /* Vertical Right 2 mode */ -#define VR3 3 /* Vertical Right 3 mode */ -#define P 4 /* Pass mode */ -#define H 5 /* Horizontal mode */ -#define EOFB 6 /* End Of File Buffer */ - -#define No_offset 0 /* no offset during fseek() */ -#define End_of_file 2 /* start at EOF during fseek() */ -#define Start_of_file 0 /* start at SOF during fseek() */ - -/* -unsigned char *calloc(); -SHORT *malloc(); -*/ - -struct parameters { - SHORT previous_color; /* color of last run of pixels */ - SHORT index; /* indicates current position in "coding_line" */ - SHORT max_pixel; /* the number of pixels in a scan line */ - SHORT pixel; /* pixel number of the last changing element */ - SHORT *reference_line; /* array of changing elements on reference line */ - SHORT *coding_line; /* array of changing elements on coding line */ -}; - -struct compressed_descriptor { - unsigned char *data; /* pointer to compressed image */ - SHORT pixels_per_line; /* the number of pixels in a scan line */ - SHORT number_of_lines; /* the number of scan lines in the image */ - int length_in_bytes; /* length of the compressed image in bytes */ -}; - -struct uncompressed_descriptor { - unsigned char *data; /* pointer to uncompressed image */ - SHORT pixels_per_line; /* the number of pixels in a scan line */ - SHORT number_of_lines; /* the number of scan lines in the image */ -}; - - -/***************************************************************************** - - declarations of all the procedures in the group4 compression routines - follow. The names of the files that contain the procedures are enclosed - in comments above the declarations. - -******************************************************************************/ - -/* compress.c */ -void control_compression(); -struct uncompressed_descriptor process_arguments(); -void set_up_first_and_last_changing_elements_c(); -void set_up_first_and_last_changing_elements_d(); -void read_uncompressed_file_into_memory(); -void write_compressed_data_into_a_file(); -void prepare_to_compress(); -void compress_image(); -void make_array_of_changing_elements(); -void prepare_to_compress_next_line(); -void set_up_first_line_c(); -void set_up_first_line_d(); -void crash_c(); - -/* table.c */ -void process_char(); - - -/* mode.c */ -void compress_line(); -void initialize_b1(); -void pass_mode_c(); -void pass_mode_d(); -void vertical_mode_c(); -void vertical_mode_d(); -void horizontal_mode_c(); -void horizontal_mode_d(); - - -/* write_run.c */ -void write_run_length(); - - -/* write_bits.c */ -void prepare_to_write_bits_c(); -void prepare_to_write_bits_d(); -void write_bits_c(); -void write_bits_d(); -unsigned int flush_buffer(); diff --git a/huagao/grp4deco.c b/huagao/grp4deco.c deleted file mode 100644 index d15001e1..00000000 --- a/huagao/grp4deco.c +++ /dev/null @@ -1,1356 +0,0 @@ -/* -# proc: grp4decomp - CCITT Group 4 decompresses an image. -# proc: -*/ - -/******************************************************************** -* File Name: grp4decomp.c * -* Modified: Darlene E. Frederick * -* Michael D. Garris * -* Date: January 25, 1990 * -* Package: CCITT4 compression routines * -* * -* Contents: ccitt4_decompress() * -* read_compressed_file_into_memory() * -* control_decompression() * -* prepare_to_decompress() * -* set_up_first_and_last_changing_elements_d() * -* prepare_to_decompress_next_line() * -* set_up_first_line_d() * -* ReportError() * -********************************************************************/ - -#include -#include -#include -//#include -#ifdef TIME -#include -#endif -#include "g4codec.h" -#include "grp4deco.h" - -// Constants -#define NOALLOC 0 -#define ALLOC 1 - -// Structs -static struct changing_element { - SHORT color; /* the color of the pixel */ - SHORT pixel; /* the position of the pixel on the line */ -} a0, a1; - -static SHORT b1, b2; /* an index into reference_line */ - -// Local variables -static int decomp_alloc_flag; -static int decomp_write_init_flag; -static int decomp_read_init_flag; - -static char *all_white; -static char *output_area; -static char *all_black; - - -#ifdef TIME -struct timeval t1, t2; -struct timezone tz; -#endif - -//#if Debug // to keep track of where we are -static int current_Byte; -static SHORT current_Line = 0; // counting starts from 1 -//#endif - -// Local functions -static void ReportError(char *message); - -/******************************************************************** -* grp4decomp is the main routine of this file. It does pre- * -* liminary setup, calls routines, and does final processing. * -********************************************************************/ -/******************************************************************** -* Arguments * -* --------- * -* Passed in: * -* indata - buffer containing the compressed data. * -* inbytes - the number of bytes in indata. * -* width - Width in pixels of uncompressed data. * -* height - Number of lines of uncompressed data. * -* Returned: * -* outdata - buffer containing the decompressed data* -* outbytes - the number of bytes in outdata. * -********************************************************************/ -int grp4decomp(unsigned char* indata, - int inbytes, - int width, - int height, - unsigned char *outdata, - int *outbytes) -{ -struct compressed_descriptor compressed; -struct decompressed_descriptor decompressed; - - compressed.pixels_per_line = width; - compressed.number_of_lines = height; - compressed.length_in_bytes = inbytes; - compressed.data = (char *)indata; - decomp_alloc_flag = NOALLOC; - decomp_write_init_flag = 1; - decomp_read_init_flag = 1; - read_compressed_file_into_memory(&compressed); - decompressed.data = (char *)outdata; - control_decompression( &compressed, &decompressed ); - *outbytes = - (decompressed.pixels_per_line >> 3) * decompressed.number_of_lines; - return (*outbytes); -} - - -/***************************** control_decompression ************************** - - calls the functions that decompress the compressed file - -*****************************************************************************/ -/******************************************************************** -* Arguments * -* --------- * -* Passed in: * -* compressed - structure containing the # of * -* pixels per line, the number of * -* lines, and the compressed data. * -* Returned: * -* decompressed - structure containing the # of * -* pixels per line, the number of * -* lines, and the compressed data. * -********************************************************************/ -void control_decompression(struct compressed_descriptor *compressed, - struct decompressed_descriptor *decompressed) -{ - struct parameters sole_parameters; - struct parameters *params = &sole_parameters; - -#ifdef TIME - SHORT i; - tz.tz_minuteswest = 0; - tz.tz_dsttime = 0; - gettimeofday(&t1, &tz); -#endif - - prepare_to_decompress( compressed, decompressed, params ); - while(decompress_line( params ) != EOFB ) - prepare_to_decompress_next_line( params ); - /* memory deallocation added by Michael D. Garris 2/23/90 */ - free(params->reference_line); - free(params->coding_line); - free(all_white); - free(all_black); - if (current_Line != (decompressed->number_of_lines+1)) - { - ReportError("Not enough lines in input!"); - } -#ifdef TIME - gettimeofday(&t2, &tz); - printf("\ntime difference: %ld:%ld\n", t2.tv_sec - t1.tv_sec, - t2.tv_usec - t1.tv_usec); - for(i=0; i<5; i++) printf("%c",'\07'); -#endif - -} - - -/************************ read_compressed_file_into_memory ********************* - - allocates memory for the compressed image - -*****************************************************************************/ -/******************************************************************** -* Arguments * -* --------- * -* Passed in: * -* compressed - structure containing the # of * -* pixels per line, the number of * -* lines, and the compressed data. * -* Returned: * -* compressed - structure containing the # of * -* pixels per line, the number of * -* lines, and the compressed data. * -********************************************************************/ -void read_compressed_file_into_memory(struct compressed_descriptor *compressed) -{ - if(decomp_alloc_flag) - { - if((compressed->data = (char *)calloc(compressed->length_in_bytes, - sizeof(char))) == NULL) - { - printf("\nCannot allocate enough memory for compressed file.\n"); - exit(1); - } - } - else if(compressed->data == NULL) - { - printf("\nNo memory allocated for input data!\n"); - exit(1); - } -} /* end read_compressed_file_into_memory() */ - - - -/*************************** prepare_to_decompress **************************** - - initializes variables in preperation for decompression - -*****************************************************************************/ -/******************************************************************** -* Arguments * -* --------- * -* Passed in: * -* compressed - structure containing the # of * -* pixels per line, the number of * -* lines, and the compressed data. * -* Returned: * -* decompressed - structure containing the # of * -* pixels per line, the number of * -* lines, and the compressed data. * -* params - structure storing information needed * -* for comparison and other tasks. * -********************************************************************/ -void prepare_to_decompress(struct compressed_descriptor *compressed, - struct decompressed_descriptor *decompressed, - struct parameters *params) -{ - - params->max_pixel = compressed->pixels_per_line; - decompressed->pixels_per_line = compressed->pixels_per_line; - decompressed->number_of_lines = compressed->number_of_lines; - - set_up_first_line_d( params ); - prepare_to_read_bits( compressed->data ); - if(decomp_alloc_flag) - { - decompressed->data = (char *)calloc(compressed->pixels_per_line * - compressed->number_of_lines / Pixels_per_byte, sizeof(char) ); - } - else if(decompressed->data == NULL) - { - printf("\nNo memory allocated for decompressed data!\n"); - exit(1); - } - prepare_to_write_bits_d( decompressed->data, - (compressed->pixels_per_line / Pixels_per_byte) ); - -} /* end decompress() */ - - - -/******************************* set_up_first_line_d *************************** - - initializes variables in preperation for decompressing the first line - -******************************************************************************/ -/******************************************************************** -* Arguments * -* --------- * -* Passed in: * -* params - structure storing information needed * -* for comparison and other tasks. * -* Returned: * -* params - structure storing information needed * -* for comparison and other tasks. * -********************************************************************/ -void set_up_first_line_d(struct parameters *params) -{ - - params->reference_line = - (SHORT *)malloc( (params->max_pixel + Extra_positions) * sizeof(SHORT) ); - params->coding_line = - (SHORT *)malloc( (params->max_pixel + Extra_positions) * sizeof(SHORT) ); - - *(params->reference_line + 0) = Invalid; - *(params->reference_line + 1) = params->max_pixel; - *(params->reference_line + 2) = params->max_pixel; - *(params->reference_line + 3) = params->max_pixel; - - /* initialize first changing element on coding line (a0 = -1) */ - *(params->coding_line) = Invalid; - - params->index = 0; - -} /* end set_up_first_line_d() */ - - - -/******************* set_up_first_and_last_changing_elements_d ***************** - - initializes the first and last changing elements in the coding line - -******************************************************************************/ -/******************************************************************** -* Arguments * -* --------- * -* Passed in: * -* params - structure storing information needed * -* for comparison and other tasks. * -* Returned: * -* params - structure storing information needed * -* for comparison and other tasks. * -********************************************************************/ -void set_up_first_and_last_changing_elements_d(struct parameters *params) -{ - *(params->coding_line) = Invalid; - - /* - * set up the imaginary first changing pixel with an illegal value. - */ - - *(params->coding_line + ++params->index) = params->max_pixel; - *(params->coding_line + ++params->index) = params->max_pixel; - *(params->coding_line + ++params->index) = params->max_pixel; - - /* - * set up three changing pixels at the end of the line, all of which - * contain the end-of-line flag "max_pixel." It is necessary to create - * three of these flags because the changing elements a0 - b2 are sometimes - * advanced by more than one element at a time (and therefore could skip - * over a single flag). - */ -} - - - -/*********************** prepare_to_decompress_next_line ********************* - - initializes variables in preperation for decompressing another line - -******************************************************************************/ -/******************************************************************** -* Arguments * -* --------- * -* Passed in: * -* params - structure storing information needed * -* for comparison and other tasks. * -* Returned: * -* params - structure storing information needed * -* for comparison and other tasks. * -********************************************************************/ -void prepare_to_decompress_next_line(struct parameters *params) -{ - - set_up_first_and_last_changing_elements_d( params ); - swap_the_reference_and_coding_lines( params ); - params->index = 0; - -} /* end prepare_to_decompress_next_line() */ - - - -/******************** swap_the_reference_and_coding_lines ******************** - - swaps the reference and coding lines - -*****************************************************************************/ -/******************************************************************** -* Arguments * -* --------- * -* Passed in: * -* params - structure storing information needed * -* for comparison and other tasks. * -* Returned: * -* params - structure storing information needed * -* for comparison and other tasks. * -********************************************************************/ -void swap_the_reference_and_coding_lines(struct parameters *params) -{ -SHORT *temp; - - temp = params->reference_line; - params->reference_line = params->coding_line; - params->coding_line = temp; - -} /* end swap_the_reference_and_coding_lines() */ - - - -/*********************************** ReportError *********************************** - - forces the program to crash and create a core file - -******************************************************************************/ -void ReportError(char *message) -{ - FILE *crash_program = NULL; - - printf("Detected error: %s\n", message); - printf("Input position: Byte-%d\n", current_Byte); - printf("Output position: Line-%d, Pixel-%d\n", current_Line, a0.pixel); - //printf("Press Enter to continue\n"); - //scanf("\n"); - system("PAUSE"); - exit(-999); - // fprintf(crash_program,"This will kill the program and create a core file"); -} - - -/***************************************************************************/ -/* Originally decomp.c */ -/***************************************************************************/ - - -/******************************* decompress_line ***************************** - - decompress one line of the compressed image - -******************************************************************************/ -SHORT decompress_line(struct parameters *params) -{ - SHORT mode; - - current_Line++; -#if Debug - printf("\n\nLINE: %d\n", current_Line); -#endif - - b1 = 1; /* this puts b1 on the first black element in the reference line , - * which is appropriate because a0 is white and on -1 */ - a0.pixel = 0; - a0.color = White; - do { - -#if Debug - printf("a0:%d, a1:%d, b1:%d", a0.pixel, a1.pixel, - *(params->reference_line + b1)); -#endif - mode = get_mode(); - switch( mode ) - { - - case V0: - - case VR1: - - case VL1: - - case VR2: - - case VL2: - - case VR3: - - case VL3: vertical_mode_d( params, mode ); - break; - - case H: horizontal_mode_d( params ); - break; - - case P: pass_mode_d( params ); - continue; - - case EOFB: return( EOFB ); - - default: ReportError("Unrecognized code!"); - - } /* end case of different modes */ - - } while(a0.pixel < params->max_pixel); - if (a0.pixel != params->max_pixel) - { - ReportError("Line width larger than expected!"); - } - return( Not_done_yet ); - -} /* end decompress_line */ - - - -/********************************** get_mode ********************************** - - read a mode code from the compressed image - -******************************************************************************/ -SHORT get_mode() -{ - SHORT i; - - if (read_bit() == 1) - return(V0); /* 1 */ - - if (read_bit() == 1) { - if (read_bit() == 1) - return(VR1); /* 011 */ - else - return(VL1); /* 010 */ - } - else { - if (read_bit() == 1) - return(H); /* 001 */ - if (read_bit() == 1) - return(P); /* 0001 */ - if (read_bit() == 1) { - if (read_bit() == 1) - return(VR2); /* 000011 */ - else - return(VL2); /* 000010 */ - } - else { - if (read_bit() == 1) { - if (read_bit() == 1) - return(VR3); /* 0000011 */ - else - return(VL3); /* 0000010 */ - } - else { - - /* - * Have read 6 zero's so far. The only valid code now - * possible is EOFB: 00000000000100000000001. - */ - - for(i=0;i<5;i++) - if (read_bit() != 0) - ReportError("Expecting page terminating code!"); - - if (read_bit() != 1) - ReportError("Expecting page terminating code!"); - - for(i=0;i<11;i++) - if (read_bit() != 0) - ReportError("Expecting page terminating code!"); - - if (read_bit() != 1) - ReportError("Expecting page terminating code!"); - - return(EOFB); /* 00000000000100000000001 */ - } - } - } - -} /* end get_mode() */ - - -/********************************** pass_mode_d ******************************** - - decompress a pass mode code - -******************************************************************************/ -void pass_mode_d(struct parameters *params) -{ - SHORT run_length; - - b2 = b1 + 1; - run_length = *(params->reference_line + b2) - a0.pixel; - write_bits_d(run_length, a0.color); - -#if Debug - printf(" P Run:%d, Color:%d\n", run_length, a0.color); -#endif - - a0.pixel += run_length; - /* a0.color does not change during pass_mode_d() */ - b1 += 2; -} /* end pass_mode_d() */ - - -/******************************* vertical_mode_d ******************************* - - decompress a vertical mode code - -******************************************************************************/ -void vertical_mode_d(struct parameters *params, SHORT offset) -{ - SHORT run_length; - - a1.pixel = *(params->reference_line + b1) + offset; - run_length = a1.pixel - a0.pixel; - if (run_length < 0) - { - ReportError("a1 in front of a0!"); - } - write_bits_d(run_length, a0.color); - -#if Debug - printf(" V%d Run:%d, Color:%d\n", offset, run_length, a0.color); -#endif - - a0.pixel = a1.pixel; - a0.color = ! a0.color; - *(params->coding_line + ++params->index) = a0.pixel; - - /* - * The color of a0 changes after each vertical mode coding. - */ - - if ((offset == -1) || (offset == 0)) { - if ( *(params->reference_line + b1) != params->max_pixel ) - b1++; - return; - } - - if ((offset == 1) || (offset == 2)) { - b1++; - if ( (*(params->reference_line + b1) <= a0.pixel) && - (*(params->reference_line + b1) != params->max_pixel) ) - b1 += 2; - return; - } - - if ((offset == -2) || (offset == -3)) { - if ( *(params->reference_line + b1 - 1) > a0.pixel ) - b1--; - else { if ( *(params->reference_line + b1) != params->max_pixel ) - b1++; - return; - } - } - - if (offset == 3) { - b1++; - while ( (*(params->reference_line + b1) <= a0.pixel) && - (*(params->reference_line + b1) != params->max_pixel) ) - b1 += 2; - return; - } - -} /* end vertical_mode_d() */ - - - - -/******************************* horizontal_mode_d ***************************** - - decompress a horizontal mode code - -******************************************************************************/ -void horizontal_mode_d(struct parameters *params) -{ - SHORT length, total_length = 0; - - do { - length = find_run_length_code(a0.color); - total_length += length; - } while (length > Max_terminating_length); - - /* - * Run lengths greater than 63 are followed by terminating codes. - * Thus if "length" is greater than 63, the terminating code must - * also be fetched in order to determine the total run length. - * - */ - - write_bits_d(total_length, a0.color); - -#if Debug - printf(" H Run:%d, Color:%d\n", total_length, a0.color); -#endif - - a0.pixel += total_length; - a0.color = !a0.color; - *(params->coding_line + ++params->index) = a0.pixel; - - /* - * a0's color changes after each run color. - */ - - total_length = 0; - do { - length = find_run_length_code(a0.color); - total_length += length; - } while (length > Max_terminating_length); - - write_bits_d(total_length, a0.color); - -#if Debug - printf(" Run:%d, Color:%d\n", total_length, a0.color); -#endif - - a0.pixel += total_length; - a0.color = !a0.color; - *(params->coding_line + ++params->index) = a0.pixel; - - while ( (*(params->reference_line + b1) <= a0.pixel) && - ( *(params->reference_line + b1) < params->max_pixel) ) - { - b1 += 2; /* must move ahead by 2 to maintain color difference with */ - /* a0, whose color does not change in this mode. */ - } - -} /* end horizontal_mode_d() */ - - -/***************************************************************************/ -/* Originally write.c */ -/***************************************************************************/ - -static char write_one[Pixels_per_byte] = -{ - (char)0x80, /* 10000000b: with | operator, it writes a one to bit 0 */ - (char)0x40, /* 01000000b: with | operator, it writes a one to bit 1 */ - (char)0x20, /* 00100000b: with | operator, it writes a one to bit 2 */ - (char)0x10, /* 00010000b: with | operator, it writes a one to bit 3 */ - (char)0x8, /* 00001000b: with | operator, it writes a one to bit 4 */ - (char)0x4, /* 00000100b: with | operator, it writes a one to bit 5 */ - (char)0x2, /* 00000010b: with | operator, it writes a one to bit 6 */ - (char)0x1, /* 00000001b: with | operator, it writes a one to bit 7 */ -}; - -static char write_zero[Pixels_per_byte] = -{ - (char)0x7F, /* 01111111b: with & operator, it writes a zero to bit 0 */ - (char)0xBF, /* 10111111b: with & operator, it writes a zero to bit 1 */ - (char)0xDF, /* 11011111b: with & operator, it writes a zero to bit 2 */ - (char)0xEF, /* 11101111b: with & operator, it writes a zero to bit 3 */ - (char)0xF7, /* 11110111b: with & operator, it writes a zero to bit 4 */ - (char)0xFB, /* 11111011b: with & operator, it writes a zero to bit 5 */ - (char)0xFD, /* 11111101b: with & operator, it writes a zero to bit 6 */ - (char)0xFE, /* 11111110b: with & operator, it writes a zero to bit 7 */ -}; - - - -/************************* prepare_to_write_bits_d ****************************** - - initializes variables in preperation for writing decompressed data - -******************************************************************************/ -void prepare_to_write_bits_d(char *output_pointer, - SHORT bytes_per_line) -{ - output_area = output_pointer; - - all_white = (char *)calloc( bytes_per_line, sizeof(char) ); - all_black = (char *)calloc( bytes_per_line, sizeof(char) ); - memset(all_black, Black_byte, bytes_per_line); - /* set all the pixels in "all_black" to '1' */ -} - - - -/******************************* write_bits_d *********************************** - - writes a number of bits of the same color to the memory buffer that holds - the decompressed image. - -******************************************************************************/ -void write_bits_d(unsigned SHORT length, - unsigned SHORT color) -{ - static unsigned SHORT write_on_this_bit = 0; - static unsigned int write_on_this_byte = 0; - unsigned int bytes; - - /* global switch added by Michael D. Garris 2/23/90 */ - if(decomp_write_init_flag){ - write_on_this_bit = 0; - write_on_this_byte = 0; - decomp_write_init_flag = 0; - } - if (color == Black) { - while( (length>0) && (write_on_this_bit != 0) ) { - *(output_area + write_on_this_byte) |= write_one[write_on_this_bit]; - length--; - if (write_on_this_bit == Last_bit_in_a_byte) { - write_on_this_bit = 0; - write_on_this_byte++; - } - else - write_on_this_bit++; - } - - memcpy((output_area+write_on_this_byte), all_black, (bytes=(length/8))); - write_on_this_byte += bytes; - length %= Bits_per_byte; - - while( length>0 ) { - *(output_area + write_on_this_byte) |= write_one[write_on_this_bit]; - length--; - if (write_on_this_bit == Last_bit_in_a_byte) { - write_on_this_bit = 0; - write_on_this_byte++; - } - else - write_on_this_bit++; - } - } /* end if color is black */ - else { - while( (length>0) && (write_on_this_bit != 0) ) { - *(output_area + write_on_this_byte) &=write_zero[write_on_this_bit]; - length--; - if (write_on_this_bit == Last_bit_in_a_byte) { - write_on_this_bit = 0; - write_on_this_byte++; - } - else - write_on_this_bit++; - } - - memcpy((output_area+write_on_this_byte), all_white, (bytes=(length/8))); - write_on_this_byte += bytes; - length %= Bits_per_byte; - - while( length>0 ) { - *(output_area + write_on_this_byte) &=write_zero[write_on_this_bit]; - length--; - if (write_on_this_bit == Last_bit_in_a_byte) { - write_on_this_bit = 0; - write_on_this_byte++; - } - else - write_on_this_bit++; - } - } /* end if color is white */ -} /* end write_bits_d() */ - - -/***************************************************************************/ -/* Originally read.c */ -/***************************************************************************/ - -static char *input_area; - -static char read_bit_mask[Pixels_per_byte] = -{ - (char)0x80, /* 10000000b: with & operator, it reads bit 0 */ - (char)0x40, /* 01000000b: with & operator, it reads bit 1 */ - (char)0x20, /* 00100000b: with & operator, it reads bit 2 */ - (char)0x10, /* 00010000b: with & operator, it reads bit 3 */ - (char)0x8, /* 00001000b: with & operator, it reads bit 4 */ - (char)0x4, /* 00000100b: with & operator, it reads bit 5 */ - (char)0x2, /* 00000010b: with & operator, it reads bit 6 */ - (char)0x1, /* 00000001b: with & operator, it reads bit 7 */ -}; - -/************************* prepare_to_read_bits ****************************** - - initialize a local, static variable, input_area - -******************************************************************************/ -void prepare_to_read_bits(char *input_pointer) -{ - input_area = input_pointer; -} - - -/******************************* read_bit ************************************* - - returns the value of the current bit of the compressed image (e.g. 1 or 0) - -******************************************************************************/ -SHORT read_bit() -{ - SHORT bit; - static unsigned SHORT read_this_bit = 0; - static unsigned int read_this_byte = 0; - - /* global switch added by Michael D. Garris 2/23/90 */ - if(decomp_read_init_flag){ - read_this_bit = 0; - read_this_byte = 0; - decomp_read_init_flag = 0; - } - /* - * read_bits gets one bit from the compressed data file, - * and returns it as an integer value of 0 or 1. - */ - - bit = *(input_area + read_this_byte) & read_bit_mask[read_this_bit]; - if(read_this_bit == Last_bit_in_a_byte) { - read_this_bit = 0; - read_this_byte++; - } /* end if current byte completely read */ - else - read_this_bit++; - -//#if Debug - current_Byte = read_this_byte; // to keep track of where we are -//#endif - - /* - * NOTE: bit contains 0 if the bit read was 0, - * or a non-zero number if the bit was 1. - */ - - return( !(bit == 0) ); - - /* - * the above operation returns a zero if bit is equal to zero, or a - * one if bit is not equal to zero. - */ - -} /* end read_bit() */ - - -/***************************************************************************/ -/* Originally tree.c */ -/***************************************************************************/ - -struct node { - SHORT value; - struct node *child_zero; - struct node *child_one; -}; /* end node struct */ - -static struct node *node_ptr; - - -/***************************************************************************** - -The following declarations create two ordered, unbalanced, binary trees. -The trees contain run length values, and are ordered by the Huffman codes -that correspond to those values. - -******************************************************************************/ - -struct node black_tree[] = - { - -1, &black_tree[ 2], &black_tree[ 1], - -1, &black_tree[ 4], &black_tree[ 3], - -1, &black_tree[ 6], &black_tree[ 5], - 2, NULL, NULL, - 3, NULL, NULL, - -1, &black_tree[ 8], &black_tree[ 7], - -1, &black_tree[ 10], &black_tree[ 9], - 4, NULL, NULL, - 1, NULL, NULL, - -1, &black_tree[ 12], &black_tree[ 11], - -1, &black_tree[ 14], &black_tree[ 13], - 5, NULL, NULL, - 6, NULL, NULL, - -1, &black_tree[ 16], &black_tree[ 15], - -1, &black_tree[ 18], &black_tree[ 17], - 7, NULL, NULL, - -1, &black_tree[ 20], &black_tree[ 19], - -1, &black_tree[ 22], &black_tree[ 21], - -1, &black_tree[ 24], &black_tree[ 23], - 8, NULL, NULL, - 9, NULL, NULL, - -1, &black_tree[ 26], &black_tree[ 25], - -1, &black_tree[ 28], &black_tree[ 27], - -1, &black_tree[ 30], &black_tree[ 29], - -1, &black_tree[ 32], &black_tree[ 31], - 12, NULL, NULL, - -1, &black_tree[ 34], &black_tree[ 33], - 11, NULL, NULL, - 10, NULL, NULL, - -1, &black_tree[ 36], &black_tree[ 35], - -1, &black_tree[ 38], &black_tree[ 37], - -1, &black_tree[ 40], &black_tree[ 39], - -1, NULL, &black_tree[ 41], - -1, &black_tree[ 43], &black_tree[ 42], - -1, &black_tree[ 45], &black_tree[ 44], - 14, NULL, NULL, - -1, &black_tree[ 47], &black_tree[ 46], - -1, &black_tree[ 49], &black_tree[ 48], - 13, NULL, NULL, - -1, &black_tree[ 51], &black_tree[ 50], - -1, &black_tree[ 53], &black_tree[ 52], - -1, &black_tree[ 55], &black_tree[ 54], - -1, &black_tree[ 57], &black_tree[ 56], - -1, &black_tree[ 59], &black_tree[ 58], - -1, &black_tree[ 61], &black_tree[ 60], - 15, NULL, NULL, - -1, &black_tree[ 63], &black_tree[ 62], - -1, &black_tree[ 65], &black_tree[ 64], - -1, &black_tree[ 67], &black_tree[ 66], - -1, &black_tree[ 69], &black_tree[ 68], - -1, &black_tree[ 71], &black_tree[ 70], - -1, &black_tree[ 73], &black_tree[ 72], - -1, &black_tree[ 75], &black_tree[ 74], - -1, &black_tree[ 77], &black_tree[ 76], - -1, &black_tree[ 79], &black_tree[ 78], - -1, &black_tree[ 81], &black_tree[ 80], - 0, NULL, NULL, - -1, &black_tree[ 83], &black_tree[ 82], - -1, &black_tree[ 85], &black_tree[ 84], - -1, &black_tree[ 87], &black_tree[ 86], - -1, &black_tree[ 89], &black_tree[ 88], - -1, &black_tree[ 91], &black_tree[ 90], - -1, &black_tree[ 93], &black_tree[ 92], - -1, &black_tree[ 95], &black_tree[ 94], - -1, &black_tree[ 97], &black_tree[ 96], - 17, NULL, NULL, - 16, NULL, NULL, - -1, &black_tree[ 99], &black_tree[ 98], - -1, &black_tree[101], &black_tree[100], - -1, &black_tree[103], &black_tree[102], - 64, NULL, NULL, - -1, &black_tree[105], &black_tree[104], - -1, &black_tree[107], &black_tree[106], - -1, &black_tree[109], &black_tree[108], - -1, &black_tree[111], &black_tree[110], - -1, &black_tree[113], &black_tree[112], - -1, &black_tree[115], &black_tree[114], - 18, NULL, NULL, - -1, &black_tree[117], &black_tree[116], - -1, &black_tree[119], &black_tree[118], - -1, &black_tree[121], &black_tree[120], - -1, &black_tree[123], &black_tree[122], - -1, &black_tree[125], &black_tree[124], - 21, NULL, NULL, - -1, &black_tree[127], &black_tree[126], - -1, &black_tree[129], &black_tree[128], - -1, &black_tree[131], &black_tree[130], - 20, NULL, NULL, - 19, NULL, NULL, - -1, &black_tree[133], &black_tree[132], - -1, &black_tree[135], &black_tree[134], - -1, &black_tree[137], &black_tree[136], - 22, NULL, NULL, - -1, &black_tree[139], &black_tree[138], - -1, &black_tree[141], &black_tree[140], - -1, &black_tree[143], &black_tree[142], - -1, &black_tree[145], &black_tree[144], - -1, &black_tree[147], &black_tree[146], - -1, &black_tree[149], &black_tree[148], - -1, &black_tree[151], &black_tree[150], - -1, &black_tree[153], &black_tree[152], - -1, &black_tree[155], &black_tree[154], - -1, &black_tree[157], &black_tree[156], - 23, NULL, NULL, - -1, &black_tree[159], &black_tree[158], - -1, &black_tree[161], &black_tree[160], - -1, &black_tree[163], &black_tree[162], - -1, &black_tree[165], &black_tree[164], - -1, &black_tree[167], &black_tree[166], - 25, NULL, NULL, - 24, NULL, NULL, - -1, &black_tree[169], &black_tree[168], - -1, &black_tree[171], &black_tree[170], - -1, &black_tree[173], &black_tree[172], - -1, &black_tree[175], &black_tree[174], - -1, &black_tree[177], &black_tree[176], - -1, &black_tree[179], &black_tree[178], - -1, &black_tree[181], &black_tree[180], - 1920, NULL, NULL, - 1856, NULL, NULL, - -1, &black_tree[183], &black_tree[182], - -1, &black_tree[185], &black_tree[184], - -1, &black_tree[187], &black_tree[186], - 1792, NULL, NULL, - 43, NULL, NULL, - 42, NULL, NULL, - 39, NULL, NULL, - 38, NULL, NULL, - 37, NULL, NULL, - 36, NULL, NULL, - 35, NULL, NULL, - 34, NULL, NULL, - 29, NULL, NULL, - 28, NULL, NULL, - 27, NULL, NULL, - 26, NULL, NULL, - 192, NULL, NULL, - 128, NULL, NULL, - 41, NULL, NULL, - 40, NULL, NULL, - 33, NULL, NULL, - 32, NULL, NULL, - 31, NULL, NULL, - 30, NULL, NULL, - 63, NULL, NULL, - 62, NULL, NULL, - 49, NULL, NULL, - 48, NULL, NULL, - 256, NULL, NULL, - 61, NULL, NULL, - 58, NULL, NULL, - 57, NULL, NULL, - 47, NULL, NULL, - 46, NULL, NULL, - 45, NULL, NULL, - 44, NULL, NULL, - 51, NULL, NULL, - 50, NULL, NULL, - -1, &black_tree[189], &black_tree[188], - -1, &black_tree[191], &black_tree[190], - -1, &black_tree[193], &black_tree[192], - 54, NULL, NULL, - 53, NULL, NULL, - -1, &black_tree[195], &black_tree[194], - 448, NULL, NULL, - 384, NULL, NULL, - 320, NULL, NULL, - -1, &black_tree[197], &black_tree[196], - -1, &black_tree[199], &black_tree[198], - 60, NULL, NULL, - 59, NULL, NULL, - -1, &black_tree[201], &black_tree[200], - -1, &black_tree[203], &black_tree[202], - 56, NULL, NULL, - 55, NULL, NULL, - -1, &black_tree[205], &black_tree[204], - -1, &black_tree[207], &black_tree[206], - 52, NULL, NULL, - 2560, NULL, NULL, - 2496, NULL, NULL, - 2432, NULL, NULL, - 2368, NULL, NULL, - 2304, NULL, NULL, - 2240, NULL, NULL, - 2176, NULL, NULL, - 2112, NULL, NULL, - 2048, NULL, NULL, - 1984, NULL, NULL, - 1216, NULL, NULL, - 1152, NULL, NULL, - 1088, NULL, NULL, - 1024, NULL, NULL, - 960, NULL, NULL, - 896, NULL, NULL, - 576, NULL, NULL, - 512, NULL, NULL, - 1728, NULL, NULL, - 1664, NULL, NULL, - 1600, NULL, NULL, - 1536, NULL, NULL, - 1472, NULL, NULL, - 1408, NULL, NULL, - 1344, NULL, NULL, - 1280, NULL, NULL, - 832, NULL, NULL, - 768, NULL, NULL, - 704, NULL, NULL, - 640, NULL, NULL, -}; /* end black_tree */ - - - -struct node white_tree[] = - { - -1, &white_tree[ 2], &white_tree[ 1], - -1, &white_tree[ 4], &white_tree[ 3], - -1, &white_tree[ 6], &white_tree[ 5], - -1, &white_tree[ 8], &white_tree[ 7], - -1, &white_tree[ 10], &white_tree[ 9], - -1, &white_tree[ 12], &white_tree[ 11], - -1, &white_tree[ 14], &white_tree[ 13], - -1, &white_tree[ 16], &white_tree[ 15], - -1, &white_tree[ 18], &white_tree[ 17], - -1, &white_tree[ 20], &white_tree[ 19], - -1, &white_tree[ 22], &white_tree[ 21], - -1, &white_tree[ 24], &white_tree[ 23], - -1, &white_tree[ 26], &white_tree[ 25], - -1, &white_tree[ 28], &white_tree[ 27], - -1, &white_tree[ 30], &white_tree[ 29], - 7, NULL, NULL, - 6, NULL, NULL, - -1, &white_tree[ 32], &white_tree[ 31], - 5, NULL, NULL, - 4, NULL, NULL, - -1, &white_tree[ 34], &white_tree[ 33], - -1, &white_tree[ 36], &white_tree[ 35], - 3, NULL, NULL, - 2, NULL, NULL, - -1, &white_tree[ 38], &white_tree[ 37], - -1, &white_tree[ 40], &white_tree[ 39], - -1, &white_tree[ 42], &white_tree[ 41], - -1, &white_tree[ 44], &white_tree[ 43], - -1, &white_tree[ 46], &white_tree[ 45], - -1, &white_tree[ 48], &white_tree[ 47], - -1, &white_tree[ 50], &white_tree[ 49], - 64, NULL, NULL, - -1, &white_tree[ 52], &white_tree[ 51], - -1, &white_tree[ 54], &white_tree[ 53], - 9, NULL, NULL, - 8, NULL, NULL, - 128, NULL, NULL, - -1, &white_tree[ 56], &white_tree[ 55], - -1, &white_tree[ 58], &white_tree[ 57], - -1, &white_tree[ 60], &white_tree[ 59], - -1, &white_tree[ 62], &white_tree[ 61], - -1, &white_tree[ 64], &white_tree[ 63], - 11, NULL, NULL, - 10, NULL, NULL, - -1, &white_tree[ 66], &white_tree[ 65], - -1, &white_tree[ 68], &white_tree[ 67], - -1, &white_tree[ 70], &white_tree[ 69], - -1, &white_tree[ 72], &white_tree[ 71], - -1, &white_tree[ 74], &white_tree[ 73], - -1, &white_tree[ 76], &white_tree[ 75], - -1, &white_tree[ 78], &white_tree[ 77], - 15, NULL, NULL, - 14, NULL, NULL, - 17, NULL, NULL, - 16, NULL, NULL, - -1, &white_tree[ 80], &white_tree[ 79], - -1, &white_tree[ 82], &white_tree[ 81], - -1, &white_tree[ 84], &white_tree[ 83], - 1664, NULL, NULL, - 192, NULL, NULL, - -1, &white_tree[ 86], &white_tree[ 85], - -1, &white_tree[ 88], &white_tree[ 87], - -1, &white_tree[ 90], &white_tree[ 89], - -1, &white_tree[ 92], &white_tree[ 91], - -1, &white_tree[ 94], &white_tree[ 93], - -1, &white_tree[ 96], &white_tree[ 95], - -1, &white_tree[ 98], &white_tree[ 97], - -1, &white_tree[100], &white_tree[ 99], - -1, &white_tree[102], &white_tree[101], - -1, &white_tree[104], &white_tree[103], - 12, NULL, NULL, - 1, NULL, NULL, - -1, &white_tree[106], &white_tree[105], - -1, &white_tree[108], &white_tree[107], - -1, &white_tree[110], &white_tree[109], - 13, NULL, NULL, - -1, &white_tree[112], &white_tree[111], - -1, &white_tree[114], &white_tree[113], - -1, &white_tree[116], &white_tree[115], - 256, NULL, NULL, - -1, &white_tree[118], &white_tree[117], - -1, &white_tree[120], &white_tree[119], - -1, &white_tree[122], &white_tree[121], - -1, &white_tree[124], &white_tree[123], - -1, &white_tree[126], &white_tree[125], - -1, &white_tree[128], &white_tree[127], - -1, &white_tree[130], &white_tree[129], - 25, NULL, NULL, - -1, &white_tree[132], &white_tree[131], - -1, &white_tree[134], &white_tree[133], - 24, NULL, NULL, - 18, NULL, NULL, - -1, &white_tree[136], &white_tree[135], - -1, &white_tree[138], &white_tree[137], - 27, NULL, NULL, - -1, &white_tree[140], &white_tree[139], - -1, &white_tree[142], &white_tree[141], - -1, &white_tree[144], &white_tree[143], - 28, NULL, NULL, - 21, NULL, NULL, - -1, &white_tree[146], &white_tree[145], - -1, &white_tree[148], &white_tree[147], - -1, &white_tree[150], &white_tree[149], - 26, NULL, NULL, - -1, &white_tree[152], &white_tree[151], - -1, &white_tree[154], &white_tree[153], - 19, NULL, NULL, - -1, &white_tree[156], &white_tree[155], - -1, &white_tree[158], &white_tree[157], - -1, &white_tree[160], &white_tree[159], - 20, NULL, NULL, - -1, &white_tree[162], &white_tree[161], - 23, NULL, NULL, - 22, NULL, NULL, - -1, &white_tree[164], &white_tree[163], - -1, &white_tree[166], &white_tree[165], - -1, NULL, &white_tree[167], - -1, &white_tree[169], &white_tree[168], - -1, &white_tree[171], &white_tree[170], - -1, &white_tree[173], &white_tree[172], - -1, &white_tree[175], &white_tree[174], - -1, &white_tree[177], &white_tree[176], - 576, NULL, NULL, - 640, NULL, NULL, - -1, &white_tree[179], &white_tree[178], - 512, NULL, NULL, - 448, NULL, NULL, - 58, NULL, NULL, - 57, NULL, NULL, - 56, NULL, NULL, - 55, NULL, NULL, - 52, NULL, NULL, - 51, NULL, NULL, - 50, NULL, NULL, - 49, NULL, NULL, - -1, &white_tree[181], &white_tree[180], - -1, &white_tree[183], &white_tree[182], - 60, NULL, NULL, - 59, NULL, NULL, - 384, NULL, NULL, - 320, NULL, NULL, - 0, NULL, NULL, - 63, NULL, NULL, - 62, NULL, NULL, - 61, NULL, NULL, - 44, NULL, NULL, - 43, NULL, NULL, - 42, NULL, NULL, - 41, NULL, NULL, - 40, NULL, NULL, - 39, NULL, NULL, - 54, NULL, NULL, - 53, NULL, NULL, - 32, NULL, NULL, - 31, NULL, NULL, - 38, NULL, NULL, - 37, NULL, NULL, - 36, NULL, NULL, - 35, NULL, NULL, - 34, NULL, NULL, - 33, NULL, NULL, - 48, NULL, NULL, - 47, NULL, NULL, - 46, NULL, NULL, - 45, NULL, NULL, - 30, NULL, NULL, - 29, NULL, NULL, - -1, &white_tree[185], &white_tree[184], - 1408, NULL, NULL, - 1344, NULL, NULL, - 1280, NULL, NULL, - 1216, NULL, NULL, - 1152, NULL, NULL, - 1088, NULL, NULL, - 1024, NULL, NULL, - 960, NULL, NULL, - 896, NULL, NULL, - 832, NULL, NULL, - 768, NULL, NULL, - 704, NULL, NULL, - 1728, NULL, NULL, - 1600, NULL, NULL, - 1536, NULL, NULL, - 1472, NULL, NULL, - -1, &white_tree[187], &white_tree[186], - -1, &white_tree[189], &white_tree[188], - -1, &white_tree[191], &white_tree[190], - -1, &white_tree[193], &white_tree[192], - -1, &white_tree[195], &white_tree[194], - -1, &white_tree[197], &white_tree[196], - -1, &white_tree[199], &white_tree[198], - -1, &white_tree[201], &white_tree[200], - 1920, NULL, NULL, - 1856, NULL, NULL, - -1, &white_tree[203], &white_tree[202], - -1, &white_tree[205], &white_tree[204], - -1, &white_tree[207], &white_tree[206], - 1792, NULL, NULL, - 2560, NULL, NULL, - 2496, NULL, NULL, - 2432, NULL, NULL, - 2368, NULL, NULL, - 2304, NULL, NULL, - 2240, NULL, NULL, - 2176, NULL, NULL, - 2112, NULL, NULL, - 2048, NULL, NULL, - 1984, NULL, NULL, -}; /* end white_tree */ - - -/************************* find_run_length_code ****************************** - - finds the length of the run in the compressed image - by traversing the above declared tree of run length codes. - -******************************************************************************/ -SHORT find_run_length_code(SHORT unsigned color) -{ - if(color == White) - node_ptr = white_tree; /* point to root node */ - else - node_ptr = black_tree; /* point to root node */ - - while(node_ptr->value == Invalid) { /* -1 */ - if((read_bit()) == 0) - node_ptr = node_ptr->child_zero; - else - node_ptr = node_ptr->child_one; - } /* end while node does not contain a run length value */ - - /* - * When this line is reached, node_ptr points to a node that contains - * the run length code: return that value. - */ - - return(node_ptr->value); - -} /* end find_run_length_code */ diff --git a/huagao/grp4deco.h b/huagao/grp4deco.h deleted file mode 100644 index e9b2207f..00000000 --- a/huagao/grp4deco.h +++ /dev/null @@ -1,118 +0,0 @@ -/*********************************************************************/ -/* grp4decomp.h */ -/* Originally decompression.h */ -/*********************************************************************/ -#include - -#define SHORT int -/* -#define True 1 -#define False 0 -#define Debug True // False - -#define White 0 -#define Black 1 -#define Black_byte 255 // byte of all black bits: 11111111b -*/ -#define Max_terminating_length 63 /* longest terminating code*/ - -#define Pixels_per_byte 8 -#define Bits_per_byte 8 -#define Last_bit_in_a_byte 7 /* assumes bits numbered from 0 - 7 */ -#define Last_bit_mask 1 /* masks the last (low magnitude) bit */ -#define Default_width_in_pixels 1728 /* default width of a scan line */ -#define Default_number_of_lines 2200 /* default length of an image */ - -#define Invalid -1 -#define None 0 -#define Extra_positions 25 /* ensures extra room in allocations */ -#define Not_done_yet 0 - -#define VL3 -3 /* Vertical Left 3 mode */ -#define VL2 -2 /* Vertical Left 2 mode */ -#define VL1 -1 /* Vertical Left 1 mode */ -#define V0 0 /* Vertical mode */ -#define VR1 1 /* Vertical Right 1 mode */ -#define VR2 2 /* Vertical Right 2 mode */ -#define VR3 3 /* Vertical Right 3 mode */ -#define P 4 /* Pass mode */ -#define H 5 /* Horizontal mode */ -#define EOFB 6 /* End Of File Buffer */ - -#define No_offset 0 /* no offset during fseek() */ -#define End_of_file 2 /* start at EOF during fseek() */ -#define Start_of_file 0 /* start at BOF during fseek() */ - -/* -char *calloc(); -SHORT *malloc(); -*/ - -struct parameters { - SHORT index; /* indicates current position in "coding_line" */ - SHORT max_pixel; /* the number of pixels in a scan line */ - SHORT *reference_line;/* array of changing elements on reference line */ - SHORT *coding_line; /* array of changing elements on coding line */ -}; - -struct decompressed_descriptor { - char *data; /* pointer to decompressed image */ - SHORT pixels_per_line; /* the number of pixels in a scan line */ - SHORT number_of_lines; /* the number of scan lines in the image */ -}; - -struct compressed_descriptor { - char *data; /* pointer to compressed image */ - SHORT pixels_per_line; /* the number of pixels in a scan line */ - SHORT number_of_lines; /* the number of scan lines in the image */ - int length_in_bytes; /* length of the compressed image in bytes */ -}; - -/***************************************************************************** - - declarations of all the procedures in the group4 decompression routines - follow. The names of the files that contain the procedures are enclosed - in comments above the declarations. - -******************************************************************************/ - -/* decompress.c */ -void control_decompression(); -struct compressed_descriptor process_arguments(); -void read_compressed_file_into_memory(); -void write_decompressed_data_into_a_file(); -void prepare_to_decompress(); -void set_up_first_line_c(); -void set_up_first_line_d(); -void set_up_first_and_last_changing_elements_c(); -void set_up_first_and_last_changing_elements_d(); -void prepare_to_decompress_next_line(); -void swap_the_reference_and_coding_lines(); -void crash_d(); - - -/* decomp.c */ -SHORT decompress_line(); -SHORT get_mode(); -void pass_mode_c(); -void pass_mode_d(); -void vertical_mode_c(); -void vertical_mode_d(); -void horizontal_mode_c(); -void horizontal_mode_d(); - - -/* read.c */ -void prepare_to_read_bits(); -SHORT read_bit(); - - -/* write.c */ -void prepare_to_write_bits_c(); -void prepare_to_write_bits_d(); -void write_bits_c(); -void write_bits_d(); - - -/* tree.c */ -SHORT find_run_length_code(); diff --git a/huagao/huagaods.cpp b/huagao/huagaods.cpp index a0f577ff..2739e567 100644 --- a/huagao/huagaods.cpp +++ b/huagao/huagaods.cpp @@ -939,10 +939,44 @@ Result HuagaoDs::identityOpenDs(const Identity&) { m_query[CapType::SerialNumber] = msgSupportGetAll; - m_caps[CapType::SerialNumber] = std::bind(oneValGetString, _1, _2, scanner->GetSerialNum()); + //m_caps[CapType::SerialNumber] = std::bind(oneValGetString, _1, _2, scanner->GetSerialNum()); + m_caps[CapType::SerialNumber] = [this](Msg msg, Capability& data)->Result { + switch (msg) { + case Msg::Get: + case Msg::GetCurrent: + case Msg::GetDefault: + { + std::string ser = scanner->GetSerialNum(); + Str255 str; + str.setData(ser.c_str(), ser.size()); + data = Capability::createOneValue(str); + return success(); + } + default: + return capBadOperation(); + } + }; + m_query[(CapType)(CapTypeEx::TwEx_HardwareVersion)] = msgSupportGetAll; - m_caps[(CapType)(CapTypeEx::TwEx_HardwareVersion)] = std::bind(oneValGetString, _1, _2, scanner->GetFWVersion()); + //m_caps[(CapType)(CapTypeEx::TwEx_HardwareVersion)] = std::bind(oneValGetString, _1, _2, scanner->GetFWVersion()); + m_caps[(CapType)(CapTypeEx::TwEx_HardwareVersion)] = [this](Msg msg, Capability& data)->Result { + switch (msg) { + case Msg::Get: + case Msg::GetCurrent: + case Msg::GetDefault: + { + std::string ser = scanner->GetFWVersion(); + Str255 str; + str.setData(ser.c_str(), ser.size()); + data = Capability::createOneValue((CapType)(CapTypeEx::TwEx_HardwareVersion),str); + return success(); + } + default: + return capBadOperation(); + } + }; + m_query[CapType::FeederLoaded] = msgSupportGetAll; m_caps[CapType::FeederLoaded] = [this](Msg msg, Capability& data) -> Result { @@ -2014,10 +2048,6 @@ Result HuagaoDs::pendingXfersEnd(const Identity&, PendingXfers& data) { else { m_pendingXfers = 1; } -#ifdef LOG_NORMAL - FileTools::write_log("out.txt", "m_pendingXfers " + std::to_string(m_pendingXfers)); -#endif // LOG_NORMAL - data.setCount(m_pendingXfers); return success(); } @@ -2068,7 +2098,8 @@ Result HuagaoDs::userInterfaceEnable(const Identity&, UserInterface& ui) { // this is an exception when we want to set state explicitly, notifyXferReady can be called only in enabled state // with hidden UI, the usual workflow DsState::Enabled -> notifyXferReady() -> DsState::XferReady is a single step setState(DsState::Enabled); - if (startScan() == success()) { + auto ret = startScan(); + if (ret == success()) { m_pendingXfers = 1; auto notified = notifyXferReady(); return success(); @@ -2076,7 +2107,7 @@ Result HuagaoDs::userInterfaceEnable(const Identity&, UserInterface& ui) { else { m_pendingXfers = 0; setState(DsState::Open); - return seqError(); + return ret; } } @@ -2222,10 +2253,11 @@ Result HuagaoDs::imageMemXferGet(const Identity& origin, ImageMemXfer& data) { return success(); } +//#define LOG_NORMAL #ifdef LOG_NORMAL static int xtfer = 0; #endif - +static int xtfer = 0; Result HuagaoDs::imageNativeXferGet(const Identity& id, ImageNativeXfer& data) { if (!m_pendingXfers) { return seqError(); @@ -2236,9 +2268,10 @@ Result HuagaoDs::imageNativeXferGet(const Identity& id, ImageNativeXfer& data) { data = ImageNativeXfer(bmpSize()); std::copy(bmpBegin(), bmpEnd(), data.data().data()); #ifdef LOG_NORMAL - FileTools::write_log("out.txt", "imageNativeXferGet " + std::to_string(++xtfer)); -#endif // LOG +#endif // LOG + //std::string info = "Twain transfered num of " + to_string(++xtfer)+" images"; + //FileTools::write_log("D:\\1.txt", info); return { ReturnCode::XferDone, ConditionCode::Success }; } @@ -2463,6 +2496,8 @@ void HuagaoDs::updataGscanCap() Twpp::Result HuagaoDs::startScan() { + xtfer = 0; + FileTools::write_log("D:\\1.txt", "RESET TWAIN TRANSFERED IMAGE COUNT"); if (!scanner->IsConnected()) { scanner->open(0x064B, 0x7823); @@ -2523,6 +2558,9 @@ Twpp::Result HuagaoDs::startScan() if (guiTwain.get()) { ((CTwainUI*)(guiTwain.get()))->EnableID_OKorID_Cancel(true); } + if (retCode == 2) + return { ReturnCode::Failure, ConditionCode::NoMedia};//ֽ + return seqError(); } diff --git a/huagao/stdafx.h b/huagao/stdafx.h index 6233ac7d..73b30bc3 100644 Binary files a/huagao/stdafx.h and b/huagao/stdafx.h differ