PNG转换为C头的SDL [英] converting png to c header for sdl

查看:288
本文介绍了PNG转换为C头的SDL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想说,我已经搜查有关此主题的互联网,但它并不适用于我的情况。

I want to say, I have searched in the internet about this topic, but it doesn't apply to my situation.

我是做对于采用C游戏的修改和我编辑图像,以便用于游戏,但需要将图像转换为C头文件,以使他们的工作。本场比赛是多平台,构建适用于Windows和Android NDK通过

I was doing modifications for a game that uses C and I'm editing the images for use for the game, but the images needs to be converted to c headers to make them work. The game is multi-platform, with builds for Windows and Android via NDK.

其实我已经完成了一些编辑用的使用.C在SDL 用GIMP出口/ .H图像作为我为我的解决方案基础上,采用mbin2h,其中任何二进制文件转换成东西是适合C头。它的工作为Android打造,但不是主要的跨平台的C版本。

I've actually accomplished some of the editing with "Using .c/.h images exported with gimp in SDL" as my basis for my solution, using mbin2h, which converts any binary file into something that's fit for a C header. It worked for the Android build, but not for the main cross platform C build.

下面是一个例子code:

Here's an example code:

这是什么mbin2h通常输出:

This is what mbin2h outputs usually:

0x89, 'P', 'N', 'G', 0xD, 0xA, 0x1A, 0xA, 0x0, 0x0, 0x0, 0xD, 'I', 'H', 'D', 'R'

从原始源这里是:

Here's from the original source:

"\x89""PNG""\15\12\32\12\0\0\0\15""IHDR"

我不知道他们用什么程序将PNG转换为头文件。你们有些人指出,这是十六进制,ASCII字符和八进制值的组合。

I don't know what program they used to convert the PNG to the header file. As some of you pointed out, it's a combination of hex, ASCII characters and octal values.

现在的问题是如何将PNG转换为类似于原来的code头文件?为了方便他人弄清楚,我已经放在压缩文件中的原始图像,原始标题和mbin2h产生的标题:的图像problem.zip - 319.73

The question is how to convert the png to a header file similar to the original code? To make it easy for others to figure out, I've placed in the zip file the original image, the original header and the header generated by mbin2h: image problem.zip - 319.73

这实际上是OpenBOR,我只是想修改该菜单的形象,而是因为我不知道如何编程的很好,所以我将需要一些帮助,我们对此深感抱歉。

This is actually for OpenBOR and I only wanted to modify the menu image, but because I don't know how to program that well, so I'm going to need some help, sorry about that.

编辑:我没看到,有一个答案按钮,我把答案了。对不起球员。

I didn't see that there's an answer button, I'll put the answer up. Sorry guys.

推荐答案

二进制转换为C兼容的数据是不是的的困难,虽然我不知道为什么创建的结构是不必要的复杂。话又说回来,如果源$ C ​​$ C的其余部分预计这种特殊的格式,它只是复制原有的结构,并在正确的价值观充盈的情况。

Converting binary to C-compatible data is not that difficult, although I do wonder why the created structure is needlessly complex. Then again, if the rest of the source code expects this particular format, it's just a case of copying the original structure and filling in the right values.

..它是十六进制,ASCII字符和八进制值的组合。

.. it's a combination of hex, ASCII characters and octal values ..

这只会使源文件尽可能短。没有另一个原因;整个图像可以写出为 \\ X .. 六角codeS为好,甚至小数为此事。

That would only be to make the source file as short as possible. There is no further reason; the entire image could be written out as \x.. hex codes as well, or even decimals for that matter.

这是一个很好的额外的挑战,使头文件尽可能小。除了在原来的,我确信没有单一的行超过100个字符;不过,通过使用更多的最短的替代品,用于测试图像我的code只生产1626线这里原来有1921; 18%少!

It was a nice additional challenge to make the header file as small as possible. Other than in the original, I made sure no single line exceeds 100 characters; still, by using more 'shortest alternatives', for the test image my code only produces 1,626 lines where the original has 1,921; 18% less!

用法:PROGRAMNAME input.png

Usage: programName input.png

它将与 .H 更换输入文件的巴纽部分,写这篇输出到当前夹。工程于OSX,我想这应该编译其他的* nix类系统为好,当我试着限制自己我POSIX文件/ O。这是否或不包含Windows是留给读者做练习。

It will replace the .png part of the input file with .h and write this output to the current folder. Works on OSX, and I guess it should compile on other *nix-like systems as well, as I tried to limit myself to Posix file I/O. Whether this does or does not include Windows is left as "exercise for the reader".

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>

#define MAX_LINE_LENGTH 100

int main (int argc, char **argv)
{
    FILE *f_in, *f_out;
    char *name_ptr,*ptr;
    off_t i;
    struct stat st;
    char out_name[FILENAME_MAX];
    unsigned char png_byte, last_byte_octal, output_length;

    if (argc != 2)
    {
        fprintf (stderr, "this requires an input file\n");
        return -1;
    }

    f_in = fopen (argv[1], "rb");
    if (!f_in)
    {
        fprintf (stderr, "this requires the input file to exist\n");
        return -1;
    }

    if (stat(argv[1], &st))
    {
        fprintf (stderr, "this requires the input file to have no problems\n");
        return -1;
    }

    name_ptr = strrchr (argv[1], '/');      // *nix path
    if (!name_ptr)
        name_ptr = strrchr (argv[1], '\\'); // Wind*ws path
    if (name_ptr)
        name_ptr++;
    else
        name_ptr = argv[1];

    if (!*name_ptr || strlen(name_ptr) >= FILENAME_MAX)
    {
        fprintf (stderr, "this requires a reasonable length for the file name\n");
        return -1;
    }
    strcpy (out_name, name_ptr);

    ptr = out_name;
    while (*ptr)
    {
        *ptr = tolower(*ptr);
        ptr++;
    }

    ptr = strrchr (out_name, '.');
    if (*ptr)
        strcpy (ptr, ".h");
    else
        strcat (out_name, ".h");

    f_out = fopen (out_name, "w");
    if (!f_out)
    {
        fprintf (stderr, "this requires the output file to be created\n");
        return -1;
    }

    fprintf (stderr, "creating %s, please hold on.\n", out_name);


    ptr = out_name;
    while (*ptr)
    {
        if (*ptr == '.') *ptr = '_';
        ptr++;
    }

    for (i=0; i<2; i++)
    {
        fprintf (f_out, "#%s", !i ? "ifndef" : "define");
        fprintf (f_out, " _");
        ptr = out_name;
        while (*ptr)
        {
            fprintf (f_out, "%c", toupper(*ptr));
            ptr++;
        }
        fprintf (f_out, "_\n");
    }

    fprintf (f_out, "\n"
        "static const struct {\n"
        "\tsize_t size;\n"
        "\tunsigned char data [%lu];\n"
        "} ", (unsigned long)st.st_size);

    ptr = name_ptr;
    while (*ptr)
    {
        if (*ptr == '.')
            fprintf (f_out, "_");
        else
            fprintf (f_out, "%c", tolower(*ptr));
        ptr++;
    }

    fprintf (f_out, " = {\n"
        "\t%lu,\n\"", (unsigned long)st.st_size);

    last_byte_octal = 0;
    output_length = 1;
    for (i=0; i<st.st_size; i++)
    {
        png_byte = fgetc (f_in);
        if (png_byte == '\\')
        {
            output_length += 2;
            if (output_length >= MAX_LINE_LENGTH)
            {
                fprintf (f_out, "\"\n\"");
                output_length = 3;
            }
            fprintf (f_out, "\\\\");
            last_byte_octal = 0;
        }
        else if (png_byte == 9)
        {
            output_length += 2;
            if (output_length >= MAX_LINE_LENGTH)
            {
                fprintf (f_out, "\"\n\"");
                output_length = 3;
            }
            fprintf (f_out, "\\t");
            last_byte_octal = 0;
        } else if (png_byte < ' ' || png_byte == '\"')
        {
            output_length += (png_byte < 8) ? 2 : 3;
            last_byte_octal = 1;
            if (output_length >= MAX_LINE_LENGTH)
            {
                fprintf (f_out, "\"\n\"");
                output_length = (png_byte < 8) ? 3 : 4;
            }
            fprintf (f_out, "\\%o", png_byte);
        } else if (png_byte > '~')
        {
            output_length += 4;
            if (output_length >= MAX_LINE_LENGTH)
            {
                fprintf (f_out, "\"\n\"");
                output_length = 5;
            }
            fprintf (f_out, "\\x%X", png_byte);
            last_byte_octal = 1;
        } else
        {
            output_length += (last_byte_octal && isxdigit(png_byte)) ? 3 : 1;
            if (output_length >= MAX_LINE_LENGTH)
            {
                fprintf (f_out, "\"\n\"");
                output_length = 2;
                last_byte_octal = 0;
            }
            if (last_byte_octal && isxdigit(png_byte))
                fprintf (f_out, "\"\"");
            fprintf (f_out, "%c", png_byte);
            last_byte_octal = 0;
        }
    }
    fprintf (f_out, "\"\n};\n\n#endif\n");

    fclose (f_in);
    fclose (f_out);

    fprintf (stderr, "done.\n");

    return 0;
}

这篇关于PNG转换为C头的SDL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆