xpmdecod.cpp

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

CPP
795
字号
/////////////////////////////////////////////////////////////////////////////
// Name:        xpmdecod.cpp
// Purpose:     wxXPMDecoder
// Author:      John Cristy, Vaclav Slavik
// RCS-ID:      $Id: xpmdecod.cpp,v 1.43.2.2 2006/01/18 16:32:46 JS Exp $
// Copyright:   (c) John Cristy, Vaclav Slavik
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

/*

This file is partially based on source code of ImageMagick by John Cristy. Its
license is as follows:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%                            X   X  PPPP   M   M                              %
%                             X X   P   P  MM MM                              %
%                              X    PPPP   M M M                              %
%                             X X   P      M   M                              %
%                            X   X  P      M   M                              %
%                                                                             %
%                                                                             %
%                    Read/Write ImageMagick Image Format.                     %
%                                                                             %
%                                                                             %
%                              Software Design                                %
%                                John Cristy                                  %
%                                 July 1992                                   %
%                                                                             %
%                                                                             %
%  Copyright (C) 2001 ImageMagick Studio, a non-profit organization dedicated %
%  to making software imaging solutions freely available.                     %
%                                                                             %
%  Permission is hereby granted, free of charge, to any person obtaining a    %
%  copy of this software and associated documentation files ("ImageMagick"),  %
%  to deal in ImageMagick without restriction, including without limitation   %
%  the rights to use, copy, modify, merge, publish, distribute, sublicense,   %
%  and/or sell copies of ImageMagick, and to permit persons to whom the       %
%  ImageMagick is furnished to do so, subject to the following conditions:    %
%                                                                             %
%  The above copyright notice and this permission notice shall be included in %
%  all copies or substantial portions of ImageMagick.                         %
%                                                                             %
%  The software is provided "as is", without warranty of any kind, express or %
%  implied, including but not limited to the warranties of merchantability,   %
%  fitness for a particular purpose and noninfringement.  In no event shall   %
%  ImageMagick Studio be liable for any claim, damages or other liability,    %
%  whether in an action of contract, tort or otherwise, arising from, out of  %
%  or in connection with ImageMagick or the use or other dealings in          %
%  ImageMagick.                                                               %
%                                                                             %
%  Except as contained in this notice, the name of the ImageMagick Studio     %
%  shall not be used in advertising or otherwise to promote the sale, use or  %
%  other dealings in ImageMagick without prior written authorization from the %
%  ImageMagick Studio.                                                        %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
*/

/*
 * Also contains some pieces from libxpm and its modification for win32 by
 * HeDu <hedu@cul-ipn.uni-kiel.de>:
 *
 * Copyright (C) 1989-95 GROUPE BULL
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of GROUPE BULL shall not be
 * used in advertising or otherwise to promote the sale, use or other dealings
 * in this Software without prior written authorization from GROUPE BULL.
 */

#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "xpmdecod.h"
#endif

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#  pragma hdrstop
#endif

#ifndef WX_PRECOMP
#  include "wx/defs.h"
#endif

#if wxUSE_IMAGE && wxUSE_XPM

#include "wx/stream.h"
#include "wx/image.h"
#include "wx/utils.h"
#include "wx/log.h"
#include "wx/hashmap.h"
#include "wx/intl.h"
#include <string.h>

#include <ctype.h>

#include "wx/xpmdecod.h"

#if wxUSE_STREAMS
bool wxXPMDecoder::CanRead(wxInputStream& stream)
{
    unsigned char buf[9];

    if ( !stream.Read(buf, WXSIZEOF(buf)) )
        return false;

    stream.SeekI(-(wxFileOffset)WXSIZEOF(buf), wxFromCurrent);

    return memcmp(buf, "/* XPM */", WXSIZEOF(buf)) == 0;
}

wxImage wxXPMDecoder::ReadFile(wxInputStream& stream)
{
    size_t length = stream.GetSize();
    wxCHECK_MSG( length != 0, wxNullImage,
                 wxT("Cannot read XPM from stream of unknown size") );

    // use a smart buffer to be sure to free memory even when we return on
    // error
    wxCharBuffer buffer(length);

    char *xpm_buffer = (char *)buffer.data();
    if ( stream.Read(xpm_buffer, length).GetLastError() == wxSTREAM_READ_ERROR )
        return wxNullImage;
    xpm_buffer[length] = '\0';

    /*
     *  Remove comments from the file:
     */
    char *p, *q;
    for (p = xpm_buffer; *p != '\0'; p++)
    {
        if ( (*p == '"') || (*p == '\'') )
        {
            if (*p == '"')
            {
              for (p++; *p != '\0'; p++)
                if ( (*p == '"') && (*(p - 1) != '\\') )
                    break;
            }
            else // *p == '\''
            {
                for (p++; *p != '\0'; p++)
                    if ( (*p == '\'') && (*(p - 1) != '\\') )
                        break;
            }
            if (*p == '\0')
                break;
            continue;
        }
        if ( (*p != '/') || (*(p + 1) != '*') )
            continue;
        for (q = p + 2; *q != '\0'; q++)
        {
            if ( (*q == '*') && (*(q + 1) == '/') )
                break;
        }

        // memmove allows overlaps (unlike strcpy):
        size_t cpylen = strlen(q + 2) + 1;
        memmove(p, q + 2, cpylen);
    }

    /*
     *  Remove unquoted characters:
     */
    size_t i = 0;
    for (p = xpm_buffer; *p != '\0'; p++)
    {
        if ( *p != '"' )
            continue;
        for (q = p + 1; *q != '\0'; q++)
            if (*q == '"')
                break;
        strncpy(xpm_buffer + i, p + 1, q - p - 1);
        i += q - p - 1;
        xpm_buffer[i++] = '\n';
        p = q + 1;
    }
    xpm_buffer[i] = '\0';

    /*
     *  Create array of lines and convert \n's to \0's:
     */
    const char **xpm_lines;
    size_t lines_cnt = 0;
    size_t line;

    for (p = xpm_buffer; *p != '\0'; p++)
    {
        if ( *p == '\n' )
            lines_cnt++;
    }

    if ( !lines_cnt )
    {
        // this doesn't really look an XPM image
        return wxNullImage;
    }

    xpm_lines = new const char*[lines_cnt];
    xpm_lines[0] = xpm_buffer;
    line = 1;
    for (p = xpm_buffer; (*p != '\0') && (line < lines_cnt); p++)
    {
        if ( *p == '\n' )
        {
            xpm_lines[line] = p + 1;
            *p = '\0';
            line++;
        }
    }

    /*
     *  Read the image:
     */
    wxImage img = ReadData(xpm_lines);

    delete[] xpm_lines;

    return img;
}
#endif // wxUSE_STREAMS


/*****************************************************************************\
* rgbtab.h                                                                    *
*                                                                             *
* A hard coded rgb.txt. To keep it short I removed all colornames with        *
* trailing numbers, Blue3 etc, except the GrayXX. Sorry Grey-lovers I prefer  *
* Gray ;-). But Grey is recognized on lookups, only on save Gray will be      *
* used, maybe you want to do some substitue there too.                        *
*                                                                             *
* To save memory the RGBs are coded in one long value, as done by the RGB     *
* macro.                                                                      *
*                                                                             *
* Developed by HeDu 3/94 (hedu@cul-ipn.uni-kiel.de)                           *
\*****************************************************************************/


typedef struct
{
    const char *name;
    wxUint32 rgb;
} rgbRecord;

#define myRGB(r,g,b)   ((wxUint32)r<<16|(wxUint32)g<<8|(wxUint32)b)

static rgbRecord theRGBRecords[] =
{
    {"aliceblue", myRGB(240, 248, 255)},
    {"antiquewhite", myRGB(250, 235, 215)},
    {"aquamarine", myRGB(50, 191, 193)},
    {"azure", myRGB(240, 255, 255)},
    {"beige", myRGB(245, 245, 220)},
    {"bisque", myRGB(255, 228, 196)},
    {"black", myRGB(0, 0, 0)},
    {"blanchedalmond", myRGB(255, 235, 205)},
    {"blue", myRGB(0, 0, 255)},
    {"blueviolet", myRGB(138, 43, 226)},
    {"brown", myRGB(165, 42, 42)},
    {"burlywood", myRGB(222, 184, 135)},
    {"cadetblue", myRGB(95, 146, 158)},
    {"chartreuse", myRGB(127, 255, 0)},
    {"chocolate", myRGB(210, 105, 30)},
    {"coral", myRGB(255, 114, 86)},
    {"cornflowerblue", myRGB(34, 34, 152)},
    {"cornsilk", myRGB(255, 248, 220)},
    {"cyan", myRGB(0, 255, 255)},
    {"darkgoldenrod", myRGB(184, 134, 11)},
    {"darkgreen", myRGB(0, 86, 45)},
    {"darkkhaki", myRGB(189, 183, 107)},
    {"darkolivegreen", myRGB(85, 86, 47)},
    {"darkorange", myRGB(255, 140, 0)},
    {"darkorchid", myRGB(139, 32, 139)},
    {"darksalmon", myRGB(233, 150, 122)},
    {"darkseagreen", myRGB(143, 188, 143)},
    {"darkslateblue", myRGB(56, 75, 102)},
    {"darkslategray", myRGB(47, 79, 79)},
    {"darkturquoise", myRGB(0, 166, 166)},
    {"darkviolet", myRGB(148, 0, 211)},
    {"deeppink", myRGB(255, 20, 147)},
    {"deepskyblue", myRGB(0, 191, 255)},
    {"dimgray", myRGB(84, 84, 84)},
    {"dodgerblue", myRGB(30, 144, 255)},
    {"firebrick", myRGB(142, 35, 35)},
    {"floralwhite", myRGB(255, 250, 240)},
    {"forestgreen", myRGB(80, 159, 105)},
    {"gainsboro", myRGB(220, 220, 220)},
    {"ghostwhite", myRGB(248, 248, 255)},
    {"gold", myRGB(218, 170, 0)},
    {"goldenrod", myRGB(239, 223, 132)},
    {"gray", myRGB(126, 126, 126)},
    {"gray0", myRGB(0, 0, 0)},
    {"gray1", myRGB(3, 3, 3)},
    {"gray10", myRGB(26, 26, 26)},
    {"gray100", myRGB(255, 255, 255)},
    {"gray11", myRGB(28, 28, 28)},
    {"gray12", myRGB(31, 31, 31)},
    {"gray13", myRGB(33, 33, 33)},
    {"gray14", myRGB(36, 36, 36)},
    {"gray15", myRGB(38, 38, 38)},
    {"gray16", myRGB(41, 41, 41)},
    {"gray17", myRGB(43, 43, 43)},
    {"gray18", myRGB(46, 46, 46)},
    {"gray19", myRGB(48, 48, 48)},
    {"gray2", myRGB(5, 5, 5)},
    {"gray20", myRGB(51, 51, 51)},
    {"gray21", myRGB(54, 54, 54)},
    {"gray22", myRGB(56, 56, 56)},
    {"gray23", myRGB(59, 59, 59)},
    {"gray24", myRGB(61, 61, 61)},
    {"gray25", myRGB(64, 64, 64)},
    {"gray26", myRGB(66, 66, 66)},
    {"gray27", myRGB(69, 69, 69)},
    {"gray28", myRGB(71, 71, 71)},
    {"gray29", myRGB(74, 74, 74)},
    {"gray3", myRGB(8, 8, 8)},
    {"gray30", myRGB(77, 77, 77)},
    {"gray31", myRGB(79, 79, 79)},
    {"gray32", myRGB(82, 82, 82)},
    {"gray33", myRGB(84, 84, 84)},
    {"gray34", myRGB(87, 87, 87)},
    {"gray35", myRGB(89, 89, 89)},
    {"gray36", myRGB(92, 92, 92)},
    {"gray37", myRGB(94, 94, 94)},
    {"gray38", myRGB(97, 97, 97)},
    {"gray39", myRGB(99, 99, 99)},
    {"gray4", myRGB(10, 10, 10)},
    {"gray40", myRGB(102, 102, 102)},
    {"gray41", myRGB(105, 105, 105)},
    {"gray42", myRGB(107, 107, 107)},
    {"gray43", myRGB(110, 110, 110)},
    {"gray44", myRGB(112, 112, 112)},
    {"gray45", myRGB(115, 115, 115)},
    {"gray46", myRGB(117, 117, 117)},
    {"gray47", myRGB(120, 120, 120)},
    {"gray48", myRGB(122, 122, 122)},
    {"gray49", myRGB(125, 125, 125)},
    {"gray5", myRGB(13, 13, 13)},
    {"gray50", myRGB(127, 127, 127)},
    {"gray51", myRGB(130, 130, 130)},
    {"gray52", myRGB(133, 133, 133)},
    {"gray53", myRGB(135, 135, 135)},
    {"gray54", myRGB(138, 138, 138)},
    {"gray55", myRGB(140, 140, 140)},
    {"gray56", myRGB(143, 143, 143)},
    {"gray57", myRGB(145, 145, 145)},
    {"gray58", myRGB(148, 148, 148)},
    {"gray59", myRGB(150, 150, 150)},
    {"gray6", myRGB(15, 15, 15)},
    {"gray60", myRGB(153, 153, 153)},
    {"gray61", myRGB(156, 156, 156)},
    {"gray62", myRGB(158, 158, 158)},
    {"gray63", myRGB(161, 161, 161)},
    {"gray64", myRGB(163, 163, 163)},
    {"gray65", myRGB(166, 166, 166)},
    {"gray66", myRGB(168, 168, 168)},
    {"gray67", myRGB(171, 171, 171)},
    {"gray68", myRGB(173, 173, 173)},
    {"gray69", myRGB(176, 176, 176)},
    {"gray7", myRGB(18, 18, 18)},
    {"gray70", myRGB(179, 179, 179)},
    {"gray71", myRGB(181, 181, 181)},
    {"gray72", myRGB(184, 184, 184)},
    {"gray73", myRGB(186, 186, 186)},
    {"gray74", myRGB(189, 189, 189)},
    {"gray75", myRGB(191, 191, 191)},
    {"gray76", myRGB(194, 194, 194)},
    {"gray77", myRGB(196, 196, 196)},
    {"gray78", myRGB(199, 199, 199)},
    {"gray79", myRGB(201, 201, 201)},
    {"gray8", myRGB(20, 20, 20)},
    {"gray80", myRGB(204, 204, 204)},
    {"gray81", myRGB(207, 207, 207)},

⌨️ 快捷键说明

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