private Color32[] GetColorDataFromCurrentImage() { int width = DevILLoader.ilGetInteger(DevILConstants.IL_IMAGE_WIDTH); // getting image width int height = DevILLoader.ilGetInteger(DevILConstants.IL_IMAGE_HEIGHT); // and height Debug.Log("Image resolution: w = " + width + "; h = " + height); /* how much memory will we need? */ int memoryNeeded = width * height * 4; /* We multiply by 4 here because we want 4 components per pixel */ byte[] imageColorData = new byte[memoryNeeded]; /* finally get the image data */ DevILLoader.ilCopyPixels(0, 0, 0, (uint)width, (uint)height, 1, DevILConstants.IL_RGBA, DevILConstants.IL_UNSIGNED_BYTE, imageColorData); if (imageColorData.Length <= 0) { return(null); } // create colors from color data Color32[] texColors = new Color32[imageColorData.Length / 4]; for (int i = 0, j = 0; i < imageColorData.Length; i += 4, ++j) { texColors[j].r = imageColorData[i]; texColors[j].g = imageColorData[i + 1]; texColors[j].b = imageColorData[i + 2]; texColors[j].a = imageColorData[i + 3]; } return(texColors); }
private Texture2D LoadImageFromData(byte[] imageData) { const int cNumImages = 1; uint[] handles = new uint[cNumImages]; Texture2D resTexture = null; /* First we initialize the library. */ /*Do not forget that... */ DevILLoader.ilInit(); /* We want all images to be loaded in a consistent manner */ DevILLoader.ilEnable(DevILConstants.IL_ORIGIN_SET); /* In the next section, we load one image */ DevILLoader.ilGenImages(cNumImages, handles); DevILLoader.ilBindImage(handles[0]); // //uint res = PluginLoader.ilLoadL(DevILConstants.IL_PNG, imageData, (uint)imageData.Length); bool res = DevILLoader.ilLoadL(DevILConstants.IL_TYPE_UNKNOWN, imageData, (uint)imageData.Length); if (!res) { Debug.LogWarning("Error! Cannot load image from data"); return(resTexture); } /* Let's spy on it a little bit */ int width = DevILLoader.ilGetInteger(DevILConstants.IL_IMAGE_WIDTH); // getting image width int height = DevILLoader.ilGetInteger(DevILConstants.IL_IMAGE_HEIGHT); // and height Debug.Log("Base image resolution: w = " + width + "; h = " + height); // create result texture resTexture = new Texture2D(width, height, TextureFormat.RGBA32, true); // Color32[] texColors = GetColorDataFromCurrentImage(); // set first mip map resTexture.SetPixels32(texColors, 0); // now, try to set another levels of bitmap { uint currMipMapLevel = 1; const uint cMaxMipMapLevel = 15; while (currMipMapLevel < cMaxMipMapLevel) { res = DevILLoader.ilActiveMipmap(currMipMapLevel); Debug.Log("res = " + res + " currMipMapLevel = " + currMipMapLevel); if (!res) { break; } // texColors = GetColorDataFromCurrentImage(); Debug.Log("currMipMapLevel = " + currMipMapLevel); // set next mip map resTexture.SetPixels32(texColors, (int)currMipMapLevel); ++currMipMapLevel; // restore base image DevILLoader.ilBindImage(handles[0]); } } resTexture.Apply(true); //resTexture.Apply(false); // show this to ven! /* Finally, clean the mess! */ DevILLoader.ilDeleteImages(cNumImages, handles); return(resTexture); }