⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codecolr.c

📁 SPIHT 源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
#endif    for (smk = LSP_mark[p], ptr = 0; ptr != smk;) {      if (!k) {        mtx = LSP_mtx[++m];  k = LSP_dim.y; }      ptr = mtx[--k];#ifdef ENCODER      if (b = (*ptr >= threshold)) *ptr -= threshold;      data_file.code_bit(b);#else      t = (data_file.decode_bit() ? ht2 : ht1);      if (*ptr > 0) *ptr += t; else *ptr -= t;#endif      if (data_file.bytes_used() >= byte_budget) return true; } }  return false;}// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void Process_Image(void){ int c;  threshold = pow(2, threshold_bits);  LSP_idx = LSP_part = LSP_plane = 0;  LSP_ptr = 0;  LSP_dim.x = pyramid_dim.x >> 2;  LSP_dim.y = 12 * pyramid_dim.y;  NEW_VECTOR(LSP_mtx, LSP_dim.x, float **, "LSP");#ifndef BINCODE  int i, j, k, n = -1, t;  for (i = 0; i <= 4; i++) {    group_model[i].set_symbols(2);    for (j = 0; j <= 4 - i; j++)      for (k = 0;  k <= 4 - i - j; k++, n++)        if (n >= 0) {          if (t = i + k) node_model[n].set_symbols(1 << t);          if (t = i + j) desc_model[n].set_symbols(1 << t); } }#endif  Image_Coord cd;  Tree_Node * prev, * node;  NEW_OBJECT(node, Tree_Node, "LISP entry");  LISP_head = LISP_end = node;  root_dim.x = pyramid_dim.x >> pyramid_levels;  root_dim.y = pyramid_dim.y >> pyramid_levels;  root_code = long(pyramid_levels + 1) << 13;  for (int c = 0; c < 3; c++) {    long st = 0x2L + root_code + long(c << 8);    for (cd.x = 0; cd.x < root_dim.x; cd.x += 2)      for (cd.y = 0; cd.y < root_dim.y; cd.y += 2) {        NEW_OBJECT(node, Tree_Node, "LISP entry");        node->coord = cd;  node->state = st;        LISP_end->next = node;  LISP_end = node; } }  LISP_end->next = NULL;  for (;; threshold *= 0.5) {    if (Sorting_Pass()) break;    if (Refinement_Pass()) break; }  code_time.stop();  for (c = LSP_part; c > 0;) delete [] LSP_mtx[--c];  delete [] LSP_mtx;  for (node = LISP_head; node;) {    prev = node;  node = node->next;  delete prev; }}// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// - - Encoder functions - - - - - - - - - - - - - - - - - - - - - - - -#ifdef ENCODERfloat Max_Desc_Val(int l, int s, int x, int y){  float t, m = 0;  Image_Coord ds;  --l;  --s;  for (int d = 0; d < 4; d++) {    ds.x = x + SHF_x[d];  ds.y = y + SHF_y[d];    t = fabs(image[ds]);    if (m < t) m = t;    if (l > 0) {      max_image[ds] = t = Max_Desc_Val(l, s, ds.x << 1, ds.y << 1);      if (m < t) m = t; } }  return m;}// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -int Calc_Max_Image(void){  Image_Coord tc, pc = pyramid_dim, lm = pc;  int c, i, sd, lv = pyramid_levels, sp = lv + 1;  float t, max = 0;  lm.x >>= lv;  lm.y >>= lv;  pc.x >>= 1;  pc.y >>= 1;  max_image.reset(pc);  for (c = 0; c < 3; c++) {    image.set_color(c);  max_image.set_color(c);    for (pc.x = 0; pc.x < lm.x; pc.x += 2)      for (pc.y = 0; pc.y < lm.y; pc.y += 2) {        max_image[pc] = t = fabs(image[pc]);        if (max < t) max = t;        for (sd = sp, i = 1; i < 4; i++) {          tc.x = pc.x + SHF_x[i];  tc.y = pc.y + SHF_y[i];          t = fabs(image[tc]);          if (max < t) max = t;          if (i == 3) --sd;          max_image[tc] = t = Max_Desc_Val(lv, sd,            pc.x + SHF_x[i] * lm.x, pc.y + SHF_y[i] * lm.y);          if (max < t) max = t; } } }  return int(log(max) / log(2.0));}// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -int main(int numb_arg, char * arg[]){  if (numb_arg != 2)    printf("\n\t* Please read the file `codecolr.doc' for "      "instructions\n\t\tand Copyright/Patent information.\n\t* Use"      " `%s -u' for command-line usage information\n\n", arg[0]);  else {    printf("\n* Usage:\n\n%s image_file coded_file number_lines "      "pixels/line bpp_rate", arg[0]);    puts(" \n");      exit(0); }  if (numb_arg == 6) {    pic_f_name = arg[1];  cod_f_name = arg[2];    dimension.x = atoi(arg[3]);  dimension.y = atoi(arg[4]);    bit_rate = atof(arg[5]); }  else {    Input_Line("File with original image", pic_f_name);    Input_Line("File with compressed image", cod_f_name);    dimension.x = Input_Int("Number of image lines");    dimension.y = Input_Int("Number of pixels per line");    bit_rate = Input_Float("Rate (bits/pixel)"); }  rate_mult = 8.0 / (float(dimension.x) * float(dimension.y));  if (Max(dimension.x, dimension.y)>8192) Error("Image is too large!");  byte_budget = long(bit_rate / rate_mult) - 1;  total_time.start();  image.read_pic(dimension, pic_f_name);  image.transform();  pyramid_dim = image.pyramid_dim();  image.averages(mean);  image.color_transform(transf_matrix);  pyramid_levels = Min(Max_Levels(pyramid_dim.x),                       Max_Levels(pyramid_dim.y));  Chronometer max_time;  max_time.start("\n  Starting computation of maximum magnitudes...");  threshold_bits = Calc_Max_Image();  max_time.display(" Maximum magnitudes computed in");  int ii, jj;  data_file.open_file(cod_f_name);  data_file.code_bits(6, dimension.x >> 8);  data_file.code_bits(6, dimension.y >> 8);  data_file.code_bits(8, dimension.x & 0xFF);  data_file.code_bits(8, dimension.y & 0xFF);  data_file.code_bits(5, threshold_bits);  for (ii = 0; ii < 3; ii++) {    data_file.code_bits(8, mean[ii]);    for (jj = 0; jj < 3; jj++) {      data_file.code_bits(8, transf_matrix[ii][jj] >> 8);      data_file.code_bits(8, transf_matrix[ii][jj] & 0xFF); } }  code_time.start("\n  Starting image compression...");  Process_Image();  code_time.display(" Image compressed in");  data_file.close_file();  printf("\n  Compressed file size = %ld bytes (%6.3f bits/pixel).\n",    data_file.bytes_used(), rate_mult * data_file.bytes_used());  total_time.display("\n  Total execution time (I/O included) =");  puts(" ");  return 0;}// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -// - - Decoder functions - - - - - - - - - - - - - - - - - - - - - - - -#elseint main(int numb_arg, char * arg[]){  if (numb_arg != 2)    printf("\n\t* Please read the file `codecolr.doc' for "      "instructions\n\t\tand Copyright/Patent information.\n\t* Use"      " `%s -u' for command-line usage information\n\n", arg[0]);  else {    printf("\n* Usage to compare the recovered image with the original:"      "\n\n%s  coded_file  original_image  bpp_rate\n", arg[0]);    printf("\n* Usage to save the recovered image:\n\n%s -s  "      "coded_file  new_image_file  bpp_rate", arg[0]);    puts(" \n");  exit(0); }  FILE * plot_file;  float mse, psnr, max_rate = 0.0, rate_inc;  boolean cont, comp, interactive, cml;  if (cml = ((numb_arg == 4) || (numb_arg == 5))) {    interactive = false;    if (numb_arg == 4) {      cod_f_name = arg[1];  pic_f_name = arg[2];      bit_rate = atof(arg[3]);  comp = true; }    else {      if ((arg[1][0] != '-') || (arg[1][1] != 's'))        Error("invalid command-line option");      cod_f_name = arg[2];  new_f_name = arg[3];      bit_rate = atof(arg[4]);  comp = false; } }  else {    interactive = Input_Answer("Interactive mode");    Input_Line("Name of the compressed image file", cod_f_name);    comp = !interactive ||      Input_Answer("Recovered image will be compared to original");    if (comp)      Input_Line("File with original image", pic_f_name);    if (!interactive) {      plot_file = Open_File("File with rate x PSNR results", "wt");      bit_rate = Input_Float("First rate (bits/pixel)");      max_rate = Input_Float("Last rate  (bits/pixel)");      rate_inc = Input_Float("Rate increment"); } }  do {    int ii, jj;    data_file.open_file(cod_f_name);    dimension.x = data_file.decode_bits(6) << 8;    dimension.y = data_file.decode_bits(6) << 8;    dimension.x += data_file.decode_bits(8);    dimension.y += data_file.decode_bits(8);    rate_mult = 8.0 / (float(dimension.x) * float(dimension.y));    threshold_bits = data_file.decode_bits(5);    for (ii = 0; ii < 3; ii++) {      mean[ii] = data_file.decode_bits(8);      for (jj = 0; jj < 3; jj++) {        transf_matrix[ii][jj] = data_file.decode_bits(8) << 8;        transf_matrix[ii][jj] += data_file.decode_bits(8); } }    printf("\n  Image size = %d x %d\n", dimension.x, dimension.y);    if (interactive)      bit_rate = Input_Float("Code rate (bits/pixel)");    byte_budget = long(bit_rate / rate_mult) - 1;    code_time.reset();  total_time.reset();  total_time.start();    image.reset(dimension, mean, transf_matrix);    pyramid_dim = image.pyramid_dim();    pyramid_levels = Min(Max_Levels(pyramid_dim.x),                          Max_Levels(pyramid_dim.y));    code_time.start("\n  Starting image recovery...");    Process_Image();    code_time.display(" Image recovered in");    data_file.close_file();    image.recover();    total_time.stop();    if (comp) {      printf("\n  Rate =%6.3f bits/pixel --> ", rate_mult *         data_file.bytes_used());      mse = image.compare(pic_f_name);      if (mse > 0.0) {        psnr = dBW(65025.0 / mse);        printf("mean squared-error =%8.3f =%7.2f dB PSNR\n",          mse, psnr); }      else        puts("no differences encountered"); }    total_time.display("\n  Total execution time (I/O included) =");    if (interactive) {      if (Input_Answer("Save new image to disk")) {        Input_Line("Name of the new image file", new_f_name);        image.write_pic(new_f_name); }      cont = Input_Answer("Test new rate"); }    else      if (cml) {        if (!comp) image.write_pic(new_f_name);        cont = false; }      else {        fprintf(plot_file, "%9.4f %9.4f\n", bit_rate, psnr);        bit_rate += rate_inc;        cont = (bit_rate <= max_rate); }  } while (cont);  if (!(cml || interactive)) fclose(plot_file);  puts(" ");  return 0;}#endif// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =// end of file < CodeColr.C >

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -