コード例 #1
0
        // This loads the PLAYPAL palette
        public override Playpal LoadPalette()
        {
            // Error when suspended
            if (issuspended)
            {
                throw new Exception("Data reader is suspended");
            }

            // Palette from wad(s)
            Playpal palette = null;

            foreach (WADReader wr in wads)
            {
                Playpal wadpalette = wr.LoadPalette();
                if (wadpalette != null)
                {
                    return(wadpalette);
                }
            }

            // Find in root directory
            string foundfile = FindFirstFile("PLAYPAL", false);

            if ((foundfile != null) && FileExists(foundfile))
            {
                MemoryStream stream = LoadFile(foundfile);
                palette = new Playpal(stream);
                stream.Dispose();
            }

            // Done
            return(palette);
        }
コード例 #2
0
        // This loads the PLAYPAL palette
        public override Playpal LoadPalette()
        {
            // Error when suspended
            if (issuspended)
            {
                throw new Exception("Data reader is suspended");
            }

            // Palette from wad(s)
            Playpal palette = null;

            foreach (WADReader wr in wads)
            {
                Playpal wadpalette = wr.LoadPalette();
                if (wadpalette != null)
                {
                    return(wadpalette);
                }
            }

            // Find in root directory
            string foundfile = FindFirstFile("PLAYPAL", false);

            if ((foundfile != null) && FileExists(foundfile))
            {
                MemoryStream stream = LoadFile(foundfile);

                if (stream.Length > 767)                //mxd
                {
                    palette = new Playpal(stream);
                }
                else
                {
                    General.ErrorLogger.Add(ErrorType.Warning, "Warning: invalid palette \"" + foundfile + "\"");
                }
                stream.Dispose();
            }

            // Done
            return(palette);
        }
コード例 #3
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());
        }
コード例 #4
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);
            }
        }
コード例 #5
0
        private static readonly int[] PCX_SIGNATURE = new[] { 10, 5, 1, 8 };         //mxd

        // Try load image data with the appropriate image reader. Returns null if the image could not be loaded
        public static Bitmap TryLoadImage(Stream data, int guessformat = UNKNOWN, Playpal palette = null)
        {
            int offsetx, offsety;

            return(TryLoadImage(data, guessformat, palette, out offsetx, out offsety));
        }
コード例 #6
0
ファイル: ImageData.cs プロジェクト: twinaphex/DoomBuilder64
        // This requests loading the image
        protected virtual void LocalLoadImage()
        {
            BitmapData bmpdata = null;

            lock (this)
            {
                // Bitmap loaded successfully?
                if (bitmap != null)
                {
                    // Bitmap has incorrect format?
                    if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
                    {
                        // villsa
                        if (General.Map.FormatInterface.InDoom64Mode)
                        {
                            ColorPalette ncp = bitmap.Palette;

                            foreach (TextureIndexInfo tp in General.Map.Config.ThingPalettes)
                            {
                                // sprite contains alternate palette?
                                if (General.Map.Data.ThingPalette.ContainsKey(tp.Title) &&
                                    tp.Index == palindex)
                                {
                                    Playpal pal = General.Map.Data.ThingPalette[tp.Title];
                                    for (int i = 0; i < 255; i++)
                                    {
                                        // copy alternate palette over
                                        ncp.Entries[i] = pal[i].ToColor();
                                    }
                                }
                            }
                        }

                        //General.ErrorLogger.Add(ErrorType.Warning, "Image '" + name + "' does not have A8R8G8B8 pixel format. Conversion was needed.");
                        Bitmap oldbitmap = bitmap;
                        try
                        {
                            // Convert to desired pixel format
                            bitmap = new Bitmap(oldbitmap.Size.Width, oldbitmap.Size.Height, PixelFormat.Format32bppArgb);
                            Graphics g = Graphics.FromImage(bitmap);
                            g.PageUnit           = GraphicsUnit.Pixel;
                            g.CompositingQuality = CompositingQuality.HighQuality;
                            g.InterpolationMode  = InterpolationMode.NearestNeighbor;
                            g.SmoothingMode      = SmoothingMode.None;
                            g.PixelOffsetMode    = PixelOffsetMode.None;
                            g.Clear(Color.Transparent);
                            g.DrawImage(oldbitmap, 0, 0, oldbitmap.Size.Width, oldbitmap.Size.Height);
                            g.Dispose();
                            oldbitmap.Dispose();
                        }
                        catch (Exception e)
                        {
                            bitmap = oldbitmap;
                            General.ErrorLogger.Add(ErrorType.Warning, "Cannot lock image '" + name + "' for pixel format conversion. The image may not be displayed correctly.\n" + e.GetType().Name + ": " + e.Message);
                        }
                    }

                    // This applies brightness correction on the image
                    if (usecolorcorrection)
                    {
                        try
                        {
                            // Try locking the bitmap
                            bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Size.Width, bitmap.Size.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
                        }
                        catch (Exception e)
                        {
                            General.ErrorLogger.Add(ErrorType.Warning, "Cannot lock image '" + name + "' for color correction. The image may not be displayed correctly.\n" + e.GetType().Name + ": " + e.Message);
                        }

                        // Bitmap locked?
                        if (bmpdata != null)
                        {
                            // Apply color correction
                            PixelColor *pixels = (PixelColor *)(bmpdata.Scan0.ToPointer());
                            General.Colors.ApplColorCorrection(pixels, bmpdata.Width * bmpdata.Height);
                            bitmap.UnlockBits(bmpdata);
                        }
                    }
                }
                else
                {
                    // Loading failed
                    // We still mark the image as ready so that it will
                    // not try loading again until Reload Resources is used
                    loadfailed = true;
                    bitmap     = new Bitmap(Properties.Resources.Failed);
                }

                if (bitmap != null)
                {
                    width  = bitmap.Size.Width;
                    height = bitmap.Size.Height;

                    // Do we still have to set a scale?
                    if ((scale.x == 0.0f) && (scale.y == 0.0f))
                    {
                        if ((General.Map != null) && (General.Map.Config != null))
                        {
                            scale.x = General.Map.Config.DefaultTextureScale;
                            scale.y = General.Map.Config.DefaultTextureScale;
                        }
                        else
                        {
                            scale.x = 1.0f;
                            scale.y = 1.0f;
                        }
                    }
                }

                // Image is ready
                imagestate = ImageLoadState.Ready;
            }
        }
コード例 #7
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());
        }