示例#1
0
// Open a BMP stream
static int openbmp(char *filename, char *packfilename)
{

#if DC || GP2X || SYMBIAN
    unsigned char mybmpheader[0x36];
#endif

    if((handle = openpackfile(filename, packfilename)) == -1)
    {
        return 0;
    }
#if DC || GP2X || SYMBIAN
    if(readpackfile(handle, &mybmpheader, 0x36) != 0x36)
    {
#else
    if(readpackfile(handle, &bmp_header, sizeof(s_bmpheader)) != sizeof(s_bmpheader))
    {
#endif
        closepackfile(handle);
        return 0;
    }

#if DC || GP2X || SYMBIAN
    build_bmp_header(&bmp_header, mybmpheader);
#else
    bmp_header.bm = SwapLSB16(bmp_header.bm);
    bmp_header.numplanes = SwapLSB16(bmp_header.numplanes);
    bmp_header.bpp = SwapLSB16(bmp_header.bpp);

    bmp_header.filesize = SwapLSB32(bmp_header.filesize);
    bmp_header.reserved = SwapLSB32(bmp_header.reserved);
    bmp_header.picstart = SwapLSB32(bmp_header.picstart);
    bmp_header.headersize = SwapLSB32(bmp_header.headersize);
    bmp_header.xsize = SwapLSB32(bmp_header.xsize);
    bmp_header.ysize = SwapLSB32(bmp_header.ysize);
    bmp_header.filesize = SwapLSB32(bmp_header.filesize);

    bmp_header.compression = SwapLSB32(bmp_header.compression);
    bmp_header.picsize = SwapLSB32(bmp_header.picsize);
    bmp_header.hres = SwapLSB32(bmp_header.hres);
    bmp_header.vres = SwapLSB32(bmp_header.vres);
    bmp_header.numcolors_used = SwapLSB32(bmp_header.numcolors_used);
    bmp_header.numcolors_important = SwapLSB32(bmp_header.numcolors_important);
#endif

    if(bmp_header.bm != 0x4D42 || bmp_header.bpp != 8 || bmp_header.compression)
    {
        closepackfile(handle);
        return 0;
    }
    res[0] = bmp_header.xsize;
    res[1] = bmp_header.ysize;
    return 1;
}

// Read data from the bitmap file
static int readbmp(unsigned char *buf, unsigned char *pal, int maxwidth, int maxheight)
{

    unsigned char *linebuffer;
    int y, s, d;
    int pb = PAL_BYTES;

    if(buf)
    {
        y = 0;
        while(y < maxheight && y < bmp_header.ysize)
        {
            linebuffer = buf + y * maxwidth;
            seekpackfile(handle, bmp_header.picstart + ((bmp_header.ysize - y - 1)*bmp_header.xsize), SEEK_SET);
            readpackfile(handle, linebuffer, bmp_header.xsize);
            ++y;
        }
    }

    if(pal && (linebuffer = (unsigned char *)malloc(1024)))
    {
        seekpackfile(handle, bmp_header.picstart - 1024, SEEK_SET);
        readpackfile(handle, linebuffer, 1024);
        if(pb == 512) // 16bit 565
        {
            for(s = 0, d = 0; s < 1024; s += 4, d += 2)
            {
                *(unsigned short *)(pal + d) = colour16(linebuffer[s + 2], linebuffer[s + 1], linebuffer[s]);
            }
        }
        else if(pb == 768) // 24bit palette, RGBA-BGR
        {
            for(s = 0, d = 0; s < 1024; s += 4, d += 3)
            {
                pal[d] = linebuffer[s + 2];
                pal[d + 1] = linebuffer[s + 1];
                pal[d + 2] = linebuffer[s];
            }
        }
        else if(pb == 1024)
        {
            for(s = 0, d = 0; s < 1024; s += 4, d += 4)
            {
                *(unsigned *)(pal + d) = colour32(linebuffer[s + 2], linebuffer[s + 1], linebuffer[s]);
            }
        }
        free(linebuffer);
        linebuffer = NULL;
    }

    return 1;
}
static int readpng(unsigned char *buf, unsigned char *pal, int maxwidth, int maxheight)
{
    int i, j, cw, ch;
    png_colorp png_pal_ptr = 0;
    int png_pal_num = 0;
    int pb = PAL_BYTES;

    cw = res[0] > maxwidth ? maxwidth : res[0];
    ch = res[1] > maxheight ? maxheight : res[1];
    if(buf)
    {
        for(i = 0; i < ch; i++)
        {
            memcpy(buf + (maxwidth * i), row_pointers[i], cw);
        }
    }
    if(pal)
    {
        if(png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY)
        {
            // set palette for grayscale images
            for(i = 0; i < 256; i++)
            {
                pal[i * 3] = pal[i * 3 + 1] = pal[i * 3 + 2] = i;
            }
            return 1;
        }
        else if(png_get_PLTE(png_ptr, info_ptr, &png_pal_ptr, &png_pal_num) != PNG_INFO_PLTE ||
                png_pal_ptr == NULL)
        {
            return 0;
        }

        png_pal_ptr[0].red = png_pal_ptr[0].green = png_pal_ptr[0].blue = 0;
        if(pb == 512) // 16bit 565
        {
            for(i = 0, j = 0; i < 512 && j < png_pal_num; i += 2, j++)
            {
                *(unsigned short *)(pal + i) = colour16(png_pal_ptr[j].red, png_pal_ptr[j].green, png_pal_ptr[j].blue);
            }
        }
        else if(pb == 768) // 24bit
        {
            for(i = 0; i < png_pal_num; i++)
            {
                pal[i * 3] = png_pal_ptr[i].red;
                pal[i * 3 + 1] = png_pal_ptr[i].green;
                pal[i * 3 + 2] = png_pal_ptr[i].blue;
            }
        }
        else if(pb == 1024) // 32bit
        {

            for(i = 0, j = 0; i < 1024 && j < png_pal_num; i += 4, j++)
            {
                *(unsigned *)(pal + i) = colour32(png_pal_ptr[j].red, png_pal_ptr[j].green, png_pal_ptr[j].blue);
            }
        }
    }
    return 1;
}