BMPCodec.cs :  » Game » RealmForge » Axiom » Media » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » RealmForge 
RealmForge » Axiom » Media » BMPCodec.cs
using System;
using Axiom.Core;
using Tao.DevIl;

namespace Axiom.Media{
  /// <summary>
  ///    BMP image file codec.
  /// </summary>
  public class BMPCodec : ILImageCodec {
    public BMPCodec() {
    }

        #region ILImageCodec Implementation

        public override object Decode(System.IO.Stream input, System.IO.Stream output, params object[] args) {
            ImageData data = new ImageData();

            int imageID;
            int format, bytesPerPixel;

            // create and bind a new image
            Il.ilGenImages(1, out imageID);
            Il.ilBindImage(imageID);

            // create a temp buffer and write the stream into it
            byte[] buffer = new byte[input.Length];
            input.Read(buffer, 0, buffer.Length);

            // load the data into DevIL
            Il.ilLoadL(this.ILType, buffer, buffer.Length);

            // check for an error
            int ilError = Il.ilGetError();

            if(ilError != Il.IL_NO_ERROR) {
                throw new AxiomException("Error while decoding image data: '{0}'", Ilu.iluErrorString(ilError));
            }

            // swap the color components
            Ilu.iluSwapColours();

            format = Il.ilGetInteger(Il.IL_IMAGE_FORMAT);
            bytesPerPixel = Il.ilGetInteger(Il.IL_IMAGE_BYTES_PER_PIXEL);

            // populate the image data
            data.width = Il.ilGetInteger(Il.IL_IMAGE_WIDTH);
            data.height = Il.ilGetInteger(Il.IL_IMAGE_HEIGHT);
            data.depth = Il.ilGetInteger(Il.IL_IMAGE_DEPTH);
            data.numMipMaps = Il.ilGetInteger(Il.IL_NUM_MIPMAPS);
            data.format = ConvertFromILFormat(format, bytesPerPixel);
            data.size = data.width * data.height * bytesPerPixel;

            // get the decoded data
            buffer = new byte[data.size];
            IntPtr ptr = Il.ilGetData();

            // copy the data into the byte array
            unsafe {
                byte* pBuffer = (byte*)ptr;
                for(int i = 0; i < buffer.Length; i++) {
                    buffer[i] = pBuffer[i];
                }
            }

            // write the decoded data to the output stream
            output.Write(buffer, 0, buffer.Length);

            // we won't be needing this anymore
            Il.ilDeleteImages(1, ref imageID);

            return data;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="dest"></param>
        /// <param name="args"></param>
        public override void Encode(System.IO.Stream source, System.IO.Stream dest, params object[] args) {
            throw new NotImplementedException("BMP encoding is not yet implemented.");
        }

        /// <summary>
        ///    Returns the BMP file extension.
        /// </summary>
        public override String Type {
            get {
                return "bmp";
            }
        }


        /// <summary>
        ///    Returns BMP enum.
        /// </summary>
        public override int ILType {
            get {
                return Il.IL_BMP;
            }
        }

        #endregion ILImageCodec Implementation
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.