Example #1
0
        public override unsafe void Save(string file)
        {
            if (string.IsNullOrEmpty(file))
            {
                throw new ArgumentException("WORM.Save(string)\n\tThe path cannot be null or empty");
            }

            switch (Path.GetExtension(file))
            {
            case ".wrm":
                this.Format = WormFormat.wrm;
                break;

            case ".dwrm":
                this.Format = WormFormat.dwrm;
                break;
            }

            if (this.Format != WormFormat.nil)
            {
                Save(file, this.Format);
            }
            else
            {
                Save(file, WormFormat.wrm);
            }
        }
Example #2
0
 public override void Clear()
 {
     if (Image != null)
     {
         Image.Dispose();
     }
     Format = WormFormat.nil;
     Image  = null;
 }
Example #3
0
        public override unsafe void Load(string file)
        {
            if (string.IsNullOrEmpty(file))
            {
                throw new ArgumentException("WORM.Load(string)\n\tThe path cannot be null or empty");
            }

            Clear();

            using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (BinaryReader binaryReader = new BinaryReader(fileStream))
                    using (GZipStream decompressor = new GZipStream(fileStream, CompressionMode.Decompress))
                    {
                        Format = GetWormFormat(binaryReader);

                        if (Format == WormFormat.nil)
                        {
                            throw new Exception("WORM.Load(string)\n\tInvalid WORM file cannot read");
                        }

                        int width  = binaryReader.ReadUInt16();
                        int height = binaryReader.ReadUInt16();

                        Image     = new Bitmap(width, height, PixelFormat.Format24bppRgb);
                        Image.Tag = ImgFormat.wrm;

                        BitmapData dstBD            = null;
                        byte[]     decompressedData = new byte[width * height * 2];
                        decompressor.Read(decompressedData, 0, decompressedData.Length);

                        dstBD = Image.LockBits(
                            new Rectangle(0, 0, Image.Width, Image.Height),
                            ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
                        try
                        {
                            byte *pDst = (byte *)(void *)dstBD.Scan0;

                            for (int i = 0; i < width * height; i += 1)
                            {
                                int    index   = i << 1;
                                int    dec     = (ushort)((decompressedData[index]) | (decompressedData[index + 1] << 8));
                                double percent = (double)dec / (double)ushort.MaxValue;
                                int    COLOR   = (int)Math.Round(percent * MAX_DEC);
                                Color  c       = ReadWORMPixel(COLOR);

                                *(pDst++) = c.B;
                                *(pDst++) = c.G;
                                *(pDst++) = c.R;
                                pDst++; // skip alpha
                            }
                        }
                        finally
                        {
                            Image.UnlockBits(dstBD);
                        }
                    }
        }
Example #4
0
        public WORM(Bitmap bmp)
        {
            if (bmp.Width > MAX_SIZE || bmp.Height > MAX_SIZE)
            {
                throw new Exception($"WORM images do not support width or height larger than {MAX_SIZE}");
            }

            this.Image  = bmp;
            this.Format = WormFormat.nil;
        }
Example #5
0
        /// <summary>
        /// Gets the dimensions of a WORM image from a binary reader.
        /// <para>If the Identifier has already been read call this with False as the second argument.</para>
        /// </summary>
        /// <param name="binaryReader">The binary reader to read from.</param>
        /// <param name="checkIdentifier">Should the WRM_IDENTIFIER or DWRM_IDENTIFIER be read from the stream.</param>
        /// <returns>The width and height of the image.</returns>
        public static Size GetDimensions(BinaryReader binaryReader, bool checkIdentifier = true)
        {
            try
            {
                if (checkIdentifier)
                {
                    WormFormat format = GetWormFormat(binaryReader);

                    if (format == WormFormat.nil)
                    {
                        return(Size.Empty);
                    }
                }

                int Width  = binaryReader.ReadUInt16();
                int Height = binaryReader.ReadUInt16();
                return(new Size(Width, Height));
            }
            catch
            {
            }
            return(Size.Empty);
        }
Example #6
0
        /// <summary>
        /// Saves an image to disk.
        /// <para>Can throw just about any exception and should be used in a try catch</para>
        /// </summary>
        /// <param name="file">The path to save.</param>
        /// <param name="format">The <see cref="WormFormat"/> to encode.</param>
        /// <exception cref="Exception"></exception>
        public virtual unsafe void Save(string file, WormFormat format)
        {
            if (string.IsNullOrEmpty(file))
            {
                throw new ArgumentException("WORM.Save(string, WormFormat)\n\tThe path cannot be null or empty");
            }
            if (this.Image == null)
            {
                throw new ArgumentException("WORM.Save(string, WormFormat)\n\tThe image cannot be null");
            }

            using (Stream stream = new FileStream(file, FileMode.OpenOrCreate))
                using (BinaryWriter binaryWriter = new BinaryWriter(stream))
                    using (GZipStream compressor = new GZipStream(stream, CompressionLevel.Optimal))
                    {
                        switch (format)
                        {
                        case WormFormat.nil:
                            throw new Exception("WORM.Save(string, WormFormat)\n\tInvalid worm format.");

                        case WormFormat.wrm:
                            binaryWriter.Write(IdentifierBytes_1, 0, IdentifierBytes_1.Length);
                            break;

                        case WormFormat.dwrm:
                            binaryWriter.Write(IdentifierBytes_2, 0, IdentifierBytes_2.Length);
                            break;
                        }

                        binaryWriter.Write((ushort)this.Image.Width);
                        binaryWriter.Write((ushort)this.Image.Height);

                        BitmapData dstBD = null;
                        byte[]     data  = new byte[this.Image.Width * this.Image.Height * 2];

                        dstBD = Image.LockBits(
                            new Rectangle(0, 0, this.Image.Width, this.Image.Height),
                            ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
                        try
                        {
                            byte *pDst = (byte *)(void *)dstBD.Scan0;

                            for (int i = 0; i < this.Image.Width * this.Image.Height; i++)
                            {
                                int    Decimal      = ColorToDecimal(*(pDst + 2), *(pDst + 1), *(pDst));
                                ushort StorageValue = (ushort)Math.Round(ushort.MaxValue * (Decimal / MAX_DEC));

                                int index = i << 1;

                                data[index]     = (byte)StorageValue;
                                data[index + 1] = (byte)(StorageValue >> 8);

                                pDst += 4; // advance
                            }
                            compressor.Write(data, 0, data.Length);
                        }
                        finally
                        {
                            Image.UnlockBits(dstBD);
                        }
                    }
        }