private void LoadTexture(string filename, uint texture) { if (Il.ilLoadImage(filename)) //載入影像檔 { int BitsPerPixel = Il.ilGetInteger(Il.IL_IMAGE_BITS_PER_PIXEL); //取得儲存每個像素的位元數 int Depth = Il.ilGetInteger(Il.IL_IMAGE_DEPTH); //取得影像的深度值 Ilu.iluScale(512, 512, Depth); //將影像大小縮放為2的指數次方 Ilu.iluFlipImage(); //顛倒影像 Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture); //連結紋理物件 //設定紋理參數 Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_REPEAT); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_REPEAT); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR); //建立紋理物件 if (BitsPerPixel == 24) { Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGB, 512, 512, 0, Il.ilGetInteger(Il.IL_IMAGE_FORMAT), Il.ilGetInteger(Il.IL_IMAGE_TYPE), Il.ilGetData()); } if (BitsPerPixel == 32) { Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA, 512, 512, 0, Il.ilGetInteger(Il.IL_IMAGE_FORMAT), Il.ilGetInteger(Il.IL_IMAGE_TYPE), Il.ilGetData()); } } else { // 若檔案無法開啟,顯示錯誤訊息 string message = "Cannot open file " + filename + "."; MessageBox.Show(message, "Image file open error!!!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
public static bool ReshapeToPowersOf2AndSave(Stream input, int size, string outputFileName) { InitializeIL(); InitializeILU(); // load the image ImageData data = new ImageData(); int imageID; // create and bind a new image Il.ilGenImages(1, out imageID); Il.ilBindImage(imageID); // create a temp buffer and copy the stream into it byte[] buffer = new byte[size]; int bytesRead = 0, byteOffset = 0, bytes = buffer.Length; do { bytesRead = input.Read(buffer, byteOffset, bytes); bytes -= bytesRead; byteOffset += bytesRead; } while (bytes > 0 && bytesRead > 0); Il.ilLoadL(Il.IL_TYPE_UNKNOWN, buffer, buffer.Length); // check errors int ilError = Il.ilGetError(); if (ilError != Il.IL_NO_ERROR) { throw new AxiomException("Error while loading image data: '{0}'", Ilu.iluErrorString(ilError)); } // determine dimensions & compute powers-of-2 reshape if needed int width = Il.ilGetInteger(Il.IL_IMAGE_WIDTH); int height = Il.ilGetInteger(Il.IL_IMAGE_HEIGHT); int newWidth = (int)Math.Pow(2, Math.Floor(Math.Log(width, 2))); int newHeight = (int)Math.Pow(2, Math.Floor(Math.Log(height, 2))); if (width != newWidth || height != newHeight) { // reshape // set the scale function filter & scale Ilu.iluImageParameter(Ilu.ILU_FILTER, Ilu.ILU_BILINEAR); // .ILU_SCALE_BSPLINE); Ilu.iluScale(newWidth, newHeight, 1); } // save Il.ilSetInteger(Il.IL_JPG_QUALITY, 50); Il.ilSaveImage(outputFileName); // drop image Il.ilDeleteImages(1, ref imageID); return(true); }
public string convertJPEG(string jpgs) { int image; Il.ilGenImages(1, out image); Il.ilBindImage(image); //Return if it f***s up the loading. if (!Il.ilLoadImage(jpgs)) { return(null); } //Convert image to closest power of 2, creds to http://thecsharpmind.blogspot.se/2006/11/using-tao-and-devil-to-load-opengl.html int image_width = Il.ilGetInteger(Il.IL_IMAGE_WIDTH); int image_height = Il.ilGetInteger(Il.IL_IMAGE_HEIGHT); int image_depth = Il.ilGetInteger(Il.IL_IMAGE_DEPTH); int texture_width = NextPowerOfTwo(image_width); int texture_height = NextPowerOfTwo(image_height); if ((texture_width != image_width) || (texture_height != image_height)) { Ilu.iluScale(texture_height, texture_height, image_depth); } //Now simply save this as a dds file with the .dds extension. if (Il.ilSaveImage(jpgs.Split('.')[0] + ".dds")) { Il.ilDeleteImage(image); return(jpgs.Split('.')[0] + ".dds"); } else { Il.ilDeleteImage(image); return(null); } }
private void LoadTexture(string filename, uint texture) { int TextureSizeX, TextureSizeY; if (Il.ilLoadImage(filename)) { int Width = Il.ilGetInteger(Il.IL_IMAGE_WIDTH); int Height = Il.ilGetInteger(Il.IL_IMAGE_HEIGHT); int BitsPerPixel = Il.ilGetInteger(Il.IL_IMAGE_BITS_PER_PIXEL); //Ilu.iluImageParameter(Ilu.ILU_FILTER, Ilu.ILU_BILINEAR); //Ilu.iluImageParameter(Ilu.ILU_FILTER, Ilu.ILU_NEAREST); if (Width > 128) { TextureSizeX = 128; } else { TextureSizeX = 64; } if (Height > 128) { TextureSizeY = 128; } else { TextureSizeY = 64; } Ilu.iluScale(TextureSizeX, TextureSizeY, 1); string ext = null; int n = filename.LastIndexOf("."); if (n > 0) { ext = filename.Substring(n + 1); } if (ext.ToLower() != "bmp") { Ilu.iluFlipImage(); } // bind to our texture object we just created Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture); // set the filtering and wrap modes for the texture Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_REPEAT); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_REPEAT); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR); if (BitsPerPixel == 24) { Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGB, TextureSizeX, TextureSizeY, 0, Il.ilGetInteger(Il.IL_IMAGE_FORMAT), Il.ilGetInteger(Il.IL_IMAGE_TYPE), Il.ilGetData()); } if (BitsPerPixel == 32) { Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA, TextureSizeX, TextureSizeY, 0, Il.ilGetInteger(Il.IL_IMAGE_FORMAT), Il.ilGetInteger(Il.IL_IMAGE_TYPE), Il.ilGetData()); } } else { string message = "Cannot open file " + filename + "."; MessageBox.Show(message, "Image file open error!!!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
/// <summary> /// We use a separate method for this because calling iluScale directly inline /// was failing on the release build, with the stack getting trashed. /// My(jsw) theory is that there is some problem with the jit compiler, pinvoke, /// or the combination of the two that was causing this problem. /// </summary> /// <param name="scaleX">destination size of the scale operation</param> /// <param name="scaleY">destination size of the scale operation</param> private void DoScale(int scaleX, int scaleY) { // set the scale function filter & scale Ilu.iluImageParameter(Ilu.ILU_FILTER, Ilu.ILU_BILINEAR); // .ILU_SCALE_BSPLINE); Ilu.iluScale(scaleX, scaleY, 1); }