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

📄 huffman_table.c

📁 spiht for linux this is used to decod and encode vedio i wich all enjoy
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  * QccPack: Quantization, compression, and coding libraries * Copyright (C) 1997-2009  James E. Fowler *  * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. *  * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Library General Public License for more details. *  * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, * MA 02139, USA. *  */#include "libQccPack.h"int QccENTHuffmanTableInitialize(QccENTHuffmanTable *table){  if (table == NULL)    return(0);  table->table_type = QCCENTHUFFMAN_DECODETABLE;  table->table = NULL;  table->table_length = 0;  table->num_codewords_list = NULL;  table->num_codewords_list_length = 0;  table->symbol_list = NULL;  table->symbol_list_length = 0;  return(0);}int QccENTHuffmanTableAlloc(QccENTHuffmanTable *table){  if (table == NULL)    return(0);  if (table->table_length <= 0)    return(0);  if ((table->table =       (QccENTHuffmanTableEntry *)malloc(sizeof(QccENTHuffmanTableEntry) *                                         table->table_length)) == NULL)    {      QccErrorAddMessage("(QccENTHuffmanTableAlloc): Error allocating memory");      return(1);    }  return(0);}void QccENTHuffmanTableFree(QccENTHuffmanTable *table){  if (table == NULL)    return;  if (table->table != NULL)    QccFree(table->table);  if (table->num_codewords_list != NULL)    QccFree(table->num_codewords_list);  if (table->symbol_list != NULL)    QccFree(table->symbol_list);}void QccENTHuffmanTablePrint(const QccENTHuffmanTable *table){  int entry;  int index;  if (table == NULL)    return;  printf("Huffman Codewords:\n");  for (entry = 0; entry < table->table_length; entry++)    {      printf("    Symbol: %.2x, Length: %2d, Codeword: ",             table->table[entry].symbol,             table->table[entry].codeword.length);      QccENTHuffmanCodewordPrint(&table->table[entry].codeword);    }  printf("  Decode Table:\n");  for (index = 0; index < QCCENTHUFFMAN_MAXCODEWORDLEN; index++)    printf("    Length: %d, Min: %d, Max: %d, Ptr: %d\n",           index + 1,           table->codeword_min[index],           table->codeword_max[index],           table->codeword_ptr[index]);}static int QccENTHuffmanTableReadHeader(FILE *fileptr,                                        QccENTHuffmanTable *table){  if (QccFileReadMagicNumber(fileptr,                             table->magic_num,                             &table->major_version,                             &table->minor_version))    {      QccErrorAddMessage("(QccENTHuffmanTableReadHeader): Error calling QccFileReadMagicNumber()");      return(1);    }  if (!strcmp(table->magic_num, QCCCHANNEL_MAGICNUM))    {      QccErrorAddMessage("(QccENTHuffmanTableReadHeader): %s is not of Huffman table (%s) type",                         table->filename, QCCENTHUFFMANTABLE_MAGICNUM);      return(1);    }  fscanf(fileptr, "%d", &(table->num_codewords_list_length));  if (ferror(fileptr) || feof(fileptr))    {      QccErrorAddMessage("(QccENTHuffmanTableReadHeader): Error reading number of codewords list length in %s",                         table->filename);      return(1);    }  fscanf(fileptr, "%d", &(table->symbol_list_length));  if (ferror(fileptr) || feof(fileptr))    {      QccErrorAddMessage("(QccENTHuffmanTableReadHeader): Error reading symbol list length in %s",                         table->filename);      return(1);    }  return(0);}static int QccENTHuffmanTableReadData(FILE *fileptr,                                      QccENTHuffmanTable *table){  int index;  for (index = 0; index < table->num_codewords_list_length; index++)    {      fscanf(fileptr, "%d", &(table->num_codewords_list[index]));      if (ferror(fileptr) || feof(fileptr))        {          QccErrorAddMessage("(QccENTHuffmanTableReadData): Error reading number of codewords in %s",                             table->filename);          return(1);        }    }  for (index = 0; index < table->symbol_list_length; index++)    {      fscanf(fileptr, "%d", &(table->symbol_list[index]));      if (ferror(fileptr) || feof(fileptr))        {          QccErrorAddMessage("(QccENTHuffmanTableReadData): Error reading number of codewords in %s",                             table->filename);          return(1);        }    }  return(0);}int QccENTHuffmanTableRead(QccENTHuffmanTable *table){  FILE *fileptr = NULL;  if (table == NULL)    return(0);  if ((table->table_type != QCCENTHUFFMAN_DECODETABLE) &&      (table->table_type != QCCENTHUFFMAN_ENCODETABLE))    {      QccErrorAddMessage("(QccENTHuffmanTableRead): Invalid Huffman table type specified");      return(1);    }  if ((fileptr = QccFileOpen(table->filename, "r")) == NULL)    {      QccErrorAddMessage("(QccENTHuffmanTableRead): Error calling QccFileOpen()");      return(1);    }  if (QccENTHuffmanTableReadHeader(fileptr, table))    {      QccErrorAddMessage("(QccENTHuffmanTableRead): Error calling QccENTHuffmanTableReadHeader()");      return(1);    }  if ((table->num_codewords_list =       (int *)malloc(sizeof(int) *                     table->num_codewords_list_length)) == NULL)    {      QccErrorAddMessage("(QccENTHuffmanTableRead): Error allocating memory");      return(1);    }  if ((table->symbol_list =       (int *)malloc(sizeof(int) *                     table->symbol_list_length)) == NULL)    {      QccErrorAddMessage("(QccENTHuffmanTableRead): Error allocating memory");      return(1);    }  if (QccENTHuffmanTableReadData(fileptr, table))    {      QccErrorAddMessage("(QccENTHuffmanTableRead): Error calling QccENTHuffmanTableReadData()");      return(1);    }  if (table->table_type == QCCENTHUFFMAN_DECODETABLE)    {      if (QccENTHuffmanTableCreateDecodeTable(table))        {          QccErrorAddMessage("(QccENTHuffmanTableRead): Error calling QccENTHuffmanTableCreateDecodeTable()");          return(1);        }    }  else    {      if (QccENTHuffmanTableCreateEncodeTable(table))        {          QccErrorAddMessage("(QccENTHuffmanTableRead): Error calling QccENTHuffmanTableCreateEncodeTable()");          return(1);        }    }  QccFileClose(fileptr);  return(0);}static int QccENTHuffmanTableWriteHeader(FILE *fileptr,                                         QccENTHuffmanTable *table){  if (QccFileWriteMagicNumber(fileptr, QCCENTHUFFMANTABLE_MAGICNUM))    goto Error;  fprintf(fileptr, "%d\n%d\n",          table->num_codewords_list_length, table->symbol_list_length);  if (ferror(fileptr))    goto Error;    return(0); Error:  QccErrorAddMessage("(QccENTHuffmanTableWriteHeader): Error writing header to %s",                     table->filename);  return(1);}static int QccENTHuffmanTableWriteData(FILE *fileptr,                                       QccENTHuffmanTable *table){  int index;  for (index = 0; index < table->num_codewords_list_length; index++)    fprintf(fileptr, "%d\n",            table->num_codewords_list[index]);  for (index = 0; index < table->symbol_list_length; index++)    fprintf(fileptr, "%d\n",            table->symbol_list[index]);  return(0);}

⌨️ 快捷键说明

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