void RecompressGMFiles(object o) { RecompressThreadArgs rta = (RecompressThreadArgs)o; string inDir = rta.inDir; string[] allFiles = Directory.GetFiles(inDir); int numFiles = allFiles.Length; WaitDlg dlg = rta.dlg; string pngDirectory = Path.Combine(inDir, "bmp"); dlg.UpdateStatus("Recompressing {0} files", numFiles); string outDir = rta.outDir; string gmllFile = @"T:\out.gmll"; for (int i = 0; i < numFiles; ++i) { Box.ResetIndexCount(); string currentFile = allFiles[i]; byte[] gmFileData; MemoryStream decompGM; using (FileStream fs = File.OpenRead(currentFile)) { decompGM = Compress.GZipDecompress(fs); gmFileData = decompGM.ToArray(); } string curFile = Path.GetFileName(currentFile); string pngFile = Path.Combine(pngDirectory, curFile + ".bmp"); GTMP.GMFile.GMFileInfo fi = GTMP.GMFile.Parse(decompGM); List <IBox> boxes = ConvertGMFileBoxes(fi.Boxes); if (!Tools.ConvertImageTo(pngFile, gmllFile, Tools.ConvertType.GM)) { dlg.UpdateStatus(String.Format("Couldn't convert {0} to GT2 foreground, skipping", pngFile)); continue; } try { byte[] gmllData = Images.LoadGMLLData(gmllFile); string newFileName = Path.Combine(outDir, curFile); GMProject.ExportGM(newFileName, boxes, fi.Metadata, gmllData); } catch (InvalidBoxStateException ibse) { DebugLogger.Log("Debug", "Exception was from {0}", currentFile); dlg.UpdateStatus("Caught invalid box exception {0} for {1} in file {2}", ibse.Message, ibse.InvalidBox.Name, currentFile); File.Delete(gmllFile); } if ((i != 0) && ((i % 100) == 0)) { dlg.UpdateStatus(String.Format("Tested {0} files", i)); } } dlg.AllowClose("All done"); }
void CheckGMFileValidity(object o) { ValidityThreadArgs vta = (ValidityThreadArgs)o; string dir = vta.dir; string[] allFiles = Directory.GetFiles(dir); int numFiles = allFiles.Length; WaitDlg dlg = vta.dlg; dlg.UpdateStatus("Checking {0} files", numFiles); int exceptions = 0; for (int i = 0; i < numFiles; ++i) { Box.ResetIndexCount(); string currentFile = allFiles[i]; GTMP.GMFile.GMFileInfo fi = GTMP.GMFile.Parse(currentFile); List <IBox> boxes = ConvertGMFileBoxes(fi.Boxes); try { MemoryStream ms = new MemoryStream(boxes.Count * 0x4c); using (BinaryWriter bw = new BinaryWriter(ms)) { foreach (IBox b in boxes) { b.Serialize(bw); } } } catch (InvalidBoxStateException ibse) { if (++exceptions == 50) { break; } DebugLogger.Log("Debug", "Exception was from {0}", currentFile); dlg.UpdateStatus("Caught invalid box exception {0} for {1} in file {2}", ibse.Message, ibse.InvalidBox.Name, currentFile); } if ((i != 0) && ((i % 10) == 0)) { dlg.UpdateStatus(String.Format("Tested {0} files", i)); } } dlg.AllowClose("All checked"); }
static internal Bitmap FromBytes(byte[] pixelData) { MemoryStream ms = new MemoryStream(pixelData); try { // this gymnastics is required otherwise we have to keep the // MemoryStream the Bitmap is created from alive as long as the Bitmap is Bitmap retBm; using (Bitmap temp = new Bitmap(ms)) { retBm = new Bitmap(temp.Width, temp.Height, temp.PixelFormat); using (Graphics g = Graphics.FromImage(retBm)) { g.DrawImageUnscaled(temp, Point.Empty); } } return(retBm); } catch (Exception) { } ms.Position = 0; Bitmap bm = GTMP.GTMPFile.Parse(ms); if (bm == null) { ms.Position = 0; GTMP.GMFile.GMFileInfo gmf = GTMP.GMFile.Parse(ms); if (gmf != null) { bm = gmf.Image; } else { bm = GTMP.GMFile.ParseGMLLData(pixelData, 0, null); } } return(bm); }
private void FGImageLoad(string fileName, Images.ImageLoadResult info) { if (DebugLogger.DoDebugActions()) { string fgImageName = Globals.MakeDebugSaveName(true, Path.GetFileName(fileName)); File.Copy(fileName, fgImageName, true); } if (info.type != Images.ImageType.GM) { byte[] newForegroundData; if (info.type != Images.ImageType.GMLL) { newForegroundData = Tools.CheckConvertImageTo(info.image, Tools.ConvertType.GM); if (newForegroundData == null) { info.image.Dispose(); return; } } else { newForegroundData = File.ReadAllBytes(fileName); } DebugLogger.Log("Main", "Got {0} bytes of non-GM GMLL data", newForegroundData.Length); if (DebugLogger.DoDebugActions()) { string nonGMDataFile = Globals.MakeDebugSaveName(false, "{0}nongm-fg.gmll", Path.GetFileNameWithoutExtension(fileName)); File.WriteAllBytes(nonGMDataFile, newForegroundData); } layerState.On(LayerStateManager.Layers.Foreground); ReplaceForegroundImage(newForegroundData, info.image); } else // (info.type == Images.ImageType.GM) { // This is a hack since CloseCurrentFile() resets everything // and disposes the images, but when we're reloading the foreground // only, we should preserve the background, so lets do this // rather than bool-ing it up just for this one case Image bgBackup = null; if (canvasBgImage != null) { bgBackup = (Image)canvasBgImage.Clone(); } if (!CloseCurrentFile()) { // didn't need it anyway if (bgBackup != null) { bgBackup.Dispose(); } info.image.Dispose(); return; } canvasBgImage = bgBackup; layerState.On(LayerStateManager.Layers.Foreground); byte[] gmllData = Images.LoadGMLLData(fileName); ReplaceForegroundImage(gmllData, info.image); GTMP.GMFile.GMFileInfo fileInf = info.gmInfo; DebugLogger.Log("Main", "Got {0} bytes of GM-GMLL data from {1}, file had {2} boxes", gmllData.Length, fileName, fileInf.Boxes == null ? -1 : fileInf.Boxes.Count); if (fileInf.Boxes != null) { boxList.BeginUpdate(); try { allBoxes.Clear(); List <IBox> boxes = ConvertGMFileBoxes(fileInf.Boxes); allBoxes.Load(boxes); } finally { boxList.EndUpdate(); } } metadataPropertyList.SelectedObject = fileInf.Metadata; } SetTitleFileName(fileName); }
public ImageLoadResult(GTMP.GMFile.GMFileInfo info) { type = ImageType.GM; image = info.Image; gmInfo = info; }
public ImageLoadResult(Bitmap bm, ImageType imgType) { type = imgType; image = bm; gmInfo = null; }