⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gifloader.cpp

📁 在MTK平台上开放的一个皮肤工具,可以支持jpg、bmp、png图片
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    stack_pointer = GIF_stack;
    buffer_pointer = buffer;
    buffer_count = xsize;
    while ((c = GIF_get_next_code(file)) != GIF_end_code)
    {
        if (c < 0)
        {
            return;
        }
        if (c == GIF_clear_code)
        {
            GIF_current_code_size = (S16) (size + 1);
            GIF_slot = GIF_new_codes;
            GIF_top_slot = (S16) (1 << GIF_current_code_size);
            while ((c = GIF_get_next_code(file)) == GIF_clear_code);
            if (c == GIF_end_code)
            {
                break;
            }
            if (c >= GIF_slot)
            {
                c = 0;
            }
            oc = fc = c;
            *buffer_pointer++ = (U8) c;
            if (--buffer_count == 0)
            {
                GIF_write_line(ix, iy, b, buffer, xsize, y, palette, transparent_GIF, transparent_color_index);
                if (interlaced)
                {
                    GIF_interlace_pass(&y, &pass, ysize);
                }
                else
                {
                    y++;
                }
                buffer_pointer = buffer;
                buffer_count = xsize;
            }
        }
        else
        {
            code = c;
            if (code >= GIF_slot)
            {
                code = oc;
                *stack_pointer++ = fc;
            }
            while (code >= GIF_new_codes)
            {
                *stack_pointer++ = GIF_suffix[code];
                code = GIF_prefix[code];
            }
            *stack_pointer++ = code;
            if (GIF_slot < GIF_top_slot)
            {
                GIF_suffix[GIF_slot] = fc = code;
                GIF_prefix[GIF_slot++] = oc;
                oc = c;
            }
            if (GIF_slot >= GIF_top_slot)
            {
                if (GIF_current_code_size < 12)
                {
                    GIF_top_slot <<= 1;
                    GIF_current_code_size++;
                }
            }
            while (stack_pointer > GIF_stack)
            {
                *buffer_pointer++ = (U8) * (--stack_pointer);
                if (--buffer_count == 0)
                {
                    GIF_write_line(ix, iy, b, buffer, xsize, y, palette, transparent_GIF, transparent_color_index);
                    if (interlaced)
                    {
                        GIF_interlace_pass(&y, &pass, ysize);
                    }
                    else
                    {
                        y++;
                    }
                    buffer_pointer = buffer;
                    buffer_count = xsize;
                }
            }
        }
    }
    if (buffer_count != xsize)
    {
        GIF_write_line(ix, iy, b, buffer, xsize, y, palette, transparent_GIF, transparent_color_index);
    }
    if (interlaced)
    {
        GIF_interlace_pass(&y, &pass, ysize);
    }
    else
    {
        y++;
    }
}


/*****************************************************************************
 * FUNCTION
 *  Load_GIF
 * DESCRIPTION
 *  
 * PARAMETERS
 *  file                [?]     
 *  total_frames        [?]     
 * RETURNS
 *  
 *****************************************************************************/
static U8 Load_GIF(bytestream *file, S32 *total_frames)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    S8 gif_signature[4];
    S8 descriptor_flags;
    S16 bits_per_pixel;
    S16 i, j, ncolors;
    S16 x, y, xsize, ysize;
    S8 interlace = 0;
    S8 gif_done;
    S16 screen_xsize, screen_ysize;
    U8 transparent_color_index = 0;
    U8 transparent_GIF = 0;
    U8 palette[256][3];
    S16 frame_counter = 0;
    S16 dest_row_bytes = 0;
    S32 bitmap_size;
    S32 first_frame_xsize = -1, first_frame_ysize = -1;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (file == NULL)
    {
        return (0);
    }
    /*   read the GIF file signature   */
    bytestream_fseek(file, 0, SEEK_SET);
    bytestream_fread((U8*) gif_signature, 1, 3, file);
    gif_signature[3] = '\0';
    if (strcmp((const char*)gif_signature, (const char*)"GIF"))
    {
        return (0);
    }
    bytestream_fseek(file, 3, SEEK_CUR);
    screen_xsize = bytestream_fgetword(file);
    screen_ysize = bytestream_fgetword(file);
    printf("Screen Width=%d, Screen Height=%d\n", screen_xsize, screen_ysize);
    descriptor_flags = bytestream_fgetbyte(file);
    bytestream_fseek(file, 2, SEEK_CUR);
    if (descriptor_flags & 0x80)
    {
        bits_per_pixel = (S16) ((descriptor_flags & 0x07) + 1);
        ncolors = (S16) (1 << bits_per_pixel);
        /*  Read the global color palette */
        for (i = 0; i < ncolors; i++)
        {
            for (j = 0; j < 3; j++)
            {
                palette[i][j] = bytestream_fgetbyte(file);
            }
        }
    }
    gif_done = 0;
    do
    {
        char block_type = bytestream_fgetbyte(file);

        switch (block_type)
        {
            case '!':   /* Process the GIF extension block  */
            {
                S32 block_size;
                S32 i;
                U8 label = bytestream_fgetbyte(file);
                U8 flags, color_index;

                if (label == 0xf9)
                {
                    bytestream_fgetbyte(file);
                    flags = bytestream_fgetbyte(file);
                    bytestream_fgetbyte(file);
                    bytestream_fgetbyte(file);
                    color_index = bytestream_fgetbyte(file);
                    if (flags & 0x01)
                    {
                        transparent_GIF = 1;
                        transparent_color_index = color_index;
                        printf("The image is has transparent color:");
                        printf(
                            "(%d,%d,%d)\n",
                            palette[color_index][0],
                            palette[color_index][1],
                            palette[color_index][2]);
                    }
                }
                else
                {
                    while ((block_size = bytestream_fgetbyte(file)) != 0)
                    {
                        for (i = 0; i < block_size; i++)
                        {
                            bytestream_fgetbyte(file);
                        }
                    }
                }
            }
                break;
            case ',':   /* Process the GIF image   */
            {
                x = bytestream_fgetword(file);
                y = bytestream_fgetword(file);
                xsize = bytestream_fgetword(file);
                ysize = bytestream_fgetword(file);
                descriptor_flags = bytestream_fgetbyte(file);
                if (descriptor_flags & 0x80)
                {
                    bits_per_pixel = (S16) ((descriptor_flags & 0x07) + 1);
                    ncolors = (S16) (1 << bits_per_pixel);
                    /* Read the local color palette  */
                    for (i = 0; i < ncolors; i++)
                    {
                        for (j = 0; j < 3; j++)
                        {
                            palette[i][j] = bytestream_fgetbyte(file);
                        }
                    }
                    printf("number of color:%d\n", ncolors);
                }
                if (descriptor_flags & 0x40)
                {
                    interlace = 1;
                }
                if (first_frame_xsize < 0)
                {
                    first_frame_xsize = xsize;
                    first_frame_ysize = ysize;
                    bitmap_size = dest_row_bytes * ysize;
                }
                //GIF_decoder(x,y,file,b,xsize,ysize,interlace,palette,transparent_GIF,transparent_color_index);
                //if((frame_counter>=frame_number) && (b!=NULL)) gif_done=1;
                frame_counter++;
            }
                break;
            case '\0':
                if (bytestream_feof(file))
                {
                    gif_done = 1;
                }
                break;
            case ';':
                gif_done = 1;
                break;
        }
    } while (!gif_done);
    *total_frames = frame_counter;
    printf("Number of frames:%d\n", frame_counter);
    return (1);
}

/*****************************************************************************
 * FUNCTION
 *  GIFLoader
 * DESCRIPTION
 *  
 * PARAMETERS
 *  in_filename         [?]     
 *  out_filename        [?]     
 * RETURNS
 *  
 *****************************************************************************/

/*-----------------------------------------------------------
* FUNCTION NAME: GIFLoader
* DESCRIPTION:
*    			get gif image data include mtk file header
* ARGUMENTS:
*			    char *in_filename:the file name include path
*         U8 *out_data:outpur data
*         U32 *data_len:output data length
* Return:
*    		gif file frame counter
* Note:
*    
*------------------------------------------------------------*/
U8 GIFLoader(char *in_filename, U8 *out_data, U32 *data_len)
{
    FILE *infile;
    U32 width, height,size;
    U32 width_height,frame_counter;
    mtk_file_header_t file_h;
    U8 header_len;

    frame_counter = Image_Get_Dimensions(in_filename, &width, &height);
    
    infile = fopen(in_filename, "rb");
    if (infile == NULL)
    {
        printf("\nError opening %s", in_filename);
        return 0;
    }
    
    fseek(infile, 0, SEEK_END);
    size = ftell(infile);
    fseek(infile, 0, SEEK_SET);

    if ((size >> 24) > 0)
    {
        printf("image is too big:%s\n", in_filename);
        return 0;
    }
    header_len = sizeof(file_h);
    fread(&out_data[header_len], 1, size, infile);
    fclose(infile);

    width_height = (((U32)width & 0XFFF) << 12) | ((U32)height & 0XFFF);

    file_h.file_type = IMAGE_TYPE_GIF;
    file_h.frame_count = (U8)frame_counter;
    //image data len
    file_h.file_len_low = (U8)(size & 0x000000ff);
    file_h.file_len_mid = (U8)((size & 0x0000ff00) >> 8);
    file_h.file_len_high = (U8)((size & 0x00ff0000) >> 16);
    //image width and height
    file_h.width_height_low = (U8)(width_height & 0x000000ff);
    file_h.width_height_mid = (U8)((width_height & 0x0000ff00) >> 8);
    file_h.width_height_high = (U8)((width_height & 0x00ff0000) >> 16);

    memcpy(out_data, &file_h, header_len);
    *data_len = size + header_len;
    return (U8)frame_counter;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -