コード例 #1
0
ファイル: palette.c プロジェクト: elraro/SGDK
static int execute(char *info, FILE *fs, FILE *fh)
{
    char temp[MAX_PATH_LEN];
    char id[50];
    char fileIn[MAX_PATH_LEN];
    int ind, size;
    int w, h, bpp;
    int nbElem;
    short *palette;

    ind = 0;
    nbElem = sscanf(info, "%s %s \"%[^\"]\" %d", temp, id, temp, &ind);

    if (nbElem < 3)
    {
        printf("Wrong PALETTE definition\n");
        printf("PALETTE name file [index]\n");
        printf("  name\t\tPalette variable name\n");
        printf("  file\tpath of the .pal or image file to convert to Palette structure (should be a 8bpp .bmp or .png)\n");
        printf("  index\tstart index where to copy the palette in CRAM\n");

        return FALSE;
    }

    // adjust input file path
    adjustPath(resDir, temp, fileIn);

    // PAL file ?
    if (!strcasecmp(getFileExtension(fileIn), "pal"))
    {
        // get palette data
        palette = (unsigned short*) readFile(fileIn, &size);
        size /= 2;
    }
    else
    {
        // retrieve basic infos about the image
        if (!Img_getInfos(fileIn, &w, &h, &bpp)) return FALSE;

        // retrieve palette data
        palette = Img_getPalette(fileIn, &size);

        // special case where we use the image as palette
        if (h == 1)
        {
            int i, imgSize;
            unsigned short *tmpPalette;
            unsigned char *data;

            tmpPalette = malloc(size * 2);
            data = Img_getData(fileIn, &imgSize, 1, 1);

            // set temp palette from pixel value
            for(i = 0; i < w; i++)
                tmpPalette[i] = palette[data[i]];

            // then store in palette
            memcpy(palette, tmpPalette, size * 2);
        }
    }

    // EXPORT PALETTE
    outPalette(palette, 0, size, fs, fh, id, TRUE);

    return TRUE;
}
コード例 #2
0
ファイル: bitmap.c プロジェクト: ehaliewicz/SGDK
static int execute(char *info, FILE *fs, FILE *fh)
{
    char temp[MAX_PATH_LEN];
    char id[50];
    char fileIn[MAX_PATH_LEN];
    int w, h, bpp;
    int isize, psize;
    int packed;
    int transInd;
    int nbElem;
    unsigned char *data;
    unsigned short *palette;
    unsigned char maxIndex;

    packed = 0;
    transInd = 0;

    nbElem = sscanf(info, "%s %s \"%[^\"]\" %d %d", temp, id, temp, &packed, &transInd);

    if (nbElem < 3)
    {
        printf("Wrong BITMAP definition\n");
        printf("BITMAP name \"file\" [packed]\n");
        printf("  name\t\tBitmap variable name\n");
        printf("  file\tthe image to convert to Bitmap structure (should be a 8bpp .bmp or .png)\n");
        printf("  packed\tset to 1 to pack the Bitmap data, by default 0 is assumed.\n\n");

        return FALSE;
    }

    // adjust input file path
    adjustPath(resDir, temp, fileIn);

    // retrieve basic infos about the image
    if (!Img_getInfos(fileIn, &w, &h, &bpp)) return FALSE;

    // adjust bitmap size on pixel pair
    w = ((w + 1) / 2) * 2;

    // get image data (always 8bpp)
    data = Img_getData(fileIn, &isize, 2, 1);
    if (!data) return FALSE;

    // find max color index
    maxIndex = getMaxIndex(data, isize);
    // not allowed here
    if (maxIndex >= 16)
    {
        printf("Error: Image %s use color index >= 16\n", fileIn);
        printf("BITMAP resource require image with a maximum of 16 colors.\n");
        printf("Use 4bpp image instead if you are unsure.\n");
        return FALSE;
    }

    // convert to 4BPP
    data = to4bppAndFree(data, isize);
    isize /= 2;
    if (!data) return FALSE;

    // pack data
    if (packed)
    {
        data = pack(data, 0, isize, &isize, &packed);
        if (!data) return FALSE;
    }

    // get palette
    palette = Img_getPalette(fileIn, &psize);
    if (!palette) return FALSE;

    // we keep 16 colors max
    psize = MIN(16, psize);

    // EXPORT PALETTE
    strcpy(temp, id);
    strcat(temp, "_palette");
    outPalette(palette, 0, psize, fs, fh, temp, FALSE);

    // EXPORT BITMAP
    outBitmap(data, packed, w, h, isize, fs, fh, id, TRUE);

    return TRUE;
}