UINT8* encodeMcu( UINT32 imageFormat, UINT8 *outputBuffer ) { levelShift(Y1); dct(Y1); quantization(Y1, ILqt); outputBuffer = huffman(1, outputBuffer); #if 0 if (imageFormat != GRAY) { levelShift(CB); dct(CB); quantization(CB, ICqt); outputBuffer = huffman(2, outputBuffer); levelShift(CR); dct(CR); quantization(CR, ICqt); outputBuffer = huffman(3, outputBuffer); } #endif //0 return outputBuffer; }
int main() { Huffman huffman("files/out.txt", "files/res.txt", "files/table.txt"); huffman.decompress(); return 0; }
int main(void) { initModel(MSG); huffman(); printf("Дърво на Хъфман за %s:\n",MSG); printTree(forest[1].root,0); writeCodes(forest[1].root,0); return 0; }
GTEST_TEST(Huffman, searchMaxLength) { Common::MemoryReadStream byteStream(kHuffmanData); Common::BitStream8MSB bitStream (byteStream); Common::Huffman huffman(0, ARRAYSIZE(kCodes), kCodes, kLengths, kSymbols); for (size_t i = 0; i < ARRAYSIZE(kDeHuffmanDataSymbols); i++) EXPECT_EQ(huffman.getSymbol(bitStream), kDeHuffmanDataSymbols[i]) << "At index " << i; }
int main(int argc, char**argv) { FreqNode *root; char *alltext; FreqNode *buffer[256] = { NULL }; NodeLList *txtfreqs = newlist(); NodeLList *auxlist; Stack *auxstack; char auxchar; char auxchar2; int i, j; int numvalidsymbols; int highestfreq; int isfirst; if (argc == 1) { printf("Error: Missing filename as argument.\n"); exit(-1); } alltext = readfile(argv[1], buffer); numvalidsymbols = 0; for (i = 0; i < 256; i++) { if (buffer[i] && strcmp(buffer[i]->sym, " ") >= 0 && strcmp(buffer[i]->sym, "~") <= 0 ) { numvalidsymbols++; } } isfirst = 1; for (i = 0; i < numvalidsymbols; i++) { for (j = 32; j < 127; j++) { if (buffer[j] && isfirst) { highestfreq = j; isfirst = 0; } else if (buffer[j] && buffer[j]->freq > buffer[highestfreq]->freq) { highestfreq = j; } } addend(&txtfreqs, buffer[highestfreq]); buffer[highestfreq] = NULL; isfirst = 1; } auxlist = copylist(txtfreqs); root = huffman(&txtfreqs, numvalidsymbols-1); auxstack = newstack(); auxchar = 0 << 7 | 0 << 6 | 0 << 5 | 0 << 4 | 0 << 3 | 0 << 2 | 0 << 1 | 0 << 0; push(&auxstack, auxchar); generatehuffcodes(&root, &auxstack, 0); writefile(argv[1], alltext, root, auxlist, numvalidsymbols); return 0; }
void lancementSimple(char* chemin, char* name) { int T[N]; int nb_occurence[N]; int i; int f,d; extentionDossierCalcul(chemin,&f,&d); char* format = malloc(sizeof(char)*(f+1)); char* dossier = malloc(sizeof(char)*(d+1)); extentionDossierCreation(chemin,format,dossier,&f,&d); char fichierCode[strlen(dossier)+strlen(name)+4]; char fichierRetour[strlen(dossier)+strlen(name)+1+strlen(format)]; strcpy(fichierCode,dossier); strcat(fichierCode,name); strcat(fichierCode,".txt"); strcpy(fichierRetour,dossier); strcat(fichierRetour,name); strcat(fichierRetour,"."); strcat(fichierRetour,format); pArbre res = huffman(chemin,nb_occurence); for (i=0;i<N;i++){ T[i]=0; } profondeur(res,T,0); unsigned char nb_symbole=255; for (i=0;i<N;i++){ if (T[i]!=0){ nb_symbole++;}} //calcul de la taille int taille=0; for (i=0;i<N;i++){ taille= taille + nb_occurence[i]*T[i]; } pArbre A=construction_arbre_canonique(T); FILE *F1= ouvertureFichierLecture(chemin); FILE *F2 = ouvertureFichierEcriture (fichierCode); //ecriture du nombre de symbole putByte(F2,nb_symbole); codage(F1,F2,A,taille); fermetureFichier(F1); fermetureFichier(F2); }
/* * @param data needs to be the sample, difference vector (num of bits of difference vector) * or mdct vector. * @brief Compress using huffman code. * @return the Huffman tree. */ node_t* huffman_compress(uint8_t *data, frequency_t *_frequency, uint32_t num_samples, uint8_t bound) { size_t i; /* loop indexes */ /* begin the frequency count */ for (i = 0; i < num_samples; i++) { _frequency[data[i]]++; } return huffman(_frequency, bound); }
GTEST_TEST(Huffman, eos) { Common::MemoryReadStream byteStream(kHuffmanData); Common::BitStream8MSB bitStream (byteStream); Common::Huffman huffman(kMaxLength, ARRAYSIZE(kCodes), kCodes, kLengths, kSymbols); for (size_t i = 0; i < ARRAYSIZE(kDeHuffmanDataSymbols); i++) huffman.getSymbol(bitStream); EXPECT_THROW(huffman.getSymbol(bitStream), Common::Exception); }
int main() { int i; probe[1] = 0x80; for(i = 2;i <= 8;i ++) probe[i] = probe[i-1] >> 1; char psource[200] = "d:\\\\in.txt",phuff[200] = "D:\\\\out.huff"; HDeCompress huffman(psource,phuff); huffman.DeCompress(); return 0; }
int main() { puts("1. 哈夫曼编码\n2. 图的邻接矩阵表示\n>>"); int choice; scanf("%d", &choice); switch (choice) { case 1: huffman(); break; case 2: graph(); break; } return 0; }
GTEST_TEST(Huffman, invalidSymbol) { static const uint32 kInvalidCodes [] = { 0, 4, 5, 6 }; static const uint8 kInvalidLengths[] = { 1, 3, 3, 3 }; static const uint8 kInvalidMaxLength = 3; static const byte kInvalidHuffmanData[] = { 0x77 }; Common::MemoryReadStream byteStream(kInvalidHuffmanData); Common::BitStream8MSB bitStream (byteStream); Common::Huffman huffman(kInvalidMaxLength, ARRAYSIZE(kInvalidCodes), kInvalidCodes, kInvalidLengths, 0); EXPECT_EQ(huffman.getSymbol(bitStream), 0); EXPECT_THROW(huffman.getSymbol(bitStream), Common::Exception); }
int main(int argc, char **argv) { int argn; int do_huffman, do_hist; int cnt; cnt = 0; argn = tino_getopt(argc, argv, 1, 0, TINO_GETOPT_VERSION(HISTOGRAM_VERSION) TINO_GETOPT_LLOPT " file [..]\n" " Use - as filename to read stdin", TINO_GETOPT_USAGE "help this help" , TINO_GETOPT_FLAG TINO_GETOPT_COUNT "huff print huffman tree" , &cnt , &do_huffman, TINO_GETOPT_FLAG TINO_GETOPT_COUNT "hist print histogram (default if nothing else given)" , &cnt , &do_hist, NULL ); if (argn<=0) return 1; for (; argn<argc; argn++) readfile(argv[argn]); if (do_huffman) huffman(); if (do_hist || !cnt) histogram(); return 0; }
int main(int argc, char *argv[]) { int n, m, *freq; char text[NUM], *dict; PHT p = NULL; PCode pc = NULL; scanf("%s", &text); n = strlen(text); m = calc_freq(text, &freq, &dict, n); huffman(&p, freq, dict, m); get_code(&pc, p, m); show_result(pc, m); return 0; }
int main() { pqueue pq; int n; int *a = pq.a; while(scanf("%d", &n) != EOF && n){ int i, weight; for(i=1;i<=n;i++) scanf("%d", &(a[i])); pq.size = n; weight = huffman(&pq); /* calculate weight of all nodes */ printf("%d\n",weight); } return 0; }
int main(void){ int i; int len; char c; while(1){ scanf("%s",str); if(strcmp(str,"END")==0){ break; } memset(hz,0,sizeof(hz)); memset(node,0,sizeof(Node)); nc=0; entropy=0; i=0; while((c=str[i])!='\0'){ if(c=='_'){ hz[26]++; }else{ hz[(int)(c-'A')]++; } i++; } len=i; for(i=0;i<27;i++){ if(hz[i]){ node[nc++]=create_node(hz[i],NULL,NULL); } } if(nc==1){//只有一个节点,特殊处理 printf("%d %d 8.0\n",8*len,len); continue; } huffman(); len=8*len; printf("%d %d %.1f\n",len,entropy,(double)(len)/(double)(entropy)); } return 0; }
void test_huffman2(void) { struct hcnode *root, *n; unsigned int freqs[256]; int chars[256]; int count = 256, i; struct hcnode **nodes; for(i=0; i<count; i++) { freqs[i] = i; chars[i] = i; } nodes = huffman_init(freqs, chars, 256); root = huffman(nodes, 256); huffman_make_codes(root); for (i=0; i<count; i++) { n = nodes[i]; printf("code for character %d: 0x%x\n", n->character, bits_to_integer(n->code, n->code_len)); } }
int main(int argc, char **argv) { std::srand(std::time(0)); ///////////////////////////////////////// Exemple d'un seuillage d'image char cNomImgLue[250], cNomImgEcrite[250]; int S = 100; sscanf (argv[1],"%s",cNomImgLue) ; // sscanf (argv[2],"%s",cNomImgEcrite); // sscanf (argv[3],"%d",&S); int width, height; OCTET *in, *out; lire_nb_lignes_colonnes_image_ppm(cNomImgLue, &width, &height); allocation_tableau(in, OCTET, width * height * 3); lire_image_ppm(cNomImgLue, in, width * height); // codagePredictif(in, width, height); huffman(in, width, height); return 0; }
void test_huffman(void) { struct hcnode *root, *n; /* This example is from CLRS */ u32 freqs[] = {5, 9, 12, 13, 16, 45}; int chars[] = {'f','e','c','b','d','a'}; u32 expect_codes[] = {0xc, 0xd, 0x4, 0x5, 0x7, 0x0}; int count = 6, i; struct hcnode **nodes; nodes = huffman_init(freqs, chars, 6); root = huffman(nodes, 6); huffman_make_codes(root); for (i=0; i<count; i++) { n = nodes[i]; /* printf("code for character %c (freq %d): %x (len %d) (expecting %x)\n", n->character, n->frequency, bits_to_integer(n->code, n->code_len), n->code_len, expect_codes[i]); */ assert(bits_to_integer(n->code, n->code_len) == expect_codes[i]); } }
int main(int argc, char** argv) { //SE OS ARGUMENTOS SÃO NULL'S , RETORNA if(argv[1] == NULL || argv[2] == NULL){ printf("Falta argumentos para a funçao. Retornando."); return; } FILE *fp; //ponteiros de arquivo de escrita fp = fopen(argv[1] , "r"); // abre o arquivo para a primeira leitura if(fp == NULL) return; TipoLista l; FazListaVazia(&l); primeiraleitura(fp , &l); ordenaauxiliar(&l); crialista(&l); huffman(&l); fclose(fp); //fecha o arquivo fp = fopen(argv[1] , "r"); // abre o arquivo para a segunda leitura segundaleitura(fp , &l , argv[2]); fclose(fp); //fecha o arquivo novamente freeall(&l); //da free nas estruturas usadas }
main() { char str[30]; int len; printf("enter the string\n"); scanf("%s",str); for(len=0;str[len]!='\0';len++); struct node *f[len]; char ch[len]; len=*freq(str,ch,f,len); struct node *start = huffman(len,f); /* int i; for(i=0;i<5;i++) { printf("%d",start->data); start=start->left; }*/ in(start); }
void codagePredictif(OCTET *in, int width, int height) { OCTET *out; allocation_tableau(out, OCTET, width * height); std::vector<int> diffs; auto topPixelDifference = [=] (int i, int j, int value) { return at(in, width, height, i, j - 1, 0) - value; }; auto leftPixelDifference = [=] (int i, int j, int value) { return at(in, width, height, i - 1, j, 0) - value; }; auto toBitSet = [] (int value) { std::vector<bool> s; bool negative = (value < 0); value = fabs(value); if (value >= pow(2, 6)) { value = pow(2, 6) - 1; } // std::cout << value << " = "; value /= 2; s.push_back(negative); while (value != 0) { s.push_back(value % 2); value /= 2; } while (s.size() < 8) s.push_back(0); return s; }; diffs.push_back(in[0]); //0 si pixel du dessus //1 si pixel de gauche for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int value = at(in, width, height, i, j, 0); int tpd = 255; int lpd = 255; if (i == 0 && j == 0) continue; if (i == 0) { //prendre le pixel du dessus tpd = topPixelDifference(i, j, value); } else if (j == 0) { //prendre le pixel de gauche lpd = leftPixelDifference(i, j, value); } else { lpd = leftPixelDifference(i, j, value); tpd = topPixelDifference(i, j, value); } if(fabs(lpd) < fabs(tpd)) { diffs.push_back(lpd); //TODO noter si c'est left ou top qui est utilisé // c'est le bit de poids fort qui s'en charge } else { diffs.push_back(tpd); } out[diffs.size() - 1] = diffs[diffs.size() - 1] + 128; // std::cout << diffs[diffs.size() - 1] << std::endl; } } //codage de la valeur sur 6 bits plus 1 bit de signe // toBitSet(-46); std::stack<bool> *vec = new std::stack<bool>(); std::vector<OCTET> *octets = new std::vector<OCTET>(); std::for_each (diffs.begin(), diffs.end(), [=](int n) { std::vector<bool> c = toBitSet(n); std::for_each(c.begin(), c.end(), [=](bool b) { vec->push(b); }); if(vec->size() > 8) { octets->push_back(toChar(vec)); // std::cout << toChar(vec); } }); huffman(&(*octets)[0], octets->size(), 1); char a[] = "out.pgm"; ecrire_image_pgm(a, out, width, height); }
uint32_t huffman_decompress(FILE *fp, frequency_t *_frequency, uint32_t num_samples, char** codes) { uint32_t count; /* count the number of frequency-sample */ size_t reading; /* fread control return */ size_t i, j = 0; /* loop indexes */ uint8_t data; /* data sample */ char target[MAX_HUFF_CODE]; /* target code to search in the table */ unsigned char read_byte; /* bytes read from file */ tree_t *huffman_tree = NULL; /* one huffman tree */ uint8_t bits = 0; /* number of bits of the last byte */ uint32_t header_counter; /* count the number of bytes in the huffman header */ char *buffer = NULL; char *sample = NULL; huffman_tree = (tree_t*) malloc(sizeof(tree_t)); tree_create(&huffman_tree); /* reading the huffman header */ reading = fread(&bits, sizeof(uint8_t), 1,fp); if (reading != 1) { TRACE("[ERROR] Fail to read file -- number of bits of the last character\n"); return 0; } reading = fread(&count, sizeof(uint32_t), 1, fp); if (reading != 1) { TRACE("[ERROR] Fail to read file -- frequency counter\n"); return 0; } for (i = 0; i < count; i++) { data = 0; fread(&data, sizeof(uint8_t), 1, fp); fread(&_frequency[data], sizeof(frequency_t), 1, fp); } /* huffman header has been read */ /*huffman_table(table, count);*/ huffman_tree->root = huffman(_frequency, MAX_SAMPLE); /*generate_table(huffman_tree->root, table, _frequency);*/ /* decode every byte read from data sector in input file */ memset(target, '\0', MAX_HUFF_CODE); while(j < num_samples) { /* read file until find a huffman code */ fread(&read_byte, sizeof(unsigned char), 1, fp); for (i = 0; i < 8 && j < num_samples; i++) { strncat(target, (read_byte >> (7-i)) & 0x01 ? "1" : "0", sizeof(char)); if (get_leaf(huffman_tree->root, target) != NULL) { /* found the huffman code? */ /*codes[j] = (char*) malloc((strlen(target) + 1) * sizeof(char));*/ memset(codes[j], '\0', (strlen(target) + 1) * sizeof(char)); strncpy(codes[j], target, strlen(target) * sizeof(char)); memset(target, '\0', MAX_HUFF_CODE * sizeof(char)); j++; } } } buffer = (char*) malloc(MAX_CODE * sizeof(char)); /* data frequency bits count num_samples*/ header_counter = count * (sizeof(uint8_t) + sizeof(frequency_t)) + sizeof(uint8_t) + sizeof(uint32_t) + sizeof(uint32_t); for (i = 0; i < num_samples; i++) { sample = get_leaf(huffman_tree->root, codes[i]); if (sample != NULL) { memset(buffer, '\0', (strlen(sample) - 1) * sizeof(char)); strncpy(buffer, (sample+1), (strlen(sample) - 2) * sizeof(char)); data_sample[i] = (uint8_t) string_to_int(buffer); } } free(huffman_tree); return header_counter; }