public static void Save( string fileName, Image bg, byte[] fgGMLL, List <IBox> boxes, GTMP.GMFile.GMMetadata metadata, IconImgType fileVersion ) { Debug.Assert(fileVersion != IconImgType.Invalid); DebugLogger.Log("Project", "Saving project to {0}", fileName); GMSerializedProject gmp = new GMSerializedProject(); gmp.foreground = (fgGMLL != null) ? Convert.ToBase64String(fgGMLL) : String.Empty; byte[] imageData = Images.GetBytes(bg, System.Drawing.Imaging.ImageFormat.Png); gmp.background = Convert.ToBase64String(imageData); gmp.fileMetadata = metadata; gmp.gt2BoxVersion = fileVersion; SeparateBoxes(boxes, out gmp.boxes, out gmp.iconBoxes); string projectFile = Json.Serialize(gmp); #if DEBUG File.WriteAllText(@"T:\gmpproj.txt", projectFile); #endif MemoryStream compProj = Compress.ZipCompressString(projectFile); File.WriteAllBytes(fileName, compProj.ToArray()); if (DebugLogger.DoDebugActions()) { string projectCopy = Globals.MakeDebugSaveName(true, Path.GetFileName(fileName)); File.WriteAllBytes(projectCopy, compProj.ToArray()); } }
internal static void Refresh(IconImgData imageData, IconImgType version, string directory) { Debug.Assert(version != IconImgType.Invalid); DebugLogger.Log("Hardcoded", "Refreshing IconImages of version {0}", version); int stride = imageData.ByteStride; IconImgFile iconFileInfo = imageData.Versions[(int)version]; string iconImgFile = Path.Combine(directory, version.ToString() + "Icon.dat"); byte[] fileData = File.ReadAllBytes(iconImgFile); IconImgEntry[] imageEntries = iconFileInfo.Entries; int id = 0; images = new List <IconImgEntry>(); foreach (IconImgEntry entry in imageEntries) { Size imageSize = entry.ImageSize; Bitmap bm = new Bitmap(imageSize.Width, imageSize.Height, System.Drawing.Imaging.PixelFormat.Format4bppIndexed); System.Drawing.Imaging.ColorPalette imgPalette = bm.Palette; Color[] entries = imgPalette.Entries; for (int i = 0, colourOffset = entry.PaletteLocation; i < 16; ++i, colourOffset += sizeof(ushort)) { ushort colour = BitConverter.ToUInt16(fileData, colourOffset); entries[i] = ((colour & 0x7fff) == 0) ? Color.Transparent : MakeColorFromBGR555(colour); } bm.Palette = imgPalette; System.Drawing.Imaging.BitmapData bmData = bm.LockBits(new Rectangle(Point.Empty, bm.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format4bppIndexed); // These are 4bpp, so the 1 X byte = 2 X pixels, so we half it int lineBytes = imageSize.Width / 2; int height = imageSize.Height; int lockDataStride = bmData.Stride; byte[] imageBuffer = new byte[lockDataStride * height]; int fileIter = (entry.ImageLocation.Y * stride) + (entry.ImageLocation.X / 2); int imageBufferIter = 0; // the image buffer in the file is Big endian, like this (pixel numbers within each byte) // [2, 1], [4, 3], [6, 5] // we need it in little endian so this copies the data while swizzling it into // [1, 2], [3, 4], [5, 6] // for (int y = 0; y < height; ++y, fileIter += stride, imageBufferIter += lockDataStride) { Buffer.BlockCopy(fileData, fileIter, imageBuffer, imageBufferIter, lineBytes); for (int x = 0; x < lineBytes; ++x) { byte pixels = imageBuffer[imageBufferIter + x]; byte swizzedPixels = (byte)(((pixels << 4) & 0xF0) | (pixels >> 4)); imageBuffer[imageBufferIter + x] = swizzedPixels; } } // blit it to the real bitmap data System.Runtime.InteropServices.Marshal.Copy(imageBuffer, 0, bmData.Scan0, imageBuffer.Length); bm.UnlockBits(bmData); entry.Image = bm; entry.Id = id++; images.Add(entry); } }
private void SaveSettings() { AppSettings settings = Globals.App; settings.CentralAnchorWidth = (int)centralAnchorWSize.Value; settings.CentralAnchorHeight = (int)centralAnchorHSize.Value; settings.ResizeAnchorWidth = (int)cornerAnchorWSize.Value; settings.ResizeAnchorHeight = (int)cornerAnchorHSize.Value; settings.AnchorColour = anchorColourSwatch.BackColor; settings.DefaultOutlineColour = drawBoxColourSwatch.BackColor; settings.SelectedOutlineColour = selectedBoxColourSwatch.BackColor; settings.TransparentBackgroundAreasColour = bgTransparencySwatch.BackColor; settings.CompressionLevel = (int)compressionLevelNumber.Value; IconImgType origVersion = settings.GT2Version; for (int i = 0; i < versionButtons.Length; ++i) { if (versionButtons[i].Checked) { settings.GT2Version = (IconImgType)i; break; } } NeedsHardcodedRefresh = (origVersion != settings.GT2Version); settings.RefreshNonSerialised(); settings.FireSettingsChanged(); }
public static bool Load(string fileName, out Bitmap bg, out Bitmap fg, out byte[] fgGMLL, out List <IBox> boxes, out GTMP.GMFile.GMMetadata metadata) { fg = bg = null; fgGMLL = null; boxes = null; metadata = null; DebugLogger.Log("Project", "Loading file at {0}", fileName); if (DebugLogger.DoDebugActions()) { string projectCopy = Globals.MakeDebugSaveName(true, Path.GetFileName(fileName)); File.Copy(fileName, projectCopy, true); } byte[] projectBytes; using (FileStream fs = File.OpenRead(fileName)) { projectBytes = Compress.DecompressStream(fs).ToArray(); } string jsonText = Encoding.UTF8.GetString(projectBytes); GMSerializedProject projectData = Json.Parse <GMSerializedProject>(jsonText); IconImgType projType = projectData.gt2BoxVersion; IconImgType currentType = Globals.App.GT2Version; Debug.Assert(projType != IconImgType.Invalid); if (projType != currentType) { System.Windows.Forms.DialogResult res = MainForm.DisplayMsgBox( System.Windows.Forms.MessageBoxButtons.YesNoCancel, System.Windows.Forms.MessageBoxIcon.Question, "{0} was saved with GT2 Version {1} which is different from the current version {2}. Change the current version?", fileName, projType, currentType ); if (res != System.Windows.Forms.DialogResult.Yes) { return(false); } Globals.App.GT2Version = projType; Hardcoded.Refresh(System.Windows.Forms.Application.StartupPath); } byte[] imageData; if (!String.IsNullOrEmpty(projectData.background)) { imageData = Convert.FromBase64String(projectData.background); bg = Images.FromBytes(imageData); } else { bg = null; } if (!String.IsNullOrEmpty(projectData.foreground)) { fgGMLL = Convert.FromBase64String(projectData.foreground); fg = Images.FromBytes(fgGMLL); } metadata = projectData.fileMetadata; boxes = new List <IBox>(); foreach (IconImgBox img in projectData.iconBoxes) { boxes.Add(img); } foreach (Box box in projectData.boxes) { boxes.Add(box); } return(true); }