dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnetlib/DotGNU.Images .cvsignore,NONE,1.1 Bmp


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnetlib/DotGNU.Images .cvsignore,NONE,1.1 BmpReader.cs,NONE,1.1 BmpWriter.cs,NONE,1.1 DotGNU.Images.build,NONE,1.1 Frame.cs,NONE,1.1 Image.cs,NONE,1.1 Makefile.am,NONE,1.1 PixelFormat.cs,NONE,1.1 RotateFlipType.cs,NONE,1.1 Utils.cs,NONE,1.1
Date: Sun, 06 Jul 2003 21:37:44 -0400

Update of /cvsroot/dotgnu-pnet/pnetlib/DotGNU.Images
In directory subversions:/tmp/cvs-serv32373/DotGNU.Images

Added Files:
        .cvsignore BmpReader.cs BmpWriter.cs DotGNU.Images.build 
        Frame.cs Image.cs Makefile.am PixelFormat.cs RotateFlipType.cs 
        Utils.cs 
Log Message:


Add the DotGNU.Images support library.


--- NEW FILE ---
Makefile
Makefile.in
.deps
*.dll

--- NEW FILE ---
/*
 * BmpReader.cs - Implementation of the "DotGNU.Images.BmpReader" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software, you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY, without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program, if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace DotGNU.Images
{

using System;
using System.IO;

internal sealed class BmpReader
{
        // Load a BMP image from the specified stream.  The first 4 bytes
        // have already been read and discarded.
        public static void Load(Stream stream, Image image)
                        {
                                byte[] buffer = new byte [1024];
                                int width, height, planes, bitCount;
                                int compression;
                                bool quads;

                                // Read the rest of the BITMAPFILEHEADER.
                                if(stream.Read(buffer, 0, 10) != 10)
                                {
                                        throw new FormatException();
                                }
                                int bfOffBits = Utils.ReadInt32(buffer, 6);

                                // The current file offset at the end of the 
BITMAPFILEHEADER.
                                int offset = 14;

                                // Get the size of the BITMAPINFOHEADER 
structure that follows,
                                // and then read it into the buffer.
                                if(stream.Read(buffer, 0, 4) != 4)
                                {
                                        throw new FormatException();
                                }
                                int size = Utils.ReadInt32(buffer, 0);
                                if(size <= 4 || size > 1024)
                                {
                                        throw new FormatException();
                                }
                                if(stream.Read(buffer, 4, size - 4) != (size - 
4))
                                {
                                        throw new FormatException();
                                }
                                offset += size;
                                if(size >= 40)
                                {
                                        // This is a BITMAPINFOHEADER structure 
(Windows bitmaps).
                                        width = Utils.ReadInt32(buffer, 4);
                                        height = Utils.ReadInt32(buffer, 8);
                                        planes = Utils.ReadUInt16(buffer, 12);
                                        bitCount = Utils.ReadUInt16(buffer, 14);
                                        compression = Utils.ReadInt32(buffer, 
16);
                                        quads = true;
                                }
                                else if(size == 12)
                                {
                                        // This is a BITMAPCOREHEADER structure 
(OS/2 bitmaps).
                                        width = Utils.ReadUInt16(buffer, 4);
                                        height = Utils.ReadUInt16(buffer, 6);
                                        planes = Utils.ReadUInt16(buffer, 8);
                                        bitCount = Utils.ReadUInt16(buffer, 10);
                                        compression = 0;        // BI_RGB
                                        quads = false;
                                }
                                else
                                {
                                        throw new FormatException();
                                }

                                // Perform a sanity check on the header values.
                                if(width < 1 || height < 1 || planes != 1)
                                {
                                        throw new FormatException();
                                }
                                if(bitCount != 1 && bitCount != 4 &&
                                   bitCount != 8 && bitCount != 24)
                                {
                                        // TODO: non-traditional BMP formats.
                                        throw new FormatException();
                                }
                                if(compression != 0)
                                {
                                        // TODO: RLE bitmaps
                                        throw new FormatException();
                                }

                                // Set the basic image properties.
                                image.Width = width;
                                image.Height = height;
                                image.SetBitCount(bitCount);
                                image.LoadFormat = Image.Bmp;

                                // Read the palette into memory and set it.
                                if(bitCount <= 8)
                                {
                                        int colors = (1 << bitCount);
                                        int index;
                                        int[] palette = new int [colors];
                                        if(quads)
                                        {
                                                // The RGB values are specified 
as RGBQUAD's.
                                                if(stream.Read(buffer, 0, 
colors * 4) != (colors * 4))
                                                {
                                                        throw new 
FormatException();
                                                }
                                                offset += colors * 4;
                                                for(index = 0; index < colors; 
++index)
                                                {
                                                        palette[index] = 
Utils.ReadBGR(buffer, index * 4);
                                                }
                                        }
                                        else
                                        {
                                                // The RGB values are specified 
as RGBTRIPLE's.
                                                if(stream.Read(buffer, 0, 
colors * 3) != (colors * 3))
                                                {
                                                        throw new 
FormatException();
                                                }
                                                offset += colors * 3;
                                                for(index = 0; index < colors; 
++index)
                                                {
                                                        palette[index] = 
Utils.ReadBGR(buffer, index * 3);
                                                }
                                        }
                                        image.Palette = palette;
                                }

                                // Seek to the start of the bitmap data.
                                Utils.Seek(stream, offset, bfOffBits);

                                // Add a frame to the image object.
                                Frame frame = image.AddFrame();

                                // Load the bitmap data from the stream into 
the frame.
                                LoadBitmapData(stream, frame, false);
                        }

        // Load bitmap data into a frame.
        public static void LoadBitmapData(Stream stream, Frame frame, bool mask)
                        {
                                byte[] data;
                                int stride;
                                int line;

                                // Get the buffer and stride for the frame.
                                if(!mask)
                                {
                                        data = frame.Data;
                                        stride = frame.Stride;
                                }
                                else
                                {
                                        frame.AddMask();
                                        data = frame.Mask;
                                        stride = frame.MaskStride;
                                }

                                // BMP images are stored upside down in the 
stream.
                                for(line = frame.Height - 1; line >= 0; --line)
                                {
                                        stream.Read(data, line * stride, 
stride);
                                }
                        }

}; // class BmpReader

}; // namespace DotGNU.Images

--- NEW FILE ---
/*
 * BmpWriter.cs - Implementation of the "DotGNU.Images.BmpWriter" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software, you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY, without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program, if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace DotGNU.Images
{

using System;
using System.IO;

internal sealed class BmpWriter
{
        // Save a BMP image to the specified stream.
        public static void Save(Stream stream, Image image)
                        {
                                byte[] buffer = new byte [1024];
                                int bitCount;
                                int offset;
                                int size;

                                // We can only save the first frame in BMP 
formats.
                                Frame frame = image.GetFrame(0);
                                if(frame == null)
                                {
                                        return;
                                }

                                // Determine the size of the bitmap and the 
offset to it.
                                bitCount = 
Utils.FormatToBitCount(frame.PixelFormat);
                                size = frame.Stride * frame.Height;
                                offset = 14 + 40;
                                if(bitCount <= 8)
                                {
                                        offset += (1 << bitCount) * 4;
                                }

                                // Build and write the BITMAPFILEHEADER 
structure.
                                buffer[0] = (byte)'B';
                                buffer[1] = (byte)'M';
                                Utils.WriteInt32(buffer, 2, offset + size);
                                buffer[6] = (byte)0;
                                buffer[7] = (byte)0;
                                buffer[8] = (byte)0;
                                buffer[9] = (byte)0;
                                Utils.WriteInt32(buffer, 10, offset);
                                stream.Write(buffer, 0, 14);

                                // Build and write the BITMAPINFO details.
                                SaveBitmapInfo(stream, frame, bitCount, size, 
buffer);

                                // Write the bitmap data in the frame to the 
stream.
                                SaveBitmapData(stream, frame, false);
                        }

        // Save a BITMAPINFO structure for a frame.
        public static void SaveBitmapInfo
                                (Stream stream, Frame frame, int bitCount,
                                 int size, byte[] buffer)
                        {
                                // Build and write the BITMAPINFOHEADER 
structure.
                                Utils.WriteInt32(buffer, 0, 40);                
        // biSize
                                Utils.WriteInt32(buffer, 4, frame.Width);
                                Utils.WriteInt32(buffer, 8, frame.Height);
                                Utils.WriteUInt16(buffer, 12, 1);               
        // biPlanes
                                Utils.WriteUInt16(buffer, 14, bitCount);
                                Utils.WriteInt32(buffer, 16, 0);                
        // biCompression
                                Utils.WriteInt32(buffer, 20, size);
                                Utils.WriteInt32(buffer, 24, 3780);             
        // biXPelsPerMeter
                                Utils.WriteInt32(buffer, 28, 3780);             
        // biYPelsPerMeter
                                Utils.WriteInt32(buffer, 32, 0);                
        // biClrUsed
                                Utils.WriteInt32(buffer, 36, 0);                
        // biClrImportant
                                stream.Write(buffer, 0, 40);

                                // Write the palette.
                                if(bitCount <= 8)
                                {
                                        int[] palette = frame.Palette;
                                        int count = (1 << bitCount);
                                        int index;
                                        for(index = 0; index < count; ++index)
                                        {
                                                if(palette != null && index < 
palette.Length)
                                                {
                                                        Utils.WriteBGR(buffer, 
index * 4, palette[index]);
                                                }
                                                else
                                                {
                                                        // Short palette: pad 
with black pixels.
                                                        Utils.WriteBGR(buffer, 
index * 4, 0);
                                                }
                                        }
                                        stream.Write(buffer, 0, count * 4);
                                }
                        }

        // Save the bitmap data in a frame.
        public static void SaveBitmapData(Stream stream, Frame frame, bool mask)
                        {
                                byte[] data;
                                int stride;
                                int line;

                                // Get the buffer and stride for the frame.
                                if(!mask)
                                {
                                        data = frame.Data;
                                        stride = frame.Stride;
                                }
                                else
                                {
                                        frame.AddMask();
                                        data = frame.Mask;
                                        stride = frame.MaskStride;
                                }

                                // BMP images are stored upside down in the 
stream.
                                for(line = frame.Height - 1; line >= 0; --line)
                                {
                                        stream.Write(data, line * stride, 
stride);
                                }
                        }

}; // class BmpWriter

}; // namespace DotGNU.Images

--- NEW FILE ---
<?xml version="1.0"?>
<project name="pnetlib DotGNU.Images" default="all">
        <target name="all">

                <!-- Build the primary DotGNU.Images.dll library -->
                <compile output="DotGNU.Images.dll"
                                 target="library"
                                 unsafe="true"
                                 nostdlib="true"
                                 debug="${CONFIG_DEBUG_LINES}"
                                 optimize="true">

                        <sources>
                                <includes name="**/*.cs"/>
                        </sources>

                        <references>
                                <file name="../runtime/mscorlib.dll"/>
                        </references>

                        <arg compiler="cscc" value="-Wno-empty-input"/>
                        <arg compiler="cscc" value="-fminimize-parameters"/>
                        <arg compiler="cscc" value="-flatin1-charset"/>
                        <arg compiler="csc" value="/nowarn:626"/>
                        <arg compiler="csc" value="/nowarn:649"/>
                        <arg compiler="csc" value="/nowarn:168"/>
                        <arg compiler="csc" value="/nowarn:67"/>
                        <arg compiler="csc" value="/nowarn:169"/>
                        <arg compiler="csc" value="/nowarn:679"/>
                </compile>

        </target>
</project>

--- NEW FILE ---
/*
 * Frame.cs - Implementation of the "DotGNU.Images.Frame" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software, you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY, without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program, if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace DotGNU.Images
{

using System;
using System.Runtime.InteropServices;

public class Frame : MarshalByRefObject, IDisposable
{
        // Internal state.
        private Image image;
        private int width;
        private int height;
        private int stride;
        private int maskStride;
        private PixelFormat pixelFormat;
        private int[] palette;
        private int transparentPixel;
        private byte[] data;
        private byte[] mask;

        // Constructor.
        internal Frame(Image image, int width, int height, PixelFormat 
pixelFormat)
                        {
                                this.image = image;
                                this.width = width;
                                this.height = height;
                                this.stride = Utils.FormatToStride(pixelFormat, 
width);
                                this.maskStride = (((width + 7) / 8) + 3) & ~3;
                                this.pixelFormat = pixelFormat;
                                this.palette = null;
                                this.transparentPixel = -1;
                                this.data = new byte [width * stride];
                                this.mask = null;
                        }
        private Frame(Image newImage, Frame frame)
                        {
                                // Clone from the other frame.
                                image = newImage;
                                width = frame.width;
                                height = frame.height;
                                stride = frame.stride;
                                maskStride = frame.maskStride;
                                pixelFormat = frame.PixelFormat;
                                if(frame.palette != null)
                                {
                                        if(frame.palette == frame.image.Palette)
                                        {
                                                // The palette is a copy of the 
image's.
                                                palette = newImage.Palette;
                                        }
                                        else
                                        {
                                                // The palette is specific to 
this frame.
                                                palette = 
(int[])(frame.palette.Clone());
                                        }
                                }
                                transparentPixel = frame.transparentPixel;
                                if(frame.data != null)
                                {
                                        data = (byte[])(frame.data.Clone());
                                }
                                if(frame.mask != null)
                                {
                                        mask = (byte[])(frame.mask.Clone());
                                }
                        }

        // Destructor.
        ~Frame()
                        {
                                Dispose(false);
                        }

        // Get the frame's properties.
        public int Width
                        {
                                get
                                {
                                        return width;
                                }
                        }
        public int Height
                        {
                                get
                                {
                                        return height;
                                }
                        }
        public int Stride
                        {
                                get
                                {
                                        return stride;
                                }
                        }
        public int MaskStride
                        {
                                get
                                {
                                        return maskStride;
                                }
                        }
        public PixelFormat PixelFormat
                        {
                                get
                                {
                                        return pixelFormat;
                                }
                        }
        public byte[] Data
                        {
                                get
                                {
                                        return data;
                                }
                        }
        public byte[] Mask
                        {
                                get
                                {
                                        return mask;
                                }
                        }
        public int[] Palette
                        {
                                get
                                {
                                        // The palette for indexed images, null 
if an RGB image.
                                        return palette;
                                }
                                set
                                {
                                        palette = value;
                                }
                        }
        public int TransparentPixel
                        {
                                get
                                {
                                        // Index into "Palette" of the 
transparent pixel value.
                                        // Returns -1 if there is no 
transparent pixel specified.
                                        return transparentPixel;
                                }
                                set
                                {
                                        transparentPixel = value;
                                }
                        }

        // Add a mask to this frame.
        public void AddMask()
                        {
                                if(mask == null)
                                {
                                        mask = new byte [width * maskStride];
                                }
                        }

        // Clone this frame into a new image.
        internal Frame CloneFrame(Image newImage)
                        {
                                return new Frame(newImage, this);
                        }

        // Dispose of this object.
        public void Dispose()
                        {
                                Dispose(true);
                                GC.SuppressFinalize(this);
                        }
        protected virtual void Dispose(bool disposing)
                        {
                                data = null;
                                mask = null;
                        }

        // Get the pixel value at a specific location.
        public int GetPixel(int x, int y)
                        {
                                // TODO
                                return 0;
                        }

        // Get the mask value at a specific location.
        public int GetMask(int x, int y)
                        {
                                // TODO
                                return 0;
                        }

        // Get the contents of a scan line (the buffer must be
        // at least "Stride" bytes in length).
        public void GetScanLine(int line, byte[] buffer)
                        {
                                if(line >= 0 && line < height && data != null)
                                {
                                        Array.Copy(data, line * stride, buffer, 
0, stride);
                                }
                        }
        public void GetScanLine(int line, IntPtr buffer)
                        {
                                if(line >= 0 && line < height && data != null &&
                                   buffer != IntPtr.Zero)
                                {
                                        Marshal.Copy(data, line * stride, 
buffer, stride);
                                }
                        }

        // Get the contents of a mask line (the buffer must be
        // at least "MaskStride" bytes in length).
        public void GetMaskLine(int line, byte[] buffer)
                        {
                                if(line >= 0 && line < height && mask != null)
                                {
                                        Array.Copy(mask, line * maskStride, 
buffer, 0, maskStride);
                                }
                        }
        public void GetMaskLine(int line, IntPtr buffer)
                        {
                                if(line >= 0 && line < height && mask != null &&
                                   buffer != IntPtr.Zero)
                                {
                                        Marshal.Copy(mask, line * maskStride, 
buffer, maskStride);
                                }
                        }

        // Rotate or flip the contents of this frame.
        public void RotateFlip(RotateFlipType rotateFlipType)
                        {
                                // TODO
                        }

        // Get a re-scaled version of this frame.
        public Frame Scale(int newWidth, int newHeight)
                        {
                                // TODO
                                return null;
                        }

        // Set the pixel value at a specific location.
        public void SetPixel(int x, int y, int value)
                        {
                                // TODO
                        }

        // Set the mask value at a specific location.
        public void SetMask(int x, int y, int value)
                        {
                                // TODO
                        }

        // Set the contents of a scan line (the buffer must be
        // at least "Stride" bytes in length).
        public void SetScanLine(int line, byte[] buffer)
                        {
                                if(line >= 0 && line < height && data != null)
                                {
                                        Array.Copy(buffer, 0, data, line * 
stride, stride);
                                }
                        }
        public void SetScanLine(int line, IntPtr buffer)
                        {
                                if(line >= 0 && line < height && data != null &&
                                   buffer != IntPtr.Zero)
                                {
                                        Marshal.Copy(buffer, data, line * 
stride, stride);
                                }
                        }

        // Set the contents of a mask line (the buffer must be
        // at least "MaskStride" bytes in length).
        public void SetMaskLine(int line, byte[] buffer)
                        {
                                if(line >= 0 && line < height && mask != null)
                                {
                                        Array.Copy(buffer, 0, mask, line * 
maskStride, maskStride);
                                }
                        }
        public void SetMaskLine(int line, IntPtr buffer)
                        {
                                if(line >= 0 && line < height && mask != null &&
                                   buffer != IntPtr.Zero)
                                {
                                        Marshal.Copy(buffer, mask, line * 
maskStride, maskStride);
                                }
                        }

}; // class Frame

}; // namespace DotGNU.Images

--- NEW FILE ---
/*
 * Image.cs - Implementation of the "DotGNU.Images.Image" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software, you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY, without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program, if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace DotGNU.Images
{

using System;
using System.IO;

public class Image : MarshalByRefObject, ICloneable, IDisposable
{
        // Internal state.
        private int width;
        private int height;
        private PixelFormat pixelFormat;
        private int numFrames;
        private Frame[] frames;
        private String format;
        private int[] palette;
        private int transparentPixel;

        // Standard image formats.
        public static readonly String Png = "png";
        public static readonly String Jpeg = "jpeg";
        public static readonly String Gif = "gif";
        public static readonly String Tiff = "tiff";
        public static readonly String Bmp = "bmp";
        public static readonly String Icon = "icon";

        // Constructors.
        public Image()
                        {
                                this.width = 0;
                                this.height = 0;
                                this.pixelFormat = PixelFormat.Undefined;
                                this.numFrames = 0;
                                this.frames = null;
                                this.format = null;
                                this.palette = null;
                                this.transparentPixel = -1;
                        }
        public Image(int width, int height, PixelFormat pixelFormat)
                        {
                                this.width = width;
                                this.height = height;
                                this.pixelFormat = pixelFormat;
                                this.numFrames = 0;
                                this.frames = null;
                                this.format = null;
                                this.palette = null;
                                this.transparentPixel = -1;
                        }
        private Image(Image image)
                        {
                                this.width = image.width;
                                this.height = image.height;
                                this.pixelFormat = image.pixelFormat;
                                this.numFrames = image.numFrames;
                                if(image.frames != null)
                                {
                                        int frame;
                                        this.frames = new Frame 
[this.numFrames];
                                        for(frame = 0; frame < this.numFrames; 
++frame)
                                        {
                                                this.frames[frame] =
                                                        
image.frames[frame].CloneFrame(this);
                                        }
                                }
                                this.format = image.format;
                                if(image.palette != null)
                                {
                                        this.palette = 
(int[])(image.palette.Clone());
                                }
                                this.transparentPixel = image.transparentPixel;
                        }

        // Destructor.
        ~Image()
                        {
                                Dispose(false);
                        }

        // Get or set the image's overall properties.  The individual frames
        // may have different properties from the ones stated here.
        public int Width
                        {
                                get
                                {
                                        return width;
                                }
                                set
                                {
                                        width = value;
                                }
                        }
        public int Height
                        {
                                get
                                {
                                        return height;
                                }
                                set
                                {
                                        height = value;
                                }
                        }
        public int NumFrames
                        {
                                get
                                {
                                        return numFrames;
                                }
                        }
        public PixelFormat PixelFormat
                        {
                                get
                                {
                                        return pixelFormat;
                                }
                                set
                                {
                                        pixelFormat = value;
                                }
                        }
        public String LoadFormat
                        {
                                get
                                {
                                        // Format the image was loaded in (e.g. 
"jpeg").
                                        // Returns a null value if created 
in-memory.
                                        return format;
                                }
                                set
                                {
                                        format = value;
                                }
                        }
        public int[] Palette
                        {
                                get
                                {
                                        // The palette for indexed images, null 
if an RGB image.
                                        return palette;
                                }
                                set
                                {
                                        palette = value;
                                }
                        }
        public int TransparentPixel
                        {
                                get
                                {
                                        // Index into "Palette" of the 
transparent pixel value.
                                        // Returns -1 if there is no 
transparent pixel specified.
                                        return transparentPixel;
                                }
                                set
                                {
                                        transparentPixel = value;
                                }
                        }

        // Add a new frame to this image.
        public Frame AddFrame()
                        {
                                return AddFrame(width, height, pixelFormat);
                        }
        public Frame AddFrame(int width, int height, PixelFormat pixelFormat)
                        {
                                Frame frame = new Frame(this, width, height, 
pixelFormat);
                                frame.Palette = palette;
                                frame.TransparentPixel = transparentPixel;
                                if(frames == null)
                                {
                                        frames = new Frame[] {frame};
                                        numFrames = 1;
                                }
                                else
                                {
                                        Frame[] newFrames = new Frame 
[numFrames + 1];
                                        Array.Copy(frames, 0, newFrames, 0, 
numFrames);
                                        frames = newFrames;
                                        frames[numFrames] = frame;
                                        ++numFrames;
                                }
                                return frame;
                        }

        // Clone this object.
        public Object Clone()
                        {
                                return new Image(this);
                        }

        // Dispose of this object.
        public void Dispose()
                        {
                                Dispose(true);
                                GC.SuppressFinalize(this);
                        }
        protected virtual void Dispose(bool disposing)
                        {
                                if(frames != null)
                                {
                                        int frame;
                                        for(frame = 0; frame < numFrames; 
++frame)
                                        {
                                                frames[frame].Dispose();
                                        }
                                        frames = null;
                                        numFrames = 0;
                                }
                                palette = null;
                                transparentPixel = -1;
                        }

        // Get a particular frame within this image.
        public Frame GetFrame(int frame)
                        {
                                if(frame >= 0 && frame < numFrames && frames != 
null)
                                {
                                        return frames[frame];
                                }
                                else
                                {
                                        return null;
                                }
                        }

        // Determine if it is possible to load a particular format.
        public static bool CanLoadFormat(String format)
                        {
                                // TODO: other formats
                                return (format == Bmp);
                        }

        // Determine if it is possible to save a particular format.
        public static bool CanSaveFormat(String format)
                        {
                                // TODO: other formats
                                return (format == Bmp);
                        }

        // Load an image from a stream into this object.  This will
        // throw "FormatException" if the format could not be loaded.
        public void Load(String filename)
                        {
                                Stream stream = new FileStream
                                        (filename, FileMode.Open, 
FileAccess.Read);
                                try
                                {
                                        Load(stream);
                                }
                                finally
                                {
                                        stream.Close();
                                }
                        }
        public void Load(Stream stream)
                        {
                                // Read the first 4 bytes from the stream to 
determine
                                // what kind of image we are loading.
                                byte[] magic = new byte [4];
                                stream.Read(magic, 0, 4);
                                if(magic[0] == (byte)'B' && magic[1] == 
(byte)'M')
                                {
                                        BmpReader.Load(stream, this);
                                }
                                else if(magic[0] == 0 && magic[1] == 0 &&
                                                magic[2] == 1 && magic[3] == 0)
                                {
                                        // TODO: this is an icon file
                                }
                                // TODO: other formats
                                throw new FormatException();
                        }

        // Save this image to a stream, in a particular format.
        // If the format is not specified, it defaults to "png".
        public void Save(String filename)
                        {
                                Stream stream = new FileStream
                                        (filename, FileMode.Create, 
FileAccess.Write);
                                try
                                {
                                        Save(stream, null);
                                }
                                finally
                                {
                                        stream.Close();
                                }
                        }
        public void Save(String filename, String format)
                        {
                                Stream stream = new FileStream
                                        (filename, FileMode.Create, 
FileAccess.Write);
                                try
                                {
                                        Save(stream, format);
                                }
                                finally
                                {
                                        stream.Close();
                                }
                        }
        public void Save(Stream stream)
                        {
                                Save(stream, null);
                        }
        public void Save(Stream stream, String format)
                        {
                                // Select a default format for the save.
                                if(format == null)
                                {
                                        format = Bmp;
                                }

                                // Determine how to save the image.
                                if(format == Bmp)
                                {
                                        BmpWriter.Save(stream, this);
                                }
                                // TODO: other image formats
                        }

        // Set the pixel format based on a bit count value.
        public void SetBitCount(int bitCount)
                        {
                                switch(bitCount)
                                {
                                        case 1:
                                                PixelFormat = 
PixelFormat.Format1bppIndexed; break;
                                        case 4:
                                                PixelFormat = 
PixelFormat.Format4bppIndexed; break;
                                        case 8:
                                                PixelFormat = 
PixelFormat.Format8bppIndexed; break;
                                        case 15:
                                                PixelFormat = 
PixelFormat.Format16bppRgb555; break;
                                        case 16:
                                                PixelFormat = 
PixelFormat.Format16bppRgb565; break;
                                        case 24:
                                                PixelFormat = 
PixelFormat.Format24bppRgb; break;
                                }
                        }

}; // class Image

}; // namespace DotGNU.Images

--- NEW FILE ---

.PHONY: DotGNU.Images.dll

all-local: DotGNU.Images.dll

DotGNU.Images.dll:
        "$(CSANT)" $(CSANT_FLAGS) -f DotGNU.Images.build all

CLEANFILES = DotGNU.Images.dll

pnetassembliesdir = $(libdir)/cscc/lib
pnetassemblies_DATA = DotGNU.Images.dll

--- NEW FILE ---
/*
 * PixelFormat.cs - Implementation of the "DotGNU.Images.PixelFormat" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software, you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY, without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program, if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace DotGNU.Images
{

// These values are intended to match "System.Drawing.Imaging.PixelFormat".
public enum PixelFormat
{
        Undefined                               = 0x00000000,
        DontCare                                = 0x00000000,
        Max                                             = 0x0000000F,
        Indexed                                 = 0x00010000,
        Gdi                                             = 0x00020000,
        Format16bppRgb555               = 0x00021005,
        Format16bppRgb565               = 0x00021006,
        Format24bppRgb                  = 0x00021808,
        Format32bppRgb                  = 0x00022009,
        Format1bppIndexed               = 0x00030101,
        Format4bppIndexed               = 0x00030402,
        Format8bppIndexed               = 0x00030803,
        Alpha                                   = 0x00040000,
        Format16bppArgb1555             = 0x00061007,
        PAlpha                                  = 0x00080000,
        Format32bppPArgb                = 0x000E200B,
        Extended                                = 0x00100000,
        Format16bppGrayScale    = 0x00101004,
        Format48bppRgb                  = 0x0010300C,
        Format64bppPArgb                = 0x001C400E,
        Canonical                               = 0x00200000,
        Format32bppArgb                 = 0x0026200A,
        Format64bppArgb                 = 0x0034400D

}; // enum PixelFormat

}; // namespace DotGNU.Images

--- NEW FILE ---
/*
 * RotateFlipType.cs - Implementation of the
 *                      "DotGNU.Images.RotateFlipType" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace DotGNU.Images
{

// These values are intended to match "System.Drawing.RotateFlipType".
public enum RotateFlipType
{
        RotateNoneFlipNone      = 0,
        Rotate90FlipNone        = 1,
        Rotate180FlipNone       = 2,
        Rotate270FlipNone       = 3,
        RotateNoneFlipX         = 4,
        Rotate90FlipX           = 5,
        Rotate180FlipX          = 6,
        Rotate270FlipX          = 7,

        Rotate180FlipXY         = RotateNoneFlipNone,
        Rotate270FlipXY         = Rotate90FlipNone,
        RotateNoneFlipXY        = Rotate180FlipNone,
        Rotate90FlipXY          = Rotate270FlipNone,
        Rotate180FlipY          = RotateNoneFlipX,
        Rotate270FlipY          = Rotate90FlipX,
        RotateNoneFlipY         = Rotate180FlipX,
        Rotate90FlipY           = Rotate270FlipX

}; // enum RotateFlipType
        
}; // namespace System.Drawing

--- NEW FILE ---
/*
 * Utils.cs - Implementation of the "DotGNU.Images.Utils" class.
 *
 * Copyright (C) 2003  Southern Storm Software, Pty Ltd.
 *
 * This program is free software, you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY, without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program, if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace DotGNU.Images
{

using System;
using System.IO;

internal sealed class Utils
{
        // Read a little-endian 16-bit integer value from a buffer.
        public static int ReadUInt16(byte[] buffer, int offset)
                        {
                                return ((buffer[offset]) | (buffer[offset + 1] 
<< 8));
                        }

        // Read a little-endian 32-bit integer value from a buffer.
        public static int ReadInt32(byte[] buffer, int offset)
                        {
                                return ((buffer[offset]) |
                                                (buffer[offset + 1] << 8) |
                                                (buffer[offset + 2] << 16) |
                                                (buffer[offset + 3] << 24));
                        }

        // Read a BGR value from a buffer.
        public static int ReadBGR(byte[] buffer, int offset)
                        {
                                return ((buffer[offset]) |
                                                (buffer[offset + 1] << 8) |
                                                (buffer[offset + 2] << 16));
                        }

        // Write a little-endian 16-bit integer value to a buffer.
        public static void WriteUInt16(byte[] buffer, int offset, int value)
                        {
                                buffer[offset] = (byte)value;
                                buffer[offset + 1] = (byte)(value >> 8);
                        }

        // Write a little-endian 32-bit integer value to a buffer.
        public static void WriteInt32(byte[] buffer, int offset, int value)
                        {
                                buffer[offset] = (byte)value;
                                buffer[offset + 1] = (byte)(value >> 8);
                                buffer[offset + 2] = (byte)(value >> 16);
                                buffer[offset + 3] = (byte)(value >> 24);
                        }

        // Write a BGR value to a buffer as an RGBQUAD.
        public static void WriteBGR(byte[] buffer, int offset, int value)
                        {
                                buffer[offset] = (byte)value;
                                buffer[offset + 1] = (byte)(value >> 8);
                                buffer[offset + 2] = (byte)(value >> 16);
                                buffer[offset + 3] = (byte)0;
                        }

        // Perform a forward-seek on a stream, even on streams that
        // cannot support seeking properly.  "current" is the current
        // position in the stream, and "offset" is the desired offset.
        public static void Seek(Stream stream, long current, long offset)
                        {
                                if(offset < current)
                                {
                                        throw new FormatException();
                                }
                                else if(offset == current)
                                {
                                        return;
                                }
                                if(stream.CanSeek)
                                {
                                        stream.Seek(offset, SeekOrigin.Begin);
                                }
                                else
                                {
                                        byte[] buffer = new byte [1024];
                                        int len;
                                        while(current < offset)
                                        {
                                                if((offset - current) < 1024)
                                                {
                                                        len = (int)(offset - 
current);
                                                }
                                                else
                                                {
                                                        len = 1024;
                                                }
                                                if(stream.Read(buffer, 0, len) 
!= len)
                                                {
                                                        throw new 
FormatException();
                                                }
                                        }
                                }
                        }

        // Convert a pixel format into a stride value.
        public static int FormatToStride(PixelFormat pixelFormat, int width)
                        {
                                int stride;
                                switch(pixelFormat)
                                {
                                        case PixelFormat.Format1bppIndexed:
                                                stride = (width + 7) / 8;
                                                break;

                                        case PixelFormat.Format4bppIndexed:
                                                stride = (width + 1) / 2;
                                                break;

                                        case PixelFormat.Format8bppIndexed:
                                                stride = width;
                                                break;

                                        case PixelFormat.Format16bppRgb555:
                                        case PixelFormat.Format16bppRgb565:
                                        case PixelFormat.Format16bppArgb1555:
                                        case PixelFormat.Format16bppGrayScale:
                                                stride = width * 2;
                                                break;

                                        case PixelFormat.Format24bppRgb:
                                                stride = width * 3;
                                                break;

                                        case PixelFormat.Format32bppRgb:
                                        case PixelFormat.Format32bppPArgb:
                                        case PixelFormat.Format32bppArgb:
                                        default:
                                                stride = width * 4;
                                                break;

                                        case PixelFormat.Format48bppRgb:
                                                stride = width * 6;
                                                break;

                                        case PixelFormat.Format64bppPArgb:
                                        case PixelFormat.Format64bppArgb:
                                                stride = width * 8;
                                                break;
                                }
                                return ((stride + 3) & ~3);
                        }

        // Convert a pixel format into a bit count value.
        public static int FormatToBitCount(PixelFormat pixelFormat)
                        {
                                switch(pixelFormat)
                                {
                                        case PixelFormat.Format1bppIndexed:     
return 1;

                                        case PixelFormat.Format4bppIndexed:     
return 4;

                                        case PixelFormat.Format8bppIndexed:     
return 8;

                                        case PixelFormat.Format16bppRgb555:
                                        case PixelFormat.Format16bppRgb565:
                                        case PixelFormat.Format16bppArgb1555:
                                        case PixelFormat.Format16bppGrayScale:  
return 16;

                                        case PixelFormat.Format24bppRgb:        
        return 24;

                                        case PixelFormat.Format32bppRgb:
                                        case PixelFormat.Format32bppPArgb:
                                        case PixelFormat.Format32bppArgb:       
        return 32;

                                        case PixelFormat.Format48bppRgb:        
        return 48;

                                        case PixelFormat.Format64bppPArgb:
                                        case PixelFormat.Format64bppArgb:       
        return 64;

                                        default:                                
                                return 1;
                                }
                        }

}; // class Utils

}; // namespace DotGNU.Images





reply via email to

[Prev in Thread] Current Thread [Next in Thread]