Ejemplo n.º 1
0
        public static Bitmap TryLoadImage(Stream data, int guessformat, Playpal palette, out int offsetx, out int offsety)
        {
            offsetx = int.MinValue;
            offsety = int.MinValue;
            try
            {
                if (data == null)
                {
                    return(null);
                }

                // Data long enough to check for signatures?
                if (data.Length > 10)
                {
                    IImageReader loader = null;
                    if (CheckSignature(data, PNG_SIGNATURE))
                    {
                        loader = new FrameworkImageReader(true);
                    }
                    else if (CheckSignature(data, JPG_SIGNATURE))
                    {
                        loader = new FrameworkImageReader(false);
                    }
                    else if (CheckSignature(data, PCX_SIGNATURE))
                    {
                        loader = new PcxImageReader();
                    }
                    else if (CheckTgaSignature(data))
                    {
                        loader = new TgaImageReader();
                    }

                    if (loader != null)
                    {
                        data.Seek(0, SeekOrigin.Begin);
                        try
                        {
                            Bitmap image = loader.ReadAsBitmap(data, out offsetx, out offsety);
                            if (image != null) // The older loaders return null when they should throw an exception
                            {
                                return(image);
                            }
                        }
                        catch
                        {
                        }
                    }
                }

                IImageReader doomloader = null;

                // Could it be a doom picture?
                switch (guessformat)
                {
                case DOOMPICTURE:
                    // Check if data is valid for a doom picture
                    data.Seek(0, SeekOrigin.Begin);
                    DoomPictureReader picreader = new DoomPictureReader(palette);
                    if (picreader.Validate(data))
                    {
                        doomloader = picreader;
                    }
                    break;

                case DOOMFLAT:
                    // Check if data is valid for a doom flat
                    data.Seek(0, SeekOrigin.Begin);
                    DoomFlatReader flatreader = new DoomFlatReader(palette);
                    if (flatreader.Validate(data))
                    {
                        doomloader = flatreader;
                    }
                    break;

                case DOOMCOLORMAP:
                    // Check if data is valid for a doom colormap
                    data.Seek(0, SeekOrigin.Begin);
                    DoomColormapReader colormapreader = new DoomColormapReader(palette);
                    if (colormapreader.Validate(data))
                    {
                        doomloader = colormapreader;
                    }
                    break;
                }

                if (doomloader != null)
                {
                    data.Seek(0, SeekOrigin.Begin);
                    Bitmap image = doomloader.ReadAsBitmap(data, out offsetx, out offsety);
                    if (image != null)
                    {
                        return(image);
                    }
                }

                return(null);
            }
            catch
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
        // This check image data and returns the appropriate image reader
        public static IImageReader GetImageReader(Stream data, int guessformat, Playpal palette)
        {
            BinaryReader       bindata = new BinaryReader(data);
            DoomPictureReader  picreader;
            DoomFlatReader     flatreader;
            DoomColormapReader colormapreader;

            // First check the formats that provide the means to 'ensure' that
            // it actually is that format. Then guess the Doom image format.

            // Data long enough to check for signatures?
            if (data.Length > 10)
            {
                // Check for PNG signature
                data.Seek(0, SeekOrigin.Begin);
                if (CheckSignature(data, PNG_SIGNATURE))
                {
                    return(new FileImageReader());
                }

                // Check for DDS signature
                data.Seek(0, SeekOrigin.Begin);
                if (CheckSignature(data, DDS_SIGNATURE))
                {
                    return(new FileImageReader());
                }

                // Check for GIF signature
                data.Seek(0, SeekOrigin.Begin);
                if (CheckSignature(data, GIF_SIGNATURE))
                {
                    return(new FileImageReader());
                }

                // Check for BMP signature
                data.Seek(0, SeekOrigin.Begin);
                if (CheckSignature(data, BMP_SIGNATURE))
                {
                    // Check if data size matches the size specified in the data
                    if (bindata.ReadUInt32() <= data.Length)
                    {
                        return(new FileImageReader());
                    }
                }
            }

            // Could it be a doom picture?
            if (guessformat == DOOMPICTURE)
            {
                // Check if data is valid for a doom picture
                data.Seek(0, SeekOrigin.Begin);
                picreader = new DoomPictureReader(palette);
                if (picreader.Validate(data))
                {
                    return(picreader);
                }
            }
            // Could it be a doom flat?
            else if (guessformat == DOOMFLAT)
            {
                // Check if data is valid for a doom flat
                data.Seek(0, SeekOrigin.Begin);
                flatreader = new DoomFlatReader(palette);
                if (flatreader.Validate(data))
                {
                    return(flatreader);
                }
            }
            // Could it be a doom colormap?
            else if (guessformat == DOOMCOLORMAP)
            {
                // Check if data is valid for a doom colormap
                data.Seek(0, SeekOrigin.Begin);
                colormapreader = new DoomColormapReader(palette);
                if (colormapreader.Validate(data))
                {
                    return(colormapreader);
                }
            }

            // Format not supported
            return(new UnknownImageReader());
        }
Ejemplo n.º 3
0
        private static readonly int[] PCX_SIGNATURE = new[] { 10, 5, 1, 8 };         //mxd

        // This check image data and returns the appropriate image reader
        public static IImageReader GetImageReader(Stream data, int guessformat, Playpal palette)
        {
            if (data == null)
            {
                return(new UnknownImageReader());                         //mxd
            }
            // Data long enough to check for signatures?
            if (data.Length > 10)
            {
                uint ilType = DevilImageType.IL_TYPE_UNKNOWN;

                // Check for PNG signature
                if (CheckSignature(data, PNG_SIGNATURE))
                {
                    ilType = DevilImageType.IL_PNG;
                }

                // Check for DDS signature
                else if (CheckSignature(data, DDS_SIGNATURE))
                {
                    ilType = DevilImageType.IL_DDS;
                }

                //mxd. Check for PCX signature
                else if (CheckSignature(data, PCX_SIGNATURE))
                {
                    ilType = DevilImageType.IL_PCX;
                }

                //mxd. Check for JPG signature
                else if (CheckSignature(data, JPG_SIGNATURE))
                {
                    ilType = DevilImageType.IL_JPG;
                }

                //mxd. TGA is VERY special in that it doesn't have a proper signature...
                else if (CheckTgaSignature(data))
                {
                    ilType = DevilImageType.IL_TGA;
                }

                //
                if (ilType != DevilImageType.IL_TYPE_UNKNOWN)
                {
                    FileImageReader ilreader = new FileImageReader(ilType, guessformat, palette);
                    // also fill in the possible proxy type
                    return(ilreader);
                }

                /*
                 *              // Check for GIF signature
                 *              if(CheckSignature(data, GIF_SIGNATURE)) return new UnknownImageReader(); //mxd. Not supported by (G)ZDoom
                 *
                 *              // Check for BMP signature
                 *              if(CheckSignature(data, BMP_SIGNATURE)) return new UnknownImageReader(); //mxd. Not supported by (G)ZDoom
                 */
            }

            // Could it be a doom picture?
            switch (guessformat)
            {
            case DOOMPICTURE:
                // Check if data is valid for a doom picture
                data.Seek(0, SeekOrigin.Begin);
                DoomPictureReader picreader = new DoomPictureReader(palette);
                if (picreader.Validate(data))
                {
                    return(picreader);
                }
                break;

            case DOOMFLAT:
                // Check if data is valid for a doom flat
                data.Seek(0, SeekOrigin.Begin);
                DoomFlatReader flatreader = new DoomFlatReader(palette);
                if (flatreader.Validate(data))
                {
                    return(flatreader);
                }
                break;

            case DOOMCOLORMAP:
                // Check if data is valid for a doom colormap
                data.Seek(0, SeekOrigin.Begin);
                DoomColormapReader colormapreader = new DoomColormapReader(palette);
                if (colormapreader.Validate(data))
                {
                    return(colormapreader);
                }
                break;
            }

            // Format not supported
            return(new UnknownImageReader());
        }