/*
** Read command line params, call search
*/
int
main(int argc, char *argv[]) {

    if (argc != 4) {
        usage(argv[0]);
        return(-1);
    }

    // plotter is not really a "grid" but why not use the read routine
    // to save coding!
    grid_t *sonar   = readGrid(strcmp(argv[1],"-")?fopen(argv[1],"r"):stdin, TRUE);
    grid_t *actual  = readGrid(strcmp(argv[2],"-")?fopen(argv[2],"r"):stdin, TRUE);
    grid_t *plotter = readGrid(strcmp(argv[3],"-")?fopen(argv[3],"r"):stdin, FALSE);

    assert(sonar);
    assert(actual);
    assert(plotter);

    // create output file
    char i = argv[2][6];
    char output_file_name[] = "actual1.txt.bit";
    output_file_name[6] = i;

    bit_file_t *bitfile = BitFileOpen(output_file_name, BF_WRITE);
    assert(bitfile);

    uint32_t bits_sent=0, moves_made=0;
    data_t current_position = plotter->data[0];
    for(int i = 0 ; i < plotter->width * plotter->height ; i++) {
        data_t new_position = plotter->data[i];
        moves_made += gridDistance(plotter, current_position, new_position);
        current_position = new_position;

        //store the PBF of difference between sonar reading and actual height in tempArray
        char tempArray[PBFLIMIT];
        int inaccuracy = sonar->data[current_position] - actual->data[current_position];
        int numBits, i;
        numBits = intToPBF(inaccuracy, tempArray, PBFLIMIT);

        //put the PBF into output file
        for (i = 0; i < numBits; i++) {
            BitFilePutBit((int)tempArray[i] - ASCII_ZERO, bitfile);
        }

        bits_sent += numBits;
    }

    assert(!BitFileClose(bitfile));

    printf("M: %10d B: %10d Bat: %10.1f\n",
           moves_made, bits_sent,
           (float)moves_made/(float)MOVES_PER_BATTERY + (float)bits_sent);

    return 0;
}
/*
** 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;
}
Exemplo n.º 3
0
Arquivo: main.c Projeto: mpetri/aazip
/*
 * aazip - compress files using a transform based compression system
 */
int main(int argc, char** argv)
{
    FILE* f;
    bit_file_t* of;
    char* infile,*outfile;
    uint8_t* input,*lupdate,*bwt,lumode;
    int32_t I,osize,opt;
    uint32_t size;
    mode_t lupdate_alg;
    float ient,oent;
    uint64_t cost,tstart,tstop,elapsed;

    /* parse command line parameter */
    opt = GETOPT_FINISHED;
    if (argc <= 1) {
        print_usage(argv[0]);
        exit(EXIT_FAILURE);
    }
    while ((opt = getopt(argc, argv, "m:h")) != GETOPT_FINISHED) {
        switch (opt) {
            case 'm':
                if (strcmp(optarg, "simple") == 0) lupdate_alg = SIMPLE;
                else if (strcmp(optarg, "mtf") == 0) lupdate_alg = MTF;
                else if (strcmp(optarg, "fc") == 0) lupdate_alg = FC;
                else if (strcmp(optarg, "wfc") == 0) lupdate_alg = WFC;
                else if (strcmp(optarg, "timestamp") == 0) lupdate_alg = TS;
                else fatal("ERROR: mode <%s> unknown!\n", optarg);
                break;
            case 'h':
            default:
                print_usage(argv[0]);
                exit(EXIT_FAILURE);
        }
    }
    /* read input file name */
    if (optind < argc) infile = argv[optind];
    else {
        print_usage(argv[0]);
        exit(EXIT_FAILURE);
    }

    /* read input file */
    f = safe_fopen(infile,"r");
    size = safe_filesize(f);
    input = (uint8_t*) safe_malloc(size+1);
    if (fread(input,1,size,f)!=(size_t)size) {
        fatal("read input file.");
    }
    safe_fclose(f);
    input[size] = 0;

    /* TODO calculate input entropy */
    ient = 0.0f;

    /* perform bwt */
    bwt = (uint8_t*) safe_malloc(size);

    tstart = gettime();

    bwt = transform_bwt(input,size,bwt,&I);

    /* peform list update */
    switch (lupdate_alg) {
        case SIMPLE:
            fprintf(stdout,"ALGORITHM: simple\n");
            lupdate = lupdate_simple(bwt,size,input,&cost);
            break;
        case MTF:
            fprintf(stdout,"ALGORITHM: move to front\n");
            lupdate = lupdate_movetofront(bwt,size,input,&cost);
            break;
        case FC:
            fprintf(stdout,"ALGORITHM: frequency count\n");
            lupdate = lupdate_freqcount(bwt,size,input,&cost);
            break;
        case WFC:
            fprintf(stdout,"ALGORITHM: weighted frequency count\n");
            lupdate = lupdate_wfc(bwt,size,input,&cost);
            break;
        case TS:
            fprintf(stdout,"ALGORITHM: timestamp\n");
            lupdate = lupdate_timestamp(bwt,size,input,&cost);
            break;
        default:
            fatal("unkown list update algorithm.");
    }

    fprintf(stdout,"INPUT: %s (%d bytes)\n",infile,size);
    fprintf(stdout,"COST: %lu\n",cost);

    /* TODO calculate entropy after list update*/
    oent = 0.0f;

    /* write output */
    outfile = safe_strcat(infile,".aazip");
    /* create bit file for writing */
    of = BitFileOpen(outfile, BF_WRITE);

    /* write aa zip header */
    BitFilePutChar('A', of);
    BitFilePutChar('A', of);

    /* write I */
    BitFilePutBitsInt(of,&I,32,sizeof(uint32_t));

    /* write lupdate mode */
    lumode = lupdate_alg;
    BitFilePutBitsInt(of,&lumode,8,sizeof(uint8_t));


    fprintf(stderr,"I %d lumode %d\n",I,lumode);

    /* perform huffman coding */
    encode_huffman(lupdate,size,of);

    tstop = gettime();

    elapsed = tstop - tstart;
    fprintf(stdout,"TIME: %.3f s\n",(float)elapsed/1000000);

    /* flush and get file stats */
    BitFileFlushOutput(of,0);
    f = BitFileToFILE(of);
    osize = ftell(f);

    fprintf(stdout,"OUTPUT: %s\n",outfile);
    fprintf(stdout,"ENTROPY: %.2f bps / %.2f bps\n",ient,oent);
    fprintf(stdout,"COMPRESSION: %.2f\n",((float)osize/(float)size)*100);

    /* clean up*/
    safe_fclose(f);
    free(input);
    free(bwt);

    return (EXIT_SUCCESS);
}
Exemplo n.º 4
0
/***************************************************************************
*   Function   : LZWDecodeFile
*   Description: This routine reads an input file 1 encoded string at a
*                time and decodes it using the LZW algorithm.
*   Parameters : inFile - Name of file to decode
*                outFile - Name of file to write decoded output to
*   Effects    : File is decoded using the LZW algorithm with CODE_LEN
*                codes.
*   Returned   : TRUE for success, otherwise FALSE.
***************************************************************************/
int LZWDecodeFile(char *inFile, char *outFile)
{
    bit_file_t *bfpIn;                  /* encoded input */
    FILE *fpOut;                        /* decoded output */

    unsigned int nextCode;              /* value of next code */
    unsigned int lastCode;              /* last decoded code word */
    unsigned int code;                  /* code word to decode */
    unsigned char c;                    /* last decoded character */

    /* open input and output files */
    if (NULL == (bfpIn = BitFileOpen(inFile, BF_READ)))
    {
        perror(inFile);
        return FALSE;
    }

    if (NULL == outFile)
    {
        fpOut = stdout;
    }
    else
    {
        if (NULL == (fpOut = fopen(outFile, "wb")))
        {
            BitFileClose(bfpIn);
            perror(outFile);
            return FALSE;
        }
    }

    /* start with 9 bit code words */
    currentCodeLen = 9;

    /* initialize for decoding */
    nextCode = FIRST_CODE;  /* code for next (first) string */

    /* first code from file must be a character.  use it for initial values */
    lastCode = GetCodeWord(bfpIn);
    c = lastCode;
    fputc(lastCode, fpOut);

    /* decode rest of file */
    while ((code = GetCodeWord(bfpIn)) != EOF)
    {

        /* look for code length increase marker */
        if (((CURRENT_MAX_CODES(currentCodeLen) - 1) == code) &&
            (currentCodeLen < MAX_CODE_LEN))
        {
            currentCodeLen++;
            code = GetCodeWord(bfpIn);
        }

        if (code < nextCode)
        {
            /* we have a known code.  decode it */
            c = DecodeRecursive(code, fpOut);
        }
        else
        {
            /***************************************************************
            * We got a code that's not in our dictionary.  This must be due
            * to the string + char + string + char + string exception.
            * Build the decoded string using the last character + the
            * string from the last code.
            ***************************************************************/
            unsigned char tmp;

            tmp = c;
            c = DecodeRecursive(lastCode, fpOut);
            fputc(tmp, fpOut);
        }

        /* if room, add new code to the dictionary */
        if (nextCode < MAX_CODES)
        {
            dictionary[nextCode - FIRST_CODE].prefixCode = lastCode;
            dictionary[nextCode - FIRST_CODE].suffixChar = c;
            nextCode++;
        }

        /* save character and code for use in unknown code word case */
        lastCode = code;
    }

    fclose(fpOut);
    BitFileClose(bfpIn);
    return TRUE;
}
Exemplo n.º 5
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);
}
Exemplo n.º 6
0
int main(int argc, char** argv)
{
    bit_file_t* f;
    FILE* outf;
    char* infile,*outfile;
    uint8_t* input,*bwt,*output,lumode;
    int32_t I,n;
    mode_t lupdate_alg;

    /* parse command line parameter */
    if (argc !=2) {
        print_usage(argv[0]);
        exit(EXIT_FAILURE);
    }

    infile = argv[1];

    /* read input file */
    f = BitFileOpen(infile, BF_READ);

    if (!f) {
        fatal("could not open file %s.",infile);
    }

    /* check if compressed with aazip */
    if (BitFileGetChar(f) != 'A' || BitFileGetChar(f) != 'A') {
        fatal("file %s not compressed with aazip.",infile);
    }

    fprintf(stdout,"FILE: %s\n",infile);

    /* read header */
    BitFileGetBitsInt(f,&I,32,sizeof(I));
    BitFileGetBitsInt(f,&lumode,8,sizeof(lumode));
    lupdate_alg = lumode;

    input = decode_huffman(f,&n);

    /* malloc output memory */
    bwt = safe_malloc(n*sizeof(uint8_t));

    /* peform list update */
    switch (lupdate_alg) {
        case SIMPLE:
            fprintf(stdout,"LUPDATE: Simple\n");
            bwt = lupdate_simple(input,n,bwt);
            break;
        case MTF:
            fprintf(stdout,"LUPDATE: Move-To-Front\n");
            bwt = lupdate_movetofront(input,n,bwt);
            break;
        case FC:
            fprintf(stdout,"LUPDATE: FC\n");
            bwt = lupdate_freqcount(input,n,bwt);
            break;
        case WFC:
            fprintf(stdout,"LUPDATE: WFC\n");
            bwt = lupdate_wfc(input,n,bwt);
            break;
        case TS:
            fprintf(stdout,"LUPDATE: TS\n");
            bwt = lupdate_timestamp(input,n,bwt);
            break;
        default:
            fatal("unkown list update algorithm.");
    }

    /* reverse bwt */
    output = reverse_bwt(bwt,n,I,input);

    /* write output */
    outfile = safe_strcat(infile,".org");
    outf = safe_fopen(outfile,"w");
    if (fwrite(output,sizeof(uint8_t),n,outf)!= (size_t)n) {
        fatal("error writing output.");
    }
    safe_fclose(outf);


    /* clean up */
    free(bwt);
    free(input);
    BitFileClose(f);

    return (EXIT_SUCCESS);
}