public static TextureInfoWrapper DDSToTexture(TexInfo Texture, bool mipmaps, bool isCompressed, bool hasAlpha) { TextureConverter.InitImageBuffer(); FileStream imgStream = new FileStream(Texture.filename, FileMode.Open, FileAccess.Read); imgStream.Position = 0; imgStream.Read(imageBuffer, 0, MAX_IMAGE_SIZE); imgStream.Close(); TextureFormat format = TextureFormat.DXT1; if (hasAlpha && isCompressed) { format = TextureFormat.DXT5; } else if (hasAlpha) { format = TextureFormat.RGBA32; } else if (!isCompressed) { format = TextureFormat.RGB24; } Texture2D newTex = new Texture2D(Texture.width, Texture.height, format, mipmaps); newTex.LoadRawTextureData(imageBuffer); newTex.Apply(false, Texture.makeNotReadable); newTex.name = Texture.name; TextureInfoWrapper newTexInfo = new TextureInfoWrapper(newTex, Texture.isNormalMap, !Texture.makeNotReadable, isCompressed); newTexInfo.name = Texture.name; return(newTexInfo); }
protected void Awake() { if (HighLogic.LoadedScene == GameScenes.LOADING) { PopulateConfig(); LoadTextures(); } else if (HighLogic.LoadedScene == GameScenes.MAINMENU && !Compressed) { Update(); Compressed = true; foreach (GameDatabase.TextureInfo Texture in GameDatabase.Instance.databaseTexture) { Texture2D texture = Texture.texture; Log("--------------------------------------------------------"); Log("Name: " + texture.name); Log("Format: " + texture.format.ToString()); Log("MipMaps: " + texture.mipmapCount.ToString()); Log("Size: " + texture.width.ToString() + "x" + texture.height); Log("Readable: " + Texture.isReadable); } long bSaved = memorySaved; long kbSaved = (long)(bSaved / 1024f); long mbSaved = (long)(kbSaved / 1024f); Log("Memory Saved : " + bSaved.ToString() + "B"); Log("Memory Saved : " + kbSaved.ToString() + "kB"); Log("Memory Saved : " + mbSaved.ToString() + "MB"); TextureConverter.DestroyImageBuffer(); Resources.UnloadUnusedAssets(); System.GC.Collect(); } }
private static TextureInfoWrapper RebuildCache(TexInfo Texture, bool compress, bool mipmaps) { Texture.loadOriginalFirst = true; ActiveTextureManagement.DBGLog("Loading texture..."); TextureConverter.GetReadable(Texture, mipmaps); ActiveTextureManagement.DBGLog("Texture loaded."); TextureInfoWrapper cacheTexture = Texture.texture; Texture2D tex = cacheTexture.texture; String textureName = cacheTexture.name; String cacheFile = KSPUtil.ApplicationRootPath + "GameData/ActiveTextureManagement/textureCache/" + textureName; ActiveTextureManagement.DBGLog("Rebuilding Cache... " + Texture.name); ActiveTextureManagement.DBGLog("Saving cache file " + cacheFile + ".imgcache"); tex.Apply(mipmaps); Color32[] colors = tex.GetPixels32(); bool hasAlpha = TextureConverter.WriteTo(tex, cacheFile + ".imgcache", compress); String originalTextureFile = Texture.filename; String cacheConfigFile = cacheFile + ".tcache"; ActiveTextureManagement.DBGLog("Created Config for" + originalTextureFile); String hashString = GetMD5String(originalTextureFile); ConfigNode config = new ConfigNode(); config.AddValue("md5", hashString); ActiveTextureManagement.DBGLog("md5: " + hashString); config.AddValue("orig_format", Path.GetExtension(originalTextureFile)); ActiveTextureManagement.DBGLog("orig_format: " + Path.GetExtension(originalTextureFile)); config.AddValue("orig_width", Texture.width.ToString()); ActiveTextureManagement.DBGLog("orig_width: " + Texture.width.ToString()); config.AddValue("orig_height", Texture.height.ToString()); ActiveTextureManagement.DBGLog("orig_height: " + Texture.height.ToString()); config.AddValue("is_normal", cacheTexture.isNormalMap.ToString()); ActiveTextureManagement.DBGLog("is_normal: " + cacheTexture.isNormalMap.ToString()); config.AddValue("is_compressed", compress); ActiveTextureManagement.DBGLog("is_compressed: " + compress); config.AddValue("width", Texture.resizeWidth.ToString()); ActiveTextureManagement.DBGLog("width: " + Texture.resizeWidth.ToString()); config.AddValue("height", Texture.resizeHeight.ToString()); ActiveTextureManagement.DBGLog("height: " + Texture.resizeHeight.ToString()); config.AddValue("hasAlpha", hasAlpha); ActiveTextureManagement.DBGLog("hasAlpha: " + hasAlpha.ToString()); config.AddValue("hasMipmaps", mipmaps); ActiveTextureManagement.DBGLog("hasMipmaps: " + hasAlpha.ToString()); config.Save(cacheConfigFile); ActiveTextureManagement.DBGLog("Saved Config."); if (compress) { tex.Compress(true); } cacheTexture.isCompressed = compress; tex.Apply(false, Texture.makeNotReadable); cacheTexture.isReadable = !Texture.makeNotReadable; return(cacheTexture); }
public static void IMGToTexture(TexInfo Texture, bool mipmaps, bool isNormalFormat) { TextureInfoWrapper texture = Texture.texture; TextureConverter.InitImageBuffer(); FileStream imgStream = new FileStream(Texture.filename, FileMode.Open, FileAccess.Read); imgStream.Position = 0; imgStream.Read(imageBuffer, 0, MAX_IMAGE_SIZE); imgStream.Close(); Texture2D tex = texture.texture; tex.LoadImage(imageBuffer); bool convertToNormalFormat = texture.isNormalMap && !isNormalFormat ? true : false; bool hasMipmaps = tex.mipmapCount == 1 ? false : true; if (Texture.loadOriginalFirst) { Texture.Resize(tex.width, tex.height); } TextureFormat format = tex.format; if (texture.isNormalMap) { format = TextureFormat.ARGB32; } if (Texture.needsResize) { TextureConverter.Resize(texture, Texture.resizeWidth, Texture.resizeHeight, mipmaps, convertToNormalFormat); } else if (convertToNormalFormat || hasMipmaps != mipmaps || format != tex.format) { Color32[] pixels = tex.GetPixels32(); if (convertToNormalFormat) { for (int i = 0; i < pixels.Length; i++) { pixels[i].a = pixels[i].r; pixels[i].r = pixels[i].g; pixels[i].b = pixels[i].g; } } if (tex.format != format || hasMipmaps != mipmaps) { tex.Resize(tex.width, tex.height, format, mipmaps); } tex.SetPixels32(pixels); tex.Apply(mipmaps); } }
protected void Start() { if (HighLogic.LoadedScene == GameScenes.LOADING) { DatabaseLoaderTexture_ATM.PopulateConfig(); SetupLoaders(); } else if (HighLogic.LoadedScene == GameScenes.MAINMENU && !Compressed) { Update(); Compressed = true; foreach (GameDatabase.TextureInfo Texture in GameDatabase.Instance.databaseTexture) { Texture2D texture = Texture.texture; Log("--------------------------------------------------------"); Log("Name: " + texture.name); Log("Format: " + texture.format.ToString()); Log("MipMaps: " + texture.mipmapCount.ToString()); Log("Size: " + texture.width.ToString() + "x" + texture.height); Log("Readable: " + Texture.isReadable); bool readable = true; try { Texture.texture.GetPixel(0, 0); } catch { readable = false; }; if (readable != Texture.isReadable) { ActiveTextureManagement.DBGLog("Readbility does not match!"); } } long bSaved = memorySaved; long kbSaved = (long)(bSaved / 1024f); long mbSaved = (long)(kbSaved / 1024f); Log("Memory Saved : " + bSaved.ToString() + "B"); Log("Memory Saved : " + kbSaved.ToString() + "kB"); Log("Memory Saved : " + mbSaved.ToString() + "MB"); TextureConverter.DestroyImageBuffer(); Resources.UnloadUnusedAssets(); System.GC.Collect(); } }
public static void TGAToTexture(TexInfo Texture, bool mipmaps) { GameDatabase.TextureInfo texture = Texture.texture; TextureConverter.InitImageBuffer(); FileStream tgaStream = new FileStream(Texture.filename, FileMode.Open, FileAccess.Read); tgaStream.Position = 0; tgaStream.Read(imageBuffer, 0, MAX_IMAGE_SIZE); tgaStream.Close(); byte imgType = imageBuffer[2]; int width = imageBuffer[12] | (imageBuffer[13] << 8); int height = imageBuffer[14] | (imageBuffer[15] << 8); if (Texture.loadOriginalFirst) { Texture.Resize(width, height); } int depth = imageBuffer[16]; bool alpha = depth == 32 ? true : false; TextureFormat texFormat = depth == 32 ? TextureFormat.RGBA32 : TextureFormat.RGB24; if (texture.isNormalMap) { texFormat = TextureFormat.ARGB32; } bool convertToNormalFormat = texture.isNormalMap ? true : false; Texture2D tex = texture.texture; Color32[] colors = new Color32[width * height]; int n = 18; if (imgType == 2) { for (int i = 0; i < width * height; i++) { colors[i].b = imageBuffer[n++]; colors[i].g = imageBuffer[n++]; colors[i].r = imageBuffer[n++]; if (alpha) { colors[i].a = imageBuffer[n++]; } else { colors[i].a = 255; } if (convertToNormalFormat) { colors[i].a = colors[i].r; colors[i].r = colors[i].g; colors[i].b = colors[i].g; } } } else if (imgType == 10) { int i = 0; int run = 0; while (i < width * height) { run = imageBuffer[n++]; if ((run & 0x80) != 0) { run = (run ^ 0x80) + 1; colors[i].b = imageBuffer[n++]; colors[i].g = imageBuffer[n++]; colors[i].r = imageBuffer[n++]; if (alpha) { colors[i].a = imageBuffer[n++]; } else { colors[i].a = 255; } if (convertToNormalFormat) { colors[i].a = colors[i].r; colors[i].r = colors[i].g; colors[i].b = colors[i].g; } i++; for (int c = 1; c < run; c++, i++) { colors[i] = colors[i - 1]; } } else { run += 1; for (int c = 0; c < run; c++, i++) { colors[i].b = imageBuffer[n++]; colors[i].g = imageBuffer[n++]; colors[i].r = imageBuffer[n++]; if (alpha) { colors[i].a = imageBuffer[n++]; } else { colors[i].a = 255; } if (convertToNormalFormat) { colors[i].a = colors[i].r; colors[i].r = colors[i].g; colors[i].b = colors[i].g; } } } } } else { ActiveTextureManagement.DBGLog("TGA format is not supported!"); } if (Texture.needsResize) { colors = TextureConverter.ResizePixels(colors, width, height, Texture.resizeWidth, Texture.resizeHeight); width = Texture.resizeWidth; height = Texture.resizeHeight; } tex.Resize((int)width, (int)height, texFormat, mipmaps); tex.SetPixels32(colors); tex.Apply(mipmaps, false); }
public static void MBMToTexture(TexInfo Texture, bool mipmaps) { GameDatabase.TextureInfo texture = Texture.texture; TextureConverter.InitImageBuffer(); FileStream mbmStream = new FileStream(Texture.filename, FileMode.Open, FileAccess.Read); mbmStream.Position = 4; uint width = 0, height = 0; for (int b = 0; b < 4; b++) { width >>= 8; width |= (uint)(mbmStream.ReadByte() << 24); } for (int b = 0; b < 4; b++) { height >>= 8; height |= (uint)(mbmStream.ReadByte() << 24); } mbmStream.Position = 12; bool convertToNormalFormat = false; if (mbmStream.ReadByte() == 1) { texture.isNormalMap = true; } else { convertToNormalFormat = texture.isNormalMap ? true : false; } mbmStream.Position = 16; int format = mbmStream.ReadByte(); mbmStream.Position += 3; int imageSize = (int)(width * height * 3); TextureFormat texformat = TextureFormat.RGB24; bool alpha = false; if (format == 32) { imageSize += (int)(width * height); texformat = TextureFormat.ARGB32; alpha = true; } if (texture.isNormalMap) { texformat = TextureFormat.ARGB32; } mbmStream.Read(imageBuffer, 0, MAX_IMAGE_SIZE); mbmStream.Close(); Texture2D tex = texture.texture; Color32[] colors = new Color32[width * height]; int n = 0; for (int i = 0; i < width * height; i++) { colors[i].r = imageBuffer[n++]; colors[i].g = imageBuffer[n++]; colors[i].b = imageBuffer[n++]; if (alpha) { colors[i].a = imageBuffer[n++]; } else { colors[i].a = 255; } if (convertToNormalFormat) { colors[i].a = colors[i].r; colors[i].r = colors[i].g; colors[i].b = colors[i].g; } } if (Texture.loadOriginalFirst) { Texture.Resize((int)width, (int)height); } if (Texture.needsResize) { colors = TextureConverter.ResizePixels(colors, (int)width, (int)height, Texture.resizeWidth, Texture.resizeHeight); width = (uint)Texture.resizeWidth; height = (uint)Texture.resizeHeight; } tex.Resize((int)width, (int)height, texformat, mipmaps); tex.SetPixels32(colors); tex.Apply(mipmaps, false); }
public static TextureInfoWrapper FetchCacheTexture(TexInfo Texture, bool compress, bool mipmaps) { String textureName = Texture.name; String originalTextureFile = KSPUtil.ApplicationRootPath + "GameData/" + textureName; String cacheFile = KSPUtil.ApplicationRootPath + "GameData/ActiveTextureManagement/textureCache/" + textureName; String cacheConfigFile = cacheFile + ".tcache"; cacheFile += ".imgcache"; String hashString = GetMD5String(originalTextureFile); if (TextureHashTable.ContainsKey(hashString)) { ActiveTextureManagement.DBGLog("hash triggered... " + textureName); TextureInfoWrapper tex = TextureHashTable[hashString]; if (tex.name != textureName) { TextureInfoWrapper cacheTexInfo = new TextureInfoWrapper(Texture.file, tex.texture, tex.isNormalMap, tex.isReadable, tex.isCompressed); cacheTexInfo.name = textureName; ActiveTextureManagement.DBGLog("Re-using from hash dictionary... " + textureName + " is a duplicate of " + tex.name); return(cacheTexInfo); } } if (File.Exists(cacheConfigFile)) { ConfigNode config = ConfigNode.Load(cacheConfigFile); string format = config.GetValue("orig_format"); String cacheHash = config.GetValue("md5"); int origWidth, origHeight; string origWidthString = config.GetValue("orig_width"); string origHeightString = config.GetValue("orig_height"); int.TryParse(origWidthString, out origWidth); int.TryParse(origHeightString, out origHeight); if (origWidthString == null || origHeightString == null || cacheHash == null || format == null) { return(RebuildCache(Texture, compress, mipmaps, hashString)); } originalTextureFile += format; Texture.Resize(origWidth, origHeight); if (format != null && File.Exists(originalTextureFile) && File.Exists(cacheFile)) { String cacheIsNormString = config.GetValue("is_normal"); String cacheIsCompressed = config.GetValue("is_compressed"); String cacheWidthString = config.GetValue("width"); String cacheHeihtString = config.GetValue("height"); string hasAlphaString = config.GetValue("hasAlpha"); string hasMipmapsString = config.GetValue("hasMipmaps"); bool cacheIsNorm = false; int cacheWidth = 0; int cacheHeight = 0; bool hasAlpha = true; bool hasMipmaps = true; bool isCompressed = true; bool.TryParse(cacheIsNormString, out cacheIsNorm); bool.TryParse(cacheIsCompressed, out isCompressed); int.TryParse(cacheWidthString, out cacheWidth); int.TryParse(cacheHeihtString, out cacheHeight); bool.TryParse(hasAlphaString, out hasAlpha); bool.TryParse(hasMipmapsString, out hasMipmaps); if (cacheHash != hashString || compress != isCompressed || mipmaps != hasMipmaps || cacheIsNorm != Texture.isNormalMap || Texture.resizeWidth != cacheWidth || Texture.resizeHeight != cacheHeight) { if (cacheHash != hashString) { ActiveTextureManagement.DBGLog(cacheHash + " != " + hashString); } if (cacheIsNorm != Texture.isNormalMap) { ActiveTextureManagement.DBGLog(cacheIsNorm + " != " + Texture.isNormalMap); } if (Texture.resizeWidth != cacheWidth) { ActiveTextureManagement.DBGLog(Texture.resizeWidth + " != " + cacheWidth); } if (Texture.resizeHeight != cacheHeight) { ActiveTextureManagement.DBGLog(Texture.resizeHeight + " != " + cacheHeight); } return(RebuildCache(Texture, compress, mipmaps, hashString)); } else { ActiveTextureManagement.DBGLog("Loading from cache... " + textureName); Texture.needsResize = false; Texture.width = Texture.resizeWidth; Texture.height = Texture.resizeHeight; Texture.filename = cacheFile; TextureInfoWrapper tex = TextureConverter.DDSToTexture(Texture.file, Texture, hasMipmaps, isCompressed, hasAlpha); if (TextureHashTable.ContainsKey(hashString)) { TextureHashTable[hashString] = tex; } else { TextureHashTable.Add(hashString, tex); } return(tex); } } else { return(RebuildCache(Texture, compress, mipmaps, hashString)); } } else { return(RebuildCache(Texture, compress, mipmaps, hashString)); } }
public static GameDatabase.TextureInfo FetchCacheTexture(TexInfo Texture, bool compress, bool mipmaps, bool makeNotReadable) { String textureName = Texture.name; String originalTextureFile = KSPUtil.ApplicationRootPath + "GameData/" + textureName; String cacheFile = KSPUtil.ApplicationRootPath + "GameData/ActiveTextureManagement/textureCache/" + textureName; String cacheConfigFile = cacheFile + ".tcache"; cacheFile += ".pngcache"; if (File.Exists(cacheConfigFile)) { ConfigNode config = ConfigNode.Load(cacheConfigFile); string format = config.GetValue("orig_format"); String cacheHash = config.GetValue("md5"); int origWidth, origHeight; string origWidthString = config.GetValue("orig_width"); string origHeightString = config.GetValue("orig_height"); int.TryParse(origWidthString, out origWidth); int.TryParse(origHeightString, out origHeight); if (origWidthString == null || origHeightString == null || cacheHash == null || format == null) { return(RebuildCache(Texture, compress, mipmaps, makeNotReadable)); } originalTextureFile += format; String hashString = GetMD5String(originalTextureFile); Texture.Resize(origWidth, origHeight); if (format != null && File.Exists(originalTextureFile) && File.Exists(cacheFile)) { String cacheIsNormString = config.GetValue("is_normal"); String cacheWidthString = config.GetValue("width"); String cacheHeihtString = config.GetValue("height"); bool cacheIsNorm = false; int cacheWidth = 0; int cacheHeight = 0; bool.TryParse(cacheIsNormString, out cacheIsNorm); int.TryParse(cacheWidthString, out cacheWidth); int.TryParse(cacheHeihtString, out cacheHeight); if (cacheHash != hashString || cacheIsNorm != Texture.isNormalMap || Texture.resizeWidth != cacheWidth || Texture.resizeHeight != cacheHeight) { if (cacheHash != hashString) { ActiveTextureManagement.DBGLog(cacheHash + " != " + hashString); } if (cacheIsNorm != Texture.isNormalMap) { ActiveTextureManagement.DBGLog(cacheIsNorm + " != " + Texture.isNormalMap); } if (Texture.resizeWidth != cacheWidth) { ActiveTextureManagement.DBGLog(Texture.resizeWidth + " != " + cacheWidth); } if (Texture.resizeHeight != cacheHeight) { ActiveTextureManagement.DBGLog(Texture.resizeHeight + " != " + cacheHeight); } return(RebuildCache(Texture, compress, mipmaps, makeNotReadable)); } else if (cacheHash == hashString && !Texture.needsResize) { return(RebuildCache(Texture, compress, mipmaps, makeNotReadable)); } else { ActiveTextureManagement.DBGLog("Loading from cache... " + textureName); Texture.needsResize = false; Texture2D newTex = new Texture2D(4, 4); GameDatabase.TextureInfo cacheTexture = new GameDatabase.TextureInfo(newTex, Texture.isNormalMap, !makeNotReadable, compress); Texture.texture = cacheTexture; Texture.filename = cacheFile; TextureConverter.IMGToTexture(Texture, mipmaps, cacheIsNorm); cacheTexture.name = textureName; newTex.name = textureName; if (compress) { newTex.Compress(true); } newTex.Apply(mipmaps, makeNotReadable); return(cacheTexture); } } else { return(RebuildCache(Texture, compress, mipmaps, makeNotReadable)); } } else { return(RebuildCache(Texture, compress, mipmaps, makeNotReadable)); } }
private static GameDatabase.TextureInfo RebuildCache(TexInfo Texture, bool compress, bool mipmaps, bool makeNotReadable) { Texture.loadOriginalFirst = true; ActiveTextureManagement.DBGLog("Loading texture..."); TextureConverter.GetReadable(Texture, mipmaps); ActiveTextureManagement.DBGLog("Texture loaded."); GameDatabase.TextureInfo cacheTexture = Texture.texture; Texture2D tex = cacheTexture.texture; String textureName = cacheTexture.name; String cacheFile = KSPUtil.ApplicationRootPath + "GameData/ActiveTextureManagement/textureCache/" + textureName; if (Texture.needsResize) { ActiveTextureManagement.DBGLog("Rebuilding Cache... " + Texture.name); ActiveTextureManagement.DBGLog("Saving cache file " + cacheFile + ".pngcache"); TextureConverter.WriteTo(cacheTexture.texture, cacheFile + ".pngcache"); String originalTextureFile = Texture.filename; String cacheConfigFile = cacheFile + ".tcache"; ActiveTextureManagement.DBGLog("Created Config for" + originalTextureFile); String hashString = GetMD5String(originalTextureFile); ConfigNode config = new ConfigNode(); config.AddValue("md5", hashString); ActiveTextureManagement.DBGLog("md5: " + hashString); config.AddValue("orig_format", Path.GetExtension(originalTextureFile)); ActiveTextureManagement.DBGLog("orig_format: " + Path.GetExtension(originalTextureFile)); config.AddValue("orig_width", Texture.width.ToString()); ActiveTextureManagement.DBGLog("orig_width: " + Texture.width.ToString()); config.AddValue("orig_height", Texture.height.ToString()); ActiveTextureManagement.DBGLog("orig_height: " + Texture.height.ToString()); config.AddValue("is_normal", cacheTexture.isNormalMap.ToString()); ActiveTextureManagement.DBGLog("is_normal: " + cacheTexture.isNormalMap.ToString()); config.AddValue("width", Texture.resizeWidth.ToString()); ActiveTextureManagement.DBGLog("width: " + Texture.resizeWidth.ToString()); config.AddValue("height", Texture.resizeHeight.ToString()); ActiveTextureManagement.DBGLog("height: " + Texture.resizeHeight.ToString()); config.Save(cacheConfigFile); ActiveTextureManagement.DBGLog("Saved Config."); } else { String directory = Path.GetDirectoryName(cacheFile + ".none"); if (File.Exists(directory)) { File.Delete(directory); } Directory.CreateDirectory(directory); } if (compress) { tex.Compress(true); } cacheTexture.isCompressed = compress; if (!makeNotReadable) { tex.Apply(mipmaps); } else { tex.Apply(mipmaps, true); } cacheTexture.isReadable = !makeNotReadable; return(cacheTexture); }