Ejemplo n.º 1
0
        /// <summary>
        /// Convert an image to a Targa (TGA) file format
        /// </summary>
        /// <param name="writer">writer to write to</param>
        /// <param name="sample">image to sample</param>
        public void Serialize(BinaryWriter writer, IColourSampler sampler)
        {
            var size = sampler.GetSize();

            //Header
            byte[] header = new byte[18];
            header[0] = (byte)0; //ID Length
            header[1] = (byte)0; //Colour map type (no colour map)
            header[2] = (byte)2; //Image type (uncompressed true-colour image)
            //Width (2 bytes)
            header[12] = (byte)(255 & size.Width);
            header[13] = (byte)(255 & (size.Width >> 8));
            //Height (2 bytes)
            header[14] = (byte)(255 & size.Height);
            header[15] = (byte)(255 & (size.Height >> 8));
            header[16] = (byte)24; //Pixel depth
            header[17] = (byte)32; //
            writer.Write(header);

            //Body
            for (int row = 0; row < size.Height; row++)
            {
                for (int column = 0; column < size.Width; column++)
                {
                    Colour c = sampler.GetPixelColour(row, column);
                    writer.Write(c.B);
                    writer.Write(c.G);
                    writer.Write(c.R);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Return the colour of the image or black if the index is out of bounds
        /// </summary>
        /// <param name="sampler">colour sampler</param>
        /// <param name="row">x coordinate</param>
        /// <param name="y">y coordinate</param>
        /// <returns>colour from sampler or black</returns>
        private Colour ColorOrDefault(IColourSampler sampler, int row, int column)
        {
            var size = sampler.GetSize();

            if (row < 0 || row >= size.Height)
            {
                return(Colour.Black);
            }
            else if (column < 0 || column >= size.Width)
            {
                return(Colour.Black);
            }
            else
            {
                return(sampler.GetPixelColour(row, column));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Convert an image to a Portal Pixel Map (PPM) file format
        /// </summary>
        /// <param name="writer">writer to write to</param>
        /// <param name="sample">image to sample</param>
        public void Serialize(TextWriter writer, IColourSampler sampler)
        {
            var size = sampler.GetSize();

            //Head
            writer.WriteLine("P3");
            writer.WriteLine(size.Width + " " + size.Height);
            writer.WriteLine(255);
            //Body
            for (int row = 0; row < size.Height; row++)
            {
                for (int column = 0; column < size.Width; column++)
                {
                    Colour c = sampler.GetPixelColour(row, column);
                    writer.WriteLine(c.R + " " + c.G + " " + c.B);
                }
            }
        }