/* ** Read command line params, call search */ int main(int argc, char *argv[]) { if (argc != 4) { usage(argv[0]); return(-1); } grid_t *sonar = readGrid(strcmp(argv[1],"-")?fopen(argv[1],"r"):stdin, TRUE); grid_t *plotter = readGrid(strcmp(argv[2],"-")?fopen(argv[2],"r"):stdin, FALSE); bit_file_t *bitfile = BitFileOpen(argv[3], BF_READ); assert(sonar); assert(plotter); assert(bitfile); // temporary home for terrain heights data_t *result = (data_t *)malloc(sizeof(data_t) * plotter->width * plotter->height); assert(result); for(uint32_t pos = 0 ; pos < plotter->width * plotter->height ; pos++) { data_t temp = 0, len = 0, tempLen = 0; //get the length of the data part of the PBF from the length defining part, store it into "len" do { tempLen = 0; BitFileGetBitsInt(bitfile, &tempLen, 3, sizeof(len)); len += tempLen; } while (tempLen == 7); //get the stored data from the data part for (int i = 0; i < len; i++) { int lastbit = BitFileGetBit(bitfile); temp = temp*2 + lastbit; } //actual height = sonar reading - inaccuracy temp = sonar->data[plotter->data[pos]] - temp; result[plotter->data[pos]] = temp; } assert(!BitFileClose(bitfile)); printf("%d %d\n", plotter->width, plotter->height); for(uint32_t pos = 0 ; pos < plotter->width * plotter->height ; pos++) printf("%d\n",result[pos]); free(result); return 0; }
void extract_fextract(bit_file_t *compressed, bit_file_t *extracted, tree_node* root, unsigned int bytecount) { unsigned int bytesread = 0; tree_node *current = root; while (bytesread < bytecount) { current = BitFileGetBit(compressed) ? current->right : current->left; if (tree_is_leaf(current)) { BitFilePutChar(current->content, extracted); current = root; bytesread++; } } }
tree_node *extract_fread_bytemap(bit_file_t *compressed, int uniquebytes) { int i, j, c, size; tree_node *root, *current, **next; root = tree_create_branch(NULL, NULL); for (i = 0; i < uniquebytes; i++) { c = BitFileGetChar(compressed); size = BitFileGetChar(compressed); current = root; for (j = 0; j < size; j++) { next = BitFileGetBit(compressed) ? ¤t->right : ¤t->left; if (*next == NULL) { *next = (j == size - 1) ? tree_create_leaf(c, 0) : tree_create_branch(NULL, NULL); } current = *next; } } return root; }
void DecodeFile(FILE *infile,FILE *outfile){ int ch; huffman_node_t *huffmanLeaves[CHARCOUNT], *htree, *currentnode; huffman_code_t *huffmanCodes[CHARCOUNT]; buffered_bit_file_t *bufferedInFile; buffered_bit_file_t *bufferedOutFile=CreateBitOutFile(outfile); for(ch=0;ch<CHARCOUNT;ch++){ huffmanLeaves[ch]=CreateNode(ch); } huffmanLeaves[EOF_CHAR]->value=EOF_CHAR; huffmanLeaves[EOF_CHAR]->count=1; ReadFrequencyArray(infile, huffmanLeaves); htree=BuildTree(huffmanLeaves, CHARCOUNT); bufferedInFile=CreateBitInFile(infile); if(htree != huffmanLeaves[EOF_CHAR]){ for(int i=0;i<CHARCOUNT;i++){ huffmanCodes[i]=CreateCode(); } GenerateCodeList(huffmanCodes, htree); currentnode=htree; for(;;){ int bit=BitFileGetBit(bufferedInFile); if(bit != 0) currentnode=currentnode->left; else currentnode=currentnode->right; if(currentnode->value != COMBINE_NODE){ if(currentnode->value == EOF_CHAR){ BitFileFlush(bufferedOutFile); break; } BitFilePutChar(bufferedOutFile, currentnode->value); currentnode=htree; } } for(int i=0;i<CHARCOUNT;i++) free(huffmanCodes[i]); } FreeHuffmanTree(htree); free(bufferedInFile); free(bufferedOutFile); }
/** * Loads a DWT file */ void DWT::load(const string &fileName) { // Open file and read magic number FILE *fHan = fopen(fileName.data(), "rb"); char thisMagic[2]; fread(thisMagic, 1, 2, fHan); if (magic[0] != thisMagic[0] || magic[1] != thisMagic[1]) { cerr << "Unrecognized file format for " << fileName << endl; exit(1); } // Read header fread(&bpp, sizeof(bpp), 1, fHan); fread(&realWidth, sizeof(int), 1, fHan); fread(&realHeight, sizeof(int), 1, fHan); #ifdef __BIG_ENDIAN__ bpp = swap_endian32(bpp); realWidth = swap_endian32(realWidth); realHeight = swap_endian32(realHeight); #endif // Calculate padded width and height width = 1; while (width < realWidth) width <<= 1; height = 1; while (height < realHeight) height <<= 1; // Drops half of the padding when reading unsigned int stopHeight = height - (height - realHeight)/2; unsigned int stopWidth = width - (width - realWidth)/2; unsigned int stopPrHeight = stopHeight * PREVIEW / height; unsigned int stopPrWidth = stopWidth * PREVIEW / width; cout << "bpp: " << bpp << endl << "width: " << width << endl << "height: " << height << endl << "realWidth: " << realWidth << endl << "realHeight: " << realHeight << endl; coeff = new float[width * height]; int maxAbsVal; fread(&maxAbsVal, sizeof(int), 1, fHan); cout << "maxValue: " << maxAbsVal << endl; const float C = ((1 << (bpp-1))-1)/(float)(maxAbsVal); // Range value const float M = (1 << (bpp-1)); // Added to get only positive values float W = 1, w = 1/sqrt(2); // Factor of multiplication for preview for (unsigned int i = PREVIEW; i < height; i <<= 1) W *= w; for (unsigned int i = PREVIEW; i < width; i <<= 1) W *= w; // Reading the preview for (unsigned int i = 0; i < stopPrHeight && i < PREVIEW; i++) { for (unsigned int j = 0; j < stopPrWidth && j < PREVIEW; j++) { unsigned char l; fread(&l, 1, 1, fHan); coeff[i*width + j] = l/W; } } // Reading the rest of the transform, decoding Huffman and RLE unsigned int zeros = 0; bit_file_t *bf = MakeBitFile(fHan, BF_READ); Huffman *huffman = new Huffman(bpp); huffman->setFile(bf); huffman->readTree(); for (unsigned int i = 0; i < stopHeight; i++) { for (unsigned int j = 0; j < stopWidth; j++) { if (i >= PREVIEW || j >= PREVIEW) { int l = 0; if (zeros > 0) { coeff[i * width + j] = 0; zeros--; } else { bool seq0 = BitFileGetBit(bf); if (seq0) { // RLE: read the number of coefficents to set to 0 and set the first coeff[i * width + j] = 0; bool cod8 = BitFileGetBit(bf); if (cod8) { BitFileGetBitsInt(bf, &zeros, 8, sizeof(zeros)); } else { BitFileGetBitsInt(bf, &zeros, 3, sizeof(zeros)); } } else { // Huffman: read coefficent and dequantize it l = huffman->readSymbol(); coeff[i*width + j] = (l-M)/C; } } } } } delete huffman; fclose(fHan); }
/**************************************************************************** * Function : HuffmanDecodeFile * Description: This routine reads a Huffman coded file and writes out a * decoded version of that file. * Parameters : inFile - Open file pointer for file to decode * outFile - Open file pointer for file receiving decoded data * Effects : Huffman encoded file is decoded * Returned : 0 for success, -1 for failure. errno will be set in the * event of a failure. Either way, inFile and outFile will * be left open. ****************************************************************************/ int HuffmanDecodeFile(FILE *inFile, FILE *outFile) { huffman_node_t *huffmanArray[NUM_CHARS]; /* array of all leaves */ huffman_node_t *huffmanTree; huffman_node_t *currentNode; int i, c; bit_file_t *bInFile; /* validate input and output files */ if ((NULL == inFile) || (NULL == outFile)) { errno = ENOENT; return -1; } bInFile = MakeBitFile(inFile, BF_READ); if (NULL == bInFile) { perror("Making Input File a BitFile"); return -1; } /* allocate array of leaves for all possible characters */ for (i = 0; i < NUM_CHARS; i++) { if ((huffmanArray[i] = AllocHuffmanNode(i)) == NULL) { /* allocation failed clear existing allocations */ for (i--; i >= 0; i--) { free(huffmanArray[i]); } inFile = BitFileToFILE(bInFile); return -1; } } /* populate leaves with frequency information from file header */ if (0 != ReadHeader(huffmanArray, bInFile)) { for (i = 0; i < NUM_CHARS; i++) { free(huffmanArray[i]); } inFile = BitFileToFILE(bInFile); return -1; } /* put array of leaves into a huffman tree */ if ((huffmanTree = BuildHuffmanTree(huffmanArray, NUM_CHARS)) == NULL) { FreeHuffmanTree(huffmanTree); inFile = BitFileToFILE(bInFile); return -1; } /* now we should have a tree that matches the tree used on the encode */ currentNode = huffmanTree; while ((c = BitFileGetBit(bInFile)) != EOF) { /* traverse the tree finding matches for our characters */ if (c != 0) { currentNode = currentNode->right; } else { currentNode = currentNode->left; } if (currentNode->value != COMPOSITE_NODE) { /* we've found a character */ if (currentNode->value == EOF_CHAR) { /* we've just read the EOF */ break; } fputc(currentNode->value, outFile); /* write out character */ currentNode = huffmanTree; /* back to top of tree */ } } /* clean up */ inFile = BitFileToFILE(bInFile); /* make file normal again */ FreeHuffmanTree(huffmanTree); /* free allocated memory */ return 0; }
/*************************************************************************** * Function : main * Description: This function demonstrates the usage of each of the bit * bit file functions. * Parameters : argc - the number command line arguments (not used) * Parameters : argv - array of command line arguments (not used) * Effects : Writes bit file, reads back results, printing them to * stdout. * Returned : EXIT_SUCCESS ***************************************************************************/ int main(int argc, char *argv[]) { bit_file_t *bfp; FILE *fp; int i, numCalls, value; if (argc < 2) { numCalls = NUM_CALLS; } else { numCalls = atoi(argv[1]); } /* create bit file for writing */ bfp = BitFileOpen("testfile", BF_WRITE); if (bfp == NULL) { perror("opening file"); return (EXIT_FAILURE); } /* write chars */ value = (int)'A'; for (i = 0; i < numCalls; i++) { printf("writing char %c\n", value); if(BitFilePutChar(value, bfp) == EOF) { perror("writing char"); if (0 != BitFileClose(bfp)) { perror("closing bitfile"); } return (EXIT_FAILURE); } value++; } /* write single bits */ value = 0; for (i = 0; i < numCalls; i++) { printf("writing bit %d\n", value); if(BitFilePutBit(value, bfp) == EOF) { perror("writing bit"); if (0 != BitFileClose(bfp)) { perror("closing bitfile"); } return (EXIT_FAILURE); } value = 1 - value; } /* write ints as bits */ value = 0x11111111; for (i = 0; i < numCalls; i++) { printf("writing bits %0X\n", (unsigned int)value); if(BitFilePutBits(bfp, &value, (unsigned int)(8 * sizeof(int))) == EOF) { perror("writing bits"); if (0 != BitFileClose(bfp)) { perror("closing bitfile"); } return (EXIT_FAILURE); } value += 0x11111111; } /* close bit file */ if (BitFileClose(bfp) != 0) { perror("closing file"); return (EXIT_FAILURE); } else { printf("closed file\n"); } /* reopen file for appending */ bfp = BitFileOpen("testfile", BF_APPEND); if (bfp == NULL) { perror("opening file"); return (EXIT_FAILURE); } /* append some chars */ value = (int)'A'; for (i = 0; i < numCalls; i++) { printf("appending char %c\n", value); if(BitFilePutChar(value, bfp) == EOF) { perror("appending char"); if (0 != BitFileClose(bfp)) { perror("closing bitfile"); } return (EXIT_FAILURE); } value++; } /* write some bits from an integer */ value = 0x111; for (i = 0; i < numCalls; i++) { printf("writing 12 bits from an integer %03X\n", (unsigned int)value); if(BitFilePutBitsInt(bfp, &value, 12, sizeof(value)) == EOF) { perror("writing bits from an integer"); if (0 != BitFileClose(bfp)) { perror("closing bitfile"); } return (EXIT_FAILURE); } value += 0x111; } /* convert to normal file */ fp = BitFileToFILE(bfp); if (fp == NULL) { perror("converting to stdio FILE"); return (EXIT_FAILURE); } else { printf("converted to stdio FILE\n"); } /* append some chars */ value = (int)'a'; for (i = 0; i < numCalls; i++) { printf("appending char %c\n", value); if(fputc(value, fp) == EOF) { perror("appending char to FILE"); if (fclose(fp) == EOF) { perror("closing stdio FILE"); } return (EXIT_FAILURE); } value++; } /* close file */ if (fclose(fp) == EOF) { perror("closing stdio FILE"); return (EXIT_FAILURE); } /* now read back writes */ /* open bit file */ bfp = BitFileOpen("testfile", BF_READ); if (bfp == NULL) { perror("reopening file"); return (EXIT_FAILURE); } /* read chars */ for (i = 0; i < numCalls; i++) { value = BitFileGetChar(bfp); if(value == EOF) { perror("reading char"); if (0 != BitFileClose(bfp)) { perror("closing bitfile"); } return (EXIT_FAILURE); } else { printf("read %c\n", value); } } /* read single bits */ for (i = 0; i < numCalls; i++) { value = BitFileGetBit(bfp); if(value == EOF) { perror("reading bit"); if (0 != BitFileClose(bfp)) { perror("closing bitfile"); } return (EXIT_FAILURE); } else { printf("read bit %d\n", value); } } /* read ints as bits */ for (i = 0; i < numCalls; i++) { if(BitFileGetBits(bfp, &value, (unsigned int)(8 * sizeof(int))) == EOF) { perror("reading bits"); if (0 != BitFileClose(bfp)) { perror("closing bitfile"); } return (EXIT_FAILURE); } else { printf("read bits %0X\n", (unsigned int)value); } } if (BitFileByteAlign(bfp) == EOF) { fprintf(stderr, "failed to align file\n"); if (0 != BitFileClose(bfp)) { perror("closing bitfile"); } return (EXIT_FAILURE); } else { printf("byte aligning file\n"); } /* read appended characters */ for (i = 0; i < numCalls; i++) { value = BitFileGetChar(bfp); if(value == EOF) { perror("reading char"); if (0 != BitFileClose(bfp)) { perror("closing bitfile"); } return (EXIT_FAILURE); } else { printf("read %c\n", value); } } /* read some bits into an integer */ for (i = 0; i < numCalls; i++) { value = 0; if(BitFileGetBitsInt(bfp, &value, 12, sizeof(value)) == EOF) { perror("reading bits from an integer"); if (0 != BitFileClose(bfp)) { perror("closing bitfile"); } return (EXIT_FAILURE); } else { printf("read 12 bits into an integer %03X\n", (unsigned int)value); } } /* convert to stdio FILE */ fp = BitFileToFILE(bfp); if (fp == NULL) { perror("converting to stdio FILE"); return (EXIT_FAILURE); } else { printf("converted to stdio FILE\n"); } /* read append some chars */ value = (int)'a'; for (i = 0; i < numCalls; i++) { value = fgetc(fp); if(value == EOF) { perror("stdio reading char"); if (0 != BitFileClose(bfp)) { perror("closing bitfile"); } return (EXIT_FAILURE); } else { printf("stdio read %c\n", value); } } /* close file */ if (fclose(fp) == EOF) { perror("closing stdio FILE"); return (EXIT_FAILURE); } return(EXIT_SUCCESS); }
int CHuffmanDecode(struct CHuffman *ch) { bit_file_t bfpIn; int i, newBit; bfpIn.mode = BF_READ; bfpIn.buf = ch->inBuf; bfpIn.bufLen = ch->inLen; BitFileInit(&bfpIn); /* open binary output file and bitfile input file */ /* decode input file */ ch->outIndex = 0; if (!ch->resume) { BitArrayClearAll(ch->code); ch->decode_length = 0; } while (1) { newBit = BitFileGetBit(&bfpIn); if (newBit == EOF) { fprintf(stderr, "error reading bitfile\n"); exit(1); } if (newBit == BUFFER_EMPTY) { ch->resume = 1; return BUFFER_EMPTY; } if (newBit != 0) { BitArraySetBit(ch->code, ch->decode_length); } ch->decode_length++; if (ch->lenIndex[ch->decode_length] != NUM_CHARS) { /* there are code of this length */ for(i = ch->lenIndex[ch->decode_length]; (i < NUM_CHARS) && (ch->canonicalList[i].codeLen == ch->decode_length); i++) { if (BitArrayCompare(ch->canonicalList[i].code, ch->code) == 0) { /* we just read a symbol output decoded value */ if (ch->canonicalList[i].value == EOF_CHAR) { if (BitFileByteAlign(&bfpIn)) { fprintf(stderr, "buffer full\n"); exit(1); } } else { if (ch->outIndex >= ch->outLen) { /* buffer limit reached */ fprintf(stderr, "buffer full\n"); exit(1); } ch->outBuf[ch->outIndex++] = ch->canonicalList[i].value; } BitArrayClearAll(ch->code); ch->decode_length = 0; break; } } } } ch->resume = 0; /* close all files */ BitFileClose(&bfpIn); return 0; }