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); }
/// <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); }