public static void LoadProject(string zipFilename, PPProject project) { using (Package package = Package.Open(zipFilename, FileMode.Open)) { if (!package.PartExists(URI_PART_GREYSCALE)) { throw new FileFormatException("Project zip file does not include greyscale image."); } if (!package.PartExists(URI_PART_COLOR)) { throw new FileFormatException("Project zip file does not include color image."); } project.GreyscaleBitmap = package.GetPart(URI_PART_GREYSCALE).GetStream().StreamToByteArray().ByteArrayToBitmap(); project.ColorBitmap = package.GetPart(URI_PART_COLOR).GetStream().StreamToByteArray().ByteArrayToBitmap(); project.ColorPalette = null; if (package.PartExists(URI_PART_PALETTE)) { string[] paletteLines = package.GetPart(URI_PART_PALETTE).GetStream().StreamToByteArray().ByteArrayToText(); FormatGPL gpl = new FormatGPL(paletteLines); project.ColorPalette = gpl.ColorPalette; } project.Config = null; if (package.PartExists(URI_PART_CONFIG)) { string[] configLines = package.GetPart(URI_PART_CONFIG).GetStream().StreamToByteArray().ByteArrayToText(); project.Config = new PPConfig(configLines); } } }
/// <summary> /// Saved a zipped Perpetual Paint Collection file. /// </summary> /// <remarks> /// Creates or overwrites <paramref name='zipFilename'/>. /// /// Handles: /// - include entire palette /// - include filename of palette (in config) /// - include no palette information /// </remarks> public static void ZipCollection(string zipFilename, PPCollection collection) { byte[] zippedBytes; using (MemoryStream zipStream = new MemoryStream()) { using (Package package = Package.Open(zipStream, FileMode.Create)) { PackagePart collectionDocument = package.CreatePart(URI_PART_COLLECTION, ""); using (MemoryStream dataStream = new MemoryStream(collection.ToTextFormat().ToByteArray())) { collectionDocument.GetStream().WriteAll(dataStream); } if (collection.ColorPalette != null) { PackagePart paletteDocument = package.CreatePart(URI_PART_PALETTE, ""); using (MemoryStream dataStream = new MemoryStream(FormatGPL.ToTextFormat(collection.ColorPalette).ToByteArray())) { paletteDocument.GetStream().WriteAll(dataStream); } } if (collection.Config != null) { PackagePart configDocument = package.CreatePart(URI_PART_CONFIG, ""); using (MemoryStream dataStream = new MemoryStream(collection.Config.ToTextFormat().ToByteArray())) { configDocument.GetStream().WriteAll(dataStream); } } } zippedBytes = zipStream.ToArray(); } File.WriteAllBytes(zipFilename, zippedBytes); }
public static PPCollection LoadCollection(string zipFilename) { using (Package package = Package.Open(zipFilename, FileMode.Open)) { if (!package.PartExists(URI_PART_COLLECTION)) { throw new FileFormatException("Collection zip file does not include list of projects."); } string[] projectFileNames = package.GetPart(URI_PART_COLLECTION).GetStream().StreamToByteArray().ByteArrayToText().Where(x => !String.IsNullOrEmpty(x)).ToArray(); WithoutHaste.Drawing.Colors.ColorPalette colorPalette = null; if (package.PartExists(URI_PART_PALETTE)) { string[] paletteLines = package.GetPart(URI_PART_PALETTE).GetStream().StreamToByteArray().ByteArrayToText(); FormatGPL gpl = new FormatGPL(paletteLines); colorPalette = gpl.ColorPalette; } PPConfig config = null; if (package.PartExists(URI_PART_CONFIG)) { string[] configLines = package.GetPart(URI_PART_CONFIG).GetStream().StreamToByteArray().ByteArrayToText(); config = new PPConfig(configLines); } return(new PPCollection(projectFileNames, config, colorPalette)); } }
public void LoadGPL() { //arrange string filename = "../../../testData/Crayola.gpl"; //act ColorPalette palette = FormatGPL.Load(filename); //assert Assert.AreEqual(128, palette.Colors.Length); }
/// <summary> /// Saved a zipped Perpetual Paint Project file. /// </summary> /// <remarks> /// Creates or overwrites <paramref name='zipFilename'/>. /// /// Handles: /// - include entire palette /// - include filename of palette (in config) /// - include no palette information /// </remarks> public static void ZipProject(string zipFilename, PPProject project) { byte[] zippedBytes; using (MemoryStream zipStream = new MemoryStream()) { using (Package package = Package.Open(zipStream, FileMode.Create)) { PackagePart greyscaleDocument = package.CreatePart(URI_PART_GREYSCALE, ""); using (MemoryStream dataStream = new MemoryStream(project.GreyscaleBitmap.ToByteArray())) { greyscaleDocument.GetStream().WriteAll(dataStream); } PackagePart colorDocument = package.CreatePart(URI_PART_COLOR, ""); using (MemoryStream dataStream = new MemoryStream(project.ColorBitmap.ToByteArray())) { colorDocument.GetStream().WriteAll(dataStream); } if (project.ColorPalette != null) { PackagePart paletteDocument = package.CreatePart(URI_PART_PALETTE, ""); using (MemoryStream dataStream = new MemoryStream(FormatGPL.ToTextFormat(project.ColorPalette).ToByteArray())) { paletteDocument.GetStream().WriteAll(dataStream); } } if (project.Config != null) { PackagePart configDocument = package.CreatePart(URI_PART_CONFIG, ""); using (MemoryStream dataStream = new MemoryStream(project.Config.ToTextFormat().ToByteArray())) { configDocument.GetStream().WriteAll(dataStream); } } } zippedBytes = zipStream.ToArray(); } File.WriteAllBytes(zipFilename, zippedBytes); }