Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Drawing/Platform/OpenGL/ILOGLTextureStorage.cs @ 11194

Last change on this file since 11194 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 8.7 KB
Line 
1///
2///    This file is part of ILNumerics Community Edition.
3///
4///    ILNumerics Community Edition - high performance computing for applications.
5///    Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
6///
7///    ILNumerics Community Edition is free software: you can redistribute it and/or modify
8///    it under the terms of the GNU General Public License version 3 as published by
9///    the Free Software Foundation.
10///
11///    ILNumerics Community Edition is distributed in the hope that it will be useful,
12///    but WITHOUT ANY WARRANTY; without even the implied warranty of
13///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14///    GNU General Public License for more details.
15///
16///    You should have received a copy of the GNU General Public License
17///    along with ILNumerics Community Edition. See the file License.txt in the root
18///    of your distribution package. If not, see <http://www.gnu.org/licenses/>.
19///
20///    In addition this software uses the following components and/or licenses:
21///
22///    =================================================================================
23///    The Open Toolkit Library License
24///   
25///    Copyright (c) 2006 - 2009 the Open Toolkit library.
26///   
27///    Permission is hereby granted, free of charge, to any person obtaining a copy
28///    of this software and associated documentation files (the "Software"), to deal
29///    in the Software without restriction, including without limitation the rights to
30///    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
31///    the Software, and to permit persons to whom the Software is furnished to do
32///    so, subject to the following conditions:
33///
34///    The above copyright notice and this permission notice shall be included in all
35///    copies or substantial portions of the Software.
36///
37///    =================================================================================
38///   
39
40using System;
41using System.Collections.Generic;
42using System.Text;
43using ILNumerics.Drawing.Labeling;
44using ILNumerics.Misc;
45using OpenTK.Graphics.OpenGL;
46using OpenTK.Graphics;
47using System.Drawing;
48using System.Drawing.Imaging;
49using ILNumerics.Exceptions;
50
51namespace ILNumerics.Drawing.Platform.OpenGL {
52    /// <summary>
53    /// OpenGL implementation of ILTextureStorage
54    /// </summary>
55    public class ILOGLTextureStorage : ILTextureStorage {
56
57        #region attributes
58        new int [] m_tmpData = new int[20*20];
59        #endregion
60
61        #region constructor
62        public ILOGLTextureStorage (int width, int height) : base (width, height) { }
63        #endregion
64
65        #region abstract overrides
66        /// <summary>
67        /// Select this storage to be current in GL
68        /// </summary>
69        public override void MakeCurrent() {
70            GL.BindTexture(TextureTarget.Texture2D,m_textureId); 
71            //System.Diagnostics.Debug.WriteLine(String.Format("ILOGLTextureStorage: switched to ID#{0}",m_textureId));
72        }
73        /// <summary>
74        /// initialize texture sheet
75        /// </summary>
76        protected override void InitTexture() {
77            GL.GenTextures(1, out m_textureId);
78            //System.Diagnostics.Debug.WriteLine(String.Format("ILTextureStorage{2}: TextureID={0}, Thread={1}"
79            //        ,m_textureId, System.Threading.Thread.CurrentThread.ManagedThreadId,this.GetHashCode()));
80            GL.BindTexture(TextureTarget.Texture2D, m_textureId);
81            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)All.Repeat);
82            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)All.Repeat);
83            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear);
84            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear);
85            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Alpha, m_width, m_height, 0,
86                OpenTK.Graphics.PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
87            // todo: error handling!
88        }
89        /// <summary>
90        /// store item in texture sheet in GL
91        /// </summary>
92        /// <param name="bmp">new item bitmap data, ARGB format</param>
93        /// <param name="bmpRectF">rectangle used in bitmap</param>
94        /// <param name="rect">rectangle specifying area to store the data into the texture sheet,
95        /// integer pixels coords: range from 0...m_width|m_height (e.g. '512')</param>
96        protected override void Store(Bitmap bmp,RectangleF bmpRectF, RectangleF rect) {
97            if (rect.Size.Width == 0 || rect.Size.Height == 0) return;
98            if (m_disposed)
99                throw new InvalidOperationException("The texture storage has been disposed already! (Need to create a new storage)");
100            if (bmp == null)
101                throw new ILArgumentException("Bitmap argument must not be null!");
102            Rectangle bmpRect = Rectangle.Ceiling(bmpRectF);
103            if (bmp.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb) {
104                bmp = bmp.Clone(bmpRect,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
105                bmpRect.X = 0; bmpRect.Y = 0;
106            }
107            GL.BindTexture(TextureTarget.Texture2D, m_textureId);
108            BitmapData bmp_data = bmp.LockBits(new Rectangle(0,0,bmp.Width,bmp.Height), ImageLockMode.ReadOnly,System.Drawing.Imaging.PixelFormat.Format32bppArgb);
109            // setup OpenGL pixel store
110            GL.PixelStore(PixelStoreParameter.UnpackAlignment,1.0f);
111            GL.PixelStore(PixelStoreParameter.UnpackRowLength,bmp.Width);
112            GL.PixelStore(PixelStoreParameter.UnpackSkipRows,bmpRect.Y);
113            GL.PixelStore(PixelStoreParameter.UnpackSkipPixels,bmpRect.X);
114            GL.TexSubImage2D(TextureTarget.Texture2D, 0, (int)rect.Left, (int)rect.Top, bmpRect.Width, bmpRect.Height,
115                     OpenTK.Graphics.PixelFormat.Rgba, PixelType.UnsignedByte, bmp_data.Scan0);
116#region DEBUGGING only
117#if EXPORTBMP
118            // must copy to new data array
119            if (m_tmpData.Length < bmpRect.Height * bmpRect.Width) {
120                if (m_tmpData != null)
121                    ILMemoryPool.Pool.RegisterObject(m_tmpData);
122                m_tmpData = ILMemoryPool.Pool.New<int>(bmpRect.Height * bmpRect.Width);   
123            }
124            unsafe {
125            fixed (int* tmpDataP = m_tmpData) {
126                int* pOut = tmpDataP;
127                int* pIn;
128                int* pInEnd;
129                for (int r = 0; r < bmpRect.Height; r++) {
130                    //pIn = (int*)bitmap_data.Scan0 + (r+(int)location.Y) * data.Width + (int)location.X;
131                    pIn = ((int*)bmp_data.Scan0) + r * (bmp_data.Stride / 4) + bmpRect.X;
132                    pInEnd = pIn + bmpRect.Width;
133                    while (pIn < pInEnd) {
134                        *(pOut++) = *(pIn++);     
135                    }
136                }
137                //GL.TexSubImage2D(TextureTarget.Texture2D, 0, (int)rect.Left, (int)rect.Top, bmpRect.Width, bmpRect.Height,
138                //     OpenTK.Graphics.PixelFormat.Rgba, PixelType.UnsignedByte, (IntPtr)tmpDataP);
139                Bitmap outB = new Bitmap(bmpRect.Width, bmpRect.Height);
140                for (int i = 0; i < bmpRect.Width * bmpRect.Height; i++)
141                    outB.SetPixel(i%bmpRect.Width,(int)(i/bmpRect.Width),Color.FromArgb(m_tmpData[i]));
142                outB.Save("EXPORTBMP_textureCached.bmp",ImageFormat.Bmp);
143                outB.Dispose();
144                string dbTrOut = String.Format("usedArea:{0}:{1}:{2}:{3} data:{4} bmp_data: {5}:{6} stride:{7} Loc:{8}"
145                                ,bmpRect.Left, bmpRect.Top, bmpRect.Width, bmpRect.Height
146                                ,bmp.Size.ToString()
147                                ,bmp_data.Width,bmp_data.Height
148                                ,bmp_data.Stride
149                                ,bmpRectF.Location);
150
151                Console.Out.WriteLine(dbTrOut);
152                System.Diagnostics.Debug.WriteLine(dbTrOut);
153            }}
154#endif
155#endregion
156            bmp.UnlockBits(bmp_data);
157        }
158        /// <summary>
159        /// dispose off the texture storage's ressources
160        /// </summary>
161        /// <param name="manual"></param>
162        public override void Dispose(bool manual) {
163            if (!m_disposed) {
164                if (manual) {
165                    // free texture from OGL
166                    GL.DeleteTextures(1, ref m_textureId);
167                }
168                m_disposed = true;
169            }
170        }
171        #endregion
172
173    }
174}
Note: See TracBrowser for help on using the repository browser.