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

📄 sp_decd.c

📁 常用的无损压缩算法源代码
💻 C
字号:
/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = *//*               S + P   I M A G E   C O M P R E S S I O N             *//* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = *//*     > > > > >    ANSI C version  2.06  -  05/30/95    < < < < <     *//*                      File shared by the decoders                    *//*   Amir Said - amir@densis.fee.unicamp.br                            *//*   Faculty of Electrical Engineering                                 *//*   University of Campinas (UNICAMP) - Campinas, SP 13081, Brazil     *//*   William A. Pearlman - pearlman@ecse.rpi.edu                       *//*   Dept. of Electrical, Computer, and Systems Engineering            *//*   Rensselaer Polytechnic Institute - Troy, NY 12180, USA            *//*     Copyright (c) 1995 Amir Said & William A. Pearlman              *//*   This program is Copyright (c) by Amir Said and William A. Pearlman.   It may be freely redistributed in its entirety provided that this   copyright notice is not removed. It may not be sold for profit or   incorporated in commercial programs without the written permission   of the copyright holders. This program is provided as is, without   any express or implied warranty, without even the warranty of   fitness for a particular purpose.*//* - - Static variables  - - - - - - - - - - - - - - - - - - - - - - - */char * r_msg = "cannot read coded file";char * w_msg = "cannot write to PIC file";Decoder code_file;/* - - Function prototypes - - - - - - - - - - - - - - - - - - - - - - */void Read_Header(Image_Coord *, int *, int *, int *, long *);long Write_Pic(Image_Coord, int, char *, int **);void Recover(Image_Coord, int, int, int **);int ** Decode_Image(int, Image_Coord);/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - *//* - - Main function - - - - - - - - - - - - - - - - - - - - - - - - - */int main(int numb_arg, char ** arg){  Image_Coord dim;  int bytes, mean, levels, ** image;  long check_sum;  double CPU_time;  FILE * temp_file;  if (numb_arg != 3) {    printf("\nUsage: %s < PIC file > < compressed file >\n", arg[0]);    exit(0); }  if ((temp_file = fopen(arg[2], "rb")) != NULL) {    printf("Overwrite file %s? (y = yes, otherwise quit) ", arg[2]);    fclose(temp_file);  gets(line);    if (line[0] != 'y') exit(0); }  CPU_time = (double) clock();  Start_Decoder(&code_file, arg[1]);  Read_Header(&dim, &mean, &levels, &bytes, &check_sum);  puts("\n  Compressed image data:");  printf("\tdimension   = %3d x %3d\n", dim.y, dim.x);  printf("\tbytes/pixel = %3d\n", bytes);  Construct_Set_Table();  image = Decode_Image(levels, dim);  Stop_Decoder(&code_file);  Recover(dim, levels, mean, image);  if (Write_Pic(dim, bytes, arg[2], image) != check_sum)    Error("Incorrect check sum");  printf("\n  CPU time = %6.2f s.\n", ((double) clock() - CPU_time) /    (double) CLOCKS_PER_SEC);  return 0;}/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - *//* - - Implementations - - - - - - - - - - - - - - - - - - - - - - - - */void Read_Header(Image_Coord * d, int * m, int * l, int * b, long * c){  int i, k1, k2;  long p[3];  k1 = Read_Bits(&code_file, 8);  k2 = Read_Bits(&code_file, 8);  if ((k1 != KEY_1) || ((k2 != KEY_2) && (k2 != KEY_3)))    Error("Invalid compressed file format");  smooth_image = (k2 == KEY_3);  d->x = Read_Bits(&code_file, 8) << 2;  d->x |= Read_Bits(&code_file, 6) << 10;  d->y = Read_Bits(&code_file, 8) << 2;  d->y |= Read_Bits(&code_file, 6) << 10;  *m = Read_Bits(&code_file, 8);  *m |= Read_Bits(&code_file, 8) << 8;  *l = Read_Bits(&code_file, 4);  *b = Read_Bits(&code_file, 1) + 1;  for (i = 0; i <= 2; i++) p[i] = Read_Bits(&code_file, 8);  *c = p[0] | (p[1] << 8) | (p[2] << 16);}/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */long Write_Pic(Image_Coord d, int bytes, char * file_name, int ** pic){  int i, j, p;  long chk_sum = 0;  FILE * pic_file = fopen(file_name, "wb");  if (pic_file == NULL) Error(w_msg);  for (i = 0; i < d.x; i++)    for (j = 0; j < d.y; j++) {      p = pic[i][j];  chk_sum += p ^ (p >> 1);  chk_sum &= 0xFFFFFFL;      if (bytes == 2)        if (putc(p >> 8, pic_file) == EOF) Error(w_msg);      if (putc(p & 0xFF, pic_file) == EOF) Error(w_msg); }  if (fclose(pic_file)) Error(w_msg);  return chk_sum;}/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */void SP_Recover(int m, int n, int l[], int h[], int c[]){  int i, k, d1, d2, d3;  if (smooth_image) {    d2 = l[m-2] - l[m-1];  d1 = l[m-3] - l[m-2];  h[m-1] += d2 >> 2;    for (i = m - 2; i > 1; i--) {      d3 = d2;  d2 = d1;  d1 = l[i-2] - l[i-1];      h[i] += ((d3 << 3) + (d2 << 2) - d1 - 6 * h[i+1] + 7) >> 4; }    h[1] += (((d1 + d2 - h[2]) << 1) + d2 + 3) >> 3; }  else {    h[m-1] += (d1 = l[m-2] - l[m-1]) >> 2;    for (i = m - 2; i > 0; i--) {      d2 = d1;  d1 = l[i-1] - l[i];      h[i] += (((d1 + d2 - h[i+1]) << 1) + d2 + 3) >> 3; } }  h[0] += d1 >> 2;  for (i = k = 0; k < n; i++, k += 2) {    c[k] = l[i] + ((h[i] + 1) >> 1);    c[k+1] = c[k] - h[i]; }}/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */void Recover(Image_Coord d, int levels, int mean, int ** cf){  int lv, i, j, mx, my, nx = d.x >> levels, ny = d.y >> levels;  int * in_line = buffer[0], * out_line = buffer[1];  for (i = 0; i < nx; i++)    for (j = 0; j < ny; j++) cf[i][j] += mean;  for (lv = levels; lv > 0; lv--) {    mx = nx;  nx <<= 1;  my = ny;  ny <<= 1;    for (i = 0; i < nx; i++) {      memcpy(in_line, cf[i], ny * sizeof(int));      SP_Recover(my, ny, in_line, in_line + my, cf[i]); }    for (j = 0; j < ny; j++) {      for (i = 0; i < nx; i++) in_line[i] = cf[i][j];      SP_Recover(mx, nx, in_line, in_line + mx, out_line);      for (i = 0; i < nx; i++) cf[i][j] = out_line[i]; } }}/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = *//* end of file < sp_decd.c > */

⌨️ 快捷键说明

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