Beispiel #1
0
Bitmap* ReadPCXFile (const Collection* A)
/* Read a bitmap from a PCX file */
{
    PCXHeader* P;
    Bitmap* B;
    unsigned char* L;
    Pixel* Px;
    unsigned MaxIdx = 0;
    unsigned X, Y;


    /* Get the file name */
    const char* Name = NeedAttrVal (A, "name", "read pcx file");

    /* Open the file */
    FILE* F = fopen (Name, "rb");
    if (F == 0) {
        Error ("Cannot open PCX file `%s': %s", Name, strerror (errno));
    }

    /* Read the PCX header */
    P = ReadPCXHeader (F, Name);

    /* Dump the header if requested */
    if (Verbosity > 0) {
        DumpPCXHeader (P, Name);
    }

    /* Create the bitmap */
    B = NewBitmap (P->Width, P->Height);

    /* Copy the name */
    SB_CopyStr (&B->Name, Name);

    /* Allocate memory for the scan line */
    L = xmalloc (P->Width);

    /* Read the pixel data */
    Px = B->Data;
    if (P->Planes == 1) {

        /* This is either monochrome or indexed */
        if (P->BPP == 1) {
            /* Monochrome */
            for (Y = 0, Px = B->Data; Y < P->Height; ++Y) {

                unsigned I;
                unsigned char Mask;

                /* Read the plane */
                ReadPlane (F, P, L);

                /* Create pixels */
                for (X = 0, I = 0, Mask = 0x01; X < P->Width; ++Px) {
                    Px->Index = (L[I] & Mask) != 0;
                    if (Mask == 0x80) {
                        Mask = 0x01;
                        ++I;
                    } else {
                        Mask <<= 1;
                    }
                }

            }
        } else {
            /* One plane with 8bpp is indexed */
            for (Y = 0, Px = B->Data; Y < P->Height; ++Y) {

                /* Read the plane */
                ReadPlane (F, P, L);

                /* Create pixels */
                for (X = 0; X < P->Width; ++X, ++Px) {
                    if (L[X] > MaxIdx) {
                        MaxIdx = L[X];
                    }
                    Px->Index = L[X];
                }
            }
        }

        /* One plane means we have a palette which is either part of the header
        ** or follows.
        */
        if (P->PalInfo == 0) {

            /* Create the monochrome palette */
            B->Pal = NewMonochromePalette ();

        } else {

            unsigned      Count;
            unsigned      I;
            unsigned char Palette[256][3];
            unsigned long EndPos;

            /* Determine the current file position */
            unsigned long CurPos = FileGetPos (F);

            /* Seek to the end of the file */
            (void) fseek (F, 0, SEEK_END);

            /* Get this position */
            EndPos = FileGetPos (F);

            /* There's a palette if the old location is 769 bytes from the end */
            if (EndPos - CurPos == sizeof (Palette) + 1) {

                /* Seek back */
                FileSetPos (F, CurPos);

                /* Check for palette marker */
                if (Read8 (F) != 0x0C) {
                    Error ("Invalid palette marker in PCX file `%s'", Name);
                }

            } else if (EndPos == CurPos) {

                /* The palette is in the header */
                FileSetPos (F, 16);

                /* Check the maximum index for safety */
                if (MaxIdx > 15) {
                    Error ("PCX file `%s' contains more than 16 indexed colors "
                           "but no extra palette", Name);
                }

            } else {
                Error ("Error in PCX file `%s': %lu bytes at end of pixel data",
                       Name, EndPos - CurPos);
            }

            /* Read the palette. We will just read what we need. */
            Count = MaxIdx + 1;
            ReadData (F, Palette, Count * sizeof (Palette[0]));

            /* Create the palette from the data */
            B->Pal = NewPalette (Count);
            for (I = 0; I < Count; ++I) {
                B->Pal->Entries[I].R = Palette[I][0];
                B->Pal->Entries[I].G = Palette[I][1];
                B->Pal->Entries[I].B = Palette[I][2];
                B->Pal->Entries[I].A = 0;
            }

        }

    } else {

        /* 3 or 4 planes are RGB or RGBA (don't know if this exists) */
        for (Y = 0, Px = B->Data; Y < P->Height; ++Y) {

            /* Read the R plane and move the data */
            ReadPlane (F, P, L);
            for (X = 0; X < P->Width; ++X, ++Px) {
                Px->C.R = L[X];
            }

            /* Read the G plane and move the data */
            ReadPlane (F, P, L);
            for (X = 0; X < P->Width; ++X, ++Px) {
                Px->C.G = L[X];
            }

            /* Read the B plane and move the data */
            ReadPlane (F, P, L);
            for (X = 0; X < P->Width; ++X, ++Px) {
                Px->C.B = L[X];
            }

            /* Either read the A plane or clear it */
            if (P->Planes == 4) {
                ReadPlane (F, P, L);
                for (X = 0; X < P->Width; ++X, ++Px) {
                    Px->C.A = L[X];
                }
            } else {
                for (X = 0; X < P->Width; ++X, ++Px) {
                    Px->C.A = 0;
                }
            }
        }
    }

    /* Close the file */
    fclose (F);

    /* Free memory for the scan line */
    xfree (L);

    /* Free the PCX header */
    FreePCXHeader (P);

    /* Return the bitmap */
    return B;
}
void main(int argc, char * argv[])
{
    FILE *f;
    FILE *fKey;
    FILE *fTrad;
    FILE *fIndex;

    long    nKey, nTrad, total;
    long    maxlen = 0;     // Just to know how long is the longest traslation

    char * buf = new char [16384];
    TIndex * index = new TIndex [MAXINDEX];

    for (total=0; total < MAXINDEX; total++)
    {
        index[total].Key  = -1;
        index[total].Trad = -1;
    }

    if (argc < 2)
    {
        printf("Dictionary Parser - (C)1998 by Mirko Buffoni\n\n");
        printf("This program create a file of keywords (.key), a file of translations (.dct)\nand an index file (.idx), starting from an input text file.\n\n");
        printf("USAGE:  MkDict.exe  <filename>\n");
        exit(-1);
    }

    printf("\nParsing file:  %s\n\n", argv[1]);
    f = fopen(argv[1], "r");

    char keywordsFilename[512];
    char dataFilename[512];
    char indexFilename[512];

    strcpy(keywordsFilename, argv[1]);
    strcpy(dataFilename, argv[1]);
    strcpy(indexFilename, argv[1]);

    StripExtension(keywordsFilename);
    StripExtension(dataFilename);
    StripExtension(indexFilename);

    strcat(keywordsFilename, ".key");
    strcat(dataFilename, ".dct");
    strcat(indexFilename, ".idx");

    fKey   = fopen(keywordsFilename, "wb");
    fTrad  = fopen(dataFilename, "wb");
    fIndex = fopen(indexFilename, "wb");

    nKey = 0;
    nTrad = 0;
    total = 0;

    while (f && !feof(f))
    {
        memset(buf,0,8192);
        fgets(buf, 8192, f);
        if (strlen(buf) > 0) buf[strlen(buf)-1] = 0;
        if (strlen(buf) > maxlen)
            maxlen = strlen(buf);

        buf = minstring(buf);

        if (strlen(buf) > 0)    // Skip empty lines
        {
            char *p = buf;
            p = strtok(buf," ");
            if (p)              // If there is no space, it's probably an error :)
            {
                if (index[total].Key == -1)
                {
                    index[total].Key = FileGetPos(fKey);
                    index[total].Trad = FileGetPos(fTrad);
                }

//              printf("%s -> ", p);
                fwrite(p, 1, strlen(p)+1, fKey);
                p += strlen(p)+1;
//              printf("%s\n", p);
                fwrite(p, 1, strlen(p)+1, fTrad);
                total++;
                printf("\x0dProc. terms: %d", total);


            }
        }

    }

    printf("\nTotal number of keywords in dictionary:  %d\n", total);
    fwrite(index,sizeof(TIndex),total,fIndex);

    fclose(fIndex);
    fclose(fTrad);
    fclose(fKey);

    delete [] index;
    delete [] buf;
}