xpmdecod.cpp

来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 795 行 · 第 1/2 页

CPP
795
字号
    {"gray82", myRGB(209, 209, 209)},
    {"gray83", myRGB(212, 212, 212)},
    {"gray84", myRGB(214, 214, 214)},
    {"gray85", myRGB(217, 217, 217)},
    {"gray86", myRGB(219, 219, 219)},
    {"gray87", myRGB(222, 222, 222)},
    {"gray88", myRGB(224, 224, 224)},
    {"gray89", myRGB(227, 227, 227)},
    {"gray9", myRGB(23, 23, 23)},
    {"gray90", myRGB(229, 229, 229)},
    {"gray91", myRGB(232, 232, 232)},
    {"gray92", myRGB(235, 235, 235)},
    {"gray93", myRGB(237, 237, 237)},
    {"gray94", myRGB(240, 240, 240)},
    {"gray95", myRGB(242, 242, 242)},
    {"gray96", myRGB(245, 245, 245)},
    {"gray97", myRGB(247, 247, 247)},
    {"gray98", myRGB(250, 250, 250)},
    {"gray99", myRGB(252, 252, 252)},
    {"green", myRGB(0, 255, 0)},
    {"greenyellow", myRGB(173, 255, 47)},
    {"honeydew", myRGB(240, 255, 240)},
    {"hotpink", myRGB(255, 105, 180)},
    {"indianred", myRGB(107, 57, 57)},
    {"ivory", myRGB(255, 255, 240)},
    {"khaki", myRGB(179, 179, 126)},
    {"lavender", myRGB(230, 230, 250)},
    {"lavenderblush", myRGB(255, 240, 245)},
    {"lawngreen", myRGB(124, 252, 0)},
    {"lemonchiffon", myRGB(255, 250, 205)},
    {"lightblue", myRGB(176, 226, 255)},
    {"lightcoral", myRGB(240, 128, 128)},
    {"lightcyan", myRGB(224, 255, 255)},
    {"lightgoldenrod", myRGB(238, 221, 130)},
    {"lightgoldenrodyellow", myRGB(250, 250, 210)},
    {"lightgray", myRGB(168, 168, 168)},
    {"lightpink", myRGB(255, 182, 193)},
    {"lightsalmon", myRGB(255, 160, 122)},
    {"lightseagreen", myRGB(32, 178, 170)},
    {"lightskyblue", myRGB(135, 206, 250)},
    {"lightslateblue", myRGB(132, 112, 255)},
    {"lightslategray", myRGB(119, 136, 153)},
    {"lightsteelblue", myRGB(124, 152, 211)},
    {"lightyellow", myRGB(255, 255, 224)},
    {"limegreen", myRGB(0, 175, 20)},
    {"linen", myRGB(250, 240, 230)},
    {"magenta", myRGB(255, 0, 255)},
    {"maroon", myRGB(143, 0, 82)},
    {"mediumaquamarine", myRGB(0, 147, 143)},
    {"mediumblue", myRGB(50, 50, 204)},
    {"mediumforestgreen", myRGB(50, 129, 75)},
    {"mediumgoldenrod", myRGB(209, 193, 102)},
    {"mediumorchid", myRGB(189, 82, 189)},
    {"mediumpurple", myRGB(147, 112, 219)},
    {"mediumseagreen", myRGB(52, 119, 102)},
    {"mediumslateblue", myRGB(106, 106, 141)},
    {"mediumspringgreen", myRGB(35, 142, 35)},
    {"mediumturquoise", myRGB(0, 210, 210)},
    {"mediumvioletred", myRGB(213, 32, 121)},
    {"midnightblue", myRGB(47, 47, 100)},
    {"mintcream", myRGB(245, 255, 250)},
    {"mistyrose", myRGB(255, 228, 225)},
    {"moccasin", myRGB(255, 228, 181)},
    {"navajowhite", myRGB(255, 222, 173)},
    {"navy", myRGB(35, 35, 117)},
    {"navyblue", myRGB(35, 35, 117)},
    {"oldlace", myRGB(253, 245, 230)},
    {"olivedrab", myRGB(107, 142, 35)},
    {"orange", myRGB(255, 135, 0)},
    {"orangered", myRGB(255, 69, 0)},
    {"orchid", myRGB(239, 132, 239)},
    {"palegoldenrod", myRGB(238, 232, 170)},
    {"palegreen", myRGB(115, 222, 120)},
    {"paleturquoise", myRGB(175, 238, 238)},
    {"palevioletred", myRGB(219, 112, 147)},
    {"papayawhip", myRGB(255, 239, 213)},
    {"peachpuff", myRGB(255, 218, 185)},
    {"peru", myRGB(205, 133, 63)},
    {"pink", myRGB(255, 181, 197)},
    {"plum", myRGB(197, 72, 155)},
    {"powderblue", myRGB(176, 224, 230)},
    {"purple", myRGB(160, 32, 240)},
    {"red", myRGB(255, 0, 0)},
    {"rosybrown", myRGB(188, 143, 143)},
    {"royalblue", myRGB(65, 105, 225)},
    {"saddlebrown", myRGB(139, 69, 19)},
    {"salmon", myRGB(233, 150, 122)},
    {"sandybrown", myRGB(244, 164, 96)},
    {"seagreen", myRGB(82, 149, 132)},
    {"seashell", myRGB(255, 245, 238)},
    {"sienna", myRGB(150, 82, 45)},
    {"silver", myRGB(192, 192, 192)},
    {"skyblue", myRGB(114, 159, 255)},
    {"slateblue", myRGB(126, 136, 171)},
    {"slategray", myRGB(112, 128, 144)},
    {"snow", myRGB(255, 250, 250)},
    {"springgreen", myRGB(65, 172, 65)},
    {"steelblue", myRGB(84, 112, 170)},
    {"tan", myRGB(222, 184, 135)},
    {"thistle", myRGB(216, 191, 216)},
    {"tomato", myRGB(255, 99, 71)},
    {"transparent", myRGB(0, 0, 1)},
    {"turquoise", myRGB(25, 204, 223)},
    {"violet", myRGB(156, 62, 206)},
    {"violetred", myRGB(243, 62, 150)},
    {"wheat", myRGB(245, 222, 179)},
    {"white", myRGB(255, 255, 255)},
    {"whitesmoke", myRGB(245, 245, 245)},
    {"yellow", myRGB(255, 255, 0)},
    {"yellowgreen", myRGB(50, 216, 56)},
    {NULL, myRGB(0, 0, 0)}
};
static int numTheRGBRecords = 235;

static unsigned char ParseHexadecimal(char digit1, char digit2)
{
    unsigned char i1, i2;

    if (digit1 >= 'a')
        i1 = (unsigned char)(digit1 - 'a' + 0x0A);
    else if (digit1 >= 'A')
        i1 = (unsigned char)(digit1 - 'A' + 0x0A);
    else
        i1 = (unsigned char)(digit1 - '0');
    if (digit2 >= 'a')
        i2 = (unsigned char)(digit2 - 'a' + 0x0A);
    else if (digit2 >= 'A')
        i2 = (unsigned char)(digit2 - 'A' + 0x0A);
    else
        i2 = (unsigned char)(digit2 - '0');
    return (unsigned char)(0x10 * i1 + i2);
}

static bool GetRGBFromName(const char *inname, bool *isNone,
                           unsigned char *r, unsigned char*g, unsigned char *b)
{
    int left, right, middle;
    int cmp;
    wxUint32 rgbVal;
    char *name;
    char *grey, *p;

    // Neither #rrggbb nor #rrrrggggbbbb are in database, we parse them directly
    size_t inname_len = strlen(inname);
    if ( *inname == '#' && (inname_len == 7 || inname_len == 13))
    {
        size_t ofs = (inname_len == 7) ? 2 : 4;
        *r = ParseHexadecimal(inname[1], inname[2]);
        *g = ParseHexadecimal(inname[1*ofs+1], inname[1*ofs+2]);
        *b = ParseHexadecimal(inname[2*ofs+1], inname[2*ofs+2]);
        *isNone = false;
        return true;
    }

    name = wxStrdupA(inname);

    // theRGBRecords[] has no names with spaces, and no grey, but a
    // lot of gray...

    // so first extract ' '
    while ((p = strchr(name, ' ')) != NULL)
    {
        while (*(p))            // till eof of string
        {
            *p = *(p + 1);      // copy to the left
            p++;
        }
    }
    // fold to lower case
    p = name;
    while (*p)
    {
        *p = (char)tolower(*p);
        p++;
    }

    // substitute Grey with Gray, else rgbtab.h would have more than 100
    // 'duplicate' entries
    if ( (grey = strstr(name, "grey")) != NULL )
        grey[2] = 'a';

    // check for special 'none' colour:
    bool found;
    if ( strcmp(name, "none") == 0 )
    {
        *isNone = true;
        found = true;
    }
    else // not "None"
    {
        found = false;

        // binary search:
        left = 0;
        right = numTheRGBRecords - 1;
        do
        {
            middle = (left + right) / 2;
            cmp = strcmp(name, theRGBRecords[middle].name);
            if ( cmp == 0 )
            {
                rgbVal = theRGBRecords[middle].rgb;
                *r = (unsigned char)((rgbVal >> 16) & 0xFF);
                *g = (unsigned char)((rgbVal >> 8) & 0xFF);
                *b = (unsigned char)((rgbVal) & 0xFF);
                *isNone = false;
                found = true;
                break;
            }
            else if ( cmp < 0 )
            {
                right = middle - 1;
            }
            else // cmp > 0
            {
                left = middle + 1;
            }
        } while (left <= right);
    }

    free(name);

    return found;
}

static const char *ParseColor(const char *data)
{
    static const char *targets[] =
                        {"c ", "g ", "g4 ", "m ", "b ", "s ", NULL};

    const char *p, *r;
    const char *q;
    int i;

    for (i = 0; targets[i] != NULL; i++)
    {
        r = data;
        for (q = targets[i]; *r != '\0'; r++)
        {
            if ( *r != *q )
                continue;
            if ( !isspace((int) (*(r - 1))) )
                continue;
            p = r;
            for (;;)
            {
                if ( *q == '\0' )
                    return p;
                if ( *p++ != *q++ )
                    break;
            }
            q = targets[i];
        }
    }
    return NULL;
}

struct wxXPMColourMapData
{
    unsigned char R,G,B;
};
WX_DECLARE_STRING_HASH_MAP(wxXPMColourMapData, wxXPMColourMap);

wxImage wxXPMDecoder::ReadData(const char **xpm_data)
{
    wxImage img;
    int count;
    unsigned width, height, colors_cnt, chars_per_pixel;
    size_t i, j, i_key;
    wxChar key[64];
    const char *clr_def;
    bool hasMask;
    wxXPMColourMap clr_tbl;
    wxXPMColourMap::iterator it;
    wxString maskKey;

    /*
     *  Read hints and initialize structures:
     */

    count = sscanf(xpm_data[0], "%u %u %u %u",
                   &width, &height, &colors_cnt, &chars_per_pixel);
    if ( count != 4 || width * height * colors_cnt == 0 )
    {
        wxLogError(_T("XPM: Not XPM data!"));
        return wxNullImage;
    }

    // VS: XPM color map this large would be insane, since XPMs are encoded with
    //     92 possible values on each position, 92^64 is *way* larger space than
    //     8bit RGB...
    wxCHECK_MSG(chars_per_pixel < 64, wxNullImage, wxT("XPM colormaps this large not supported."));

    img.Create(width, height);
    if ( !img.Ok() ) return img;

    img.SetMask(false);
    key[chars_per_pixel] = wxT('\0');
    hasMask = false;

    /*
     *  Create colour map:
     */
    for (i = 0; i < colors_cnt; i++)
    {
        wxXPMColourMapData clr_data = {255,0,255};

        for (i_key = 0; i_key < chars_per_pixel; i_key++)
            key[i_key] = (wxChar)xpm_data[1 + i][i_key];
        clr_def = ParseColor(xpm_data[1 + i] + chars_per_pixel);

        if ( clr_def == NULL )
        {
            wxLogError(_("XPM: malformed colour definition '%s'!"),
                       xpm_data[1+i]);
        }
        else
        {
            bool isNone = false;
            if ( !GetRGBFromName(clr_def, &isNone,
                                 &clr_data.R, &clr_data.G, &clr_data.B) )
            {
                wxLogError(_("XPM: malformed colour definition '%s'!"),
                           xpm_data[1+i]);
            }
            else
            {
                if ( isNone )
                {
                    img.SetMask(true);
                    img.SetMaskColour(255, 0, 255);
                    hasMask = true;
                    maskKey = key;
                }
            }
        }
        clr_tbl[key] = clr_data;
    }

    /*
     *  Modify colour entries with RGB = (255,0,255) to (255,0,254) if
     *  mask colour is present (so that existing pixels with (255,0,255)
     *  magenta colour are not incorrectly made transparent):
     */
    if (hasMask)
    {
        for (it = clr_tbl.begin(); it != clr_tbl.end(); ++it)
        {
            if (it->second.R == 255 && it->second.G == 0 &&
                it->second.B == 255 &&
                it->first != maskKey)
            {
                it->second.B = 254;
            }
        }
    }

    /*
     *  Parse image data:
     */

    unsigned char *img_data = img.GetData();
    wxXPMColourMap::iterator entry;
    wxXPMColourMap::iterator end = clr_tbl.end();

    for (j = 0; j < height; j++)
    {
        for (i = 0; i < width; i++, img_data += 3)
        {
            for (i_key = 0; i_key < chars_per_pixel; i_key++)
                key[i_key] = (wxChar)xpm_data[1 + colors_cnt + j]
                                             [chars_per_pixel * i + i_key];
            entry = clr_tbl.find(key);
            if ( entry == end )
            {
                wxLogError(_("XPM: Malformed pixel data!"));

                // better return right now as otherwise we risk to flood the
                // user with error messages as something seems to be seriously
                // wrong with the file and so we could give this message for
                // each remaining pixel if we don't bail out
                return wxNullImage;
            }
            else
            {
                img_data[0] = entry->second.R;
                img_data[1] = entry->second.G;
                img_data[2] = entry->second.B;
            }
        }
    }

    return img;
}

#endif // wxUSE_IMAGE && wxUSE_XPM

⌨️ 快捷键说明

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