public static void VerifyIndexedPaletteSizeDoesNotExceedBitCount(ushort bitCount, int paletteSize)
 {
     if (paletteSize > ImageHeader.CalcMaxIndexedPaletteSize(bitCount))
     {
         throw new System.Exception("Too many colors listed on the indexed palette");
     }
 }
        /// <summary>
        /// Create default indexed bitmap with palette.
        /// </summary>
        public BitmapFile(ushort bitCount, uint width, int height, Color[] palette) : this(bitCount, width, height)
        {
            if (palette.Length > 1 << bitCount)
            {
                throw new System.Exception("Unable to create bitmap. Provided palette length is greater than provided bit count.");
            }

            // Expand palette, if needed
            int maxSize = ImageHeader.CalcMaxIndexedPaletteSize(bitCount);

            if (palette.Length != maxSize)
            {
                Color[] newPalette = new Color[maxSize];
                System.Array.Copy(palette, newPalette, palette.Length < newPalette.Length ? palette.Length : newPalette.Length);

                palette = newPalette;
            }

            // Create headers
            int pixelOffset = BmpHeader.SizeInBytes + ImageHeader.SizeInBytes + palette.Length * Color.SizeInBytes;
            int fileSize    = pixelOffset + ImageHeader.CalculatePitch(bitCount, (int)width) * System.Math.Abs(height);

            bmpHeader   = BmpHeader.Create((uint)fileSize, (uint)pixelOffset);
            imageHeader = ImageHeader.Create((int)width, height, bitCount);

            // Store data
            this.palette = palette;
        }
        private void WriteIndexed(BinaryWriter seekableWriter, ushort bitCount, int width, int height, Color[] palette, byte[] indexedPixels)
        {
            VerifyIndexedImageForSerialization(bitCount);
            VerifyIndexedPaletteSizeDoesNotExceedBitCount(bitCount, palette.Length);
            VerifyPixelSizeMatchesImageDimensionsWithPitch(bitCount, width, height, indexedPixels.Length);

            int maxSize = ImageHeader.CalcMaxIndexedPaletteSize(bitCount);

            if (palette.Length != maxSize)
            {
                Color[] newPalette = new Color[maxSize];
                System.Array.Copy(palette, newPalette, palette.Length < newPalette.Length ? palette.Length : newPalette.Length);

                palette = newPalette;
            }

            WriteHeaders(seekableWriter, bitCount, width, height, palette);

            for (int i = 0; i < palette.Length; ++i)
            {
                palette[i].Serialize(seekableWriter);
            }

            WritePixels(seekableWriter, indexedPixels, width, height, bitCount);
        }
        /// <summary>
        /// Create default indexed bitmap.
        /// </summary>
        public BitmapFile(ushort bitCount, uint width, int height)
        {
            VerifyIndexedImageForSerialization(bitCount);

            // Create headers and default palette
            imageHeader = ImageHeader.Create((int)width, (int)height, bitCount);
            palette     = new Color[imageHeader.CalcMaxIndexedPaletteSize()];
            pixels      = new byte[imageHeader.CalculatePitch() * System.Math.Abs(height)];

            int pixelOffset    = BmpHeader.SizeInBytes + ImageHeader.SizeInBytes + palette.Length * Color.SizeInBytes;
            int bitmapFileSize = pixelOffset + pixels.Length * sizeof(byte);

            //if (bitmapFileSize > uint.MaxValue) {
            //	throw new System.Exception("Maximum size of a bitmap file has been exceeded");
            //}

            bmpHeader = BmpHeader.Create((uint)bitmapFileSize, (uint)pixelOffset);
        }