private HistoryMemento ImportOneLayer(DocumentWorkspace documentWorkspace, BitmapLayer layer) { SegmentedList <HistoryMemento> items = new SegmentedList <HistoryMemento>(); bool flag = true; if (flag && !documentWorkspace.Selection.IsEmpty) { HistoryMemento item = new DeselectFunction().Execute(documentWorkspace); items.Add(item); } if (flag && ((layer.Width > documentWorkspace.Document.Width) || (layer.Height > documentWorkspace.Document.Height))) { HistoryMemento memento3 = this.DoCanvasResize(documentWorkspace, layer.Size); if (memento3 == null) { flag = false; } else { items.Add(memento3); } } if (flag && (layer.Size != documentWorkspace.Document.Size)) { BitmapLayer layer2; try { using (new WaitCursorChanger(documentWorkspace)) { CleanupManager.RequestCleanup(); layer2 = CanvasSizeAction.ResizeLayer(layer, documentWorkspace.Document.Size, AnchorEdge.TopLeft, ColorBgra.White.NewAlpha(0)); } } catch (OutOfMemoryException exception) { ExceptionDialog.ShowErrorDialog(documentWorkspace, PdnResources.GetString("ImportFromFileAction.ImportOneLayer.OutOfMemory"), exception); flag = false; layer2 = null; } if (layer2 != null) { layer.Dispose(); layer = layer2; } } if (flag) { NewLayerHistoryMemento memento4 = new NewLayerHistoryMemento(string.Empty, null, documentWorkspace, documentWorkspace.Document.Layers.Count); documentWorkspace.Document.Layers.Add(layer); items.Add(memento4); } if (flag) { return(new CompoundHistoryMemento(string.Empty, null, items.ToArrayEx <HistoryMemento>())); } this.Rollback(items); return(null); }
/// <summary> /// Decodes the gray scale PixMap into a BitmapLayer. /// </summary> /// <param name="width">The width of the image.</param> /// <param name="height">The height of the image.</param> /// <param name="sixteenBit"><c>true</c> if the image data is 16 bits-per-channel; otherwise, <c>false</c>.</param> /// <returns>A BitmapLayer containing the decoded image.</returns> private unsafe BitmapLayer DecodeGrayScale(int width, int height, bool sixteenBit) { BitmapLayer layer = null; BitmapLayer tempLayer = null; try { tempLayer = Layer.CreateBackgroundLayer(width, height); Surface surface = tempLayer.Surface; if (sixteenBit) { byte[] map = CreateEightBitLookupTable(); for (int y = 0; y < height; y++) { ColorBgra *p = surface.GetRowAddressUnchecked(y); for (int x = 0; x < width; x++) { ushort value = reader.ReadUInt16(); p->R = p->G = p->B = map[value]; p->A = 255; p++; } } } else { for (int y = 0; y < height; y++) { ColorBgra *p = surface.GetRowAddressUnchecked(y); for (int x = 0; x < width; x++) { p->R = p->G = p->B = reader.ReadByte(); p->A = 255; p++; } } } layer = tempLayer; tempLayer = null; } finally { if (tempLayer != null) { tempLayer.Dispose(); tempLayer = null; } } return(layer); }
private HistoryMemento ImportOneLayer(DocumentWorkspace documentWorkspace, BitmapLayer layer) { HistoryMemento retHA; List<HistoryMemento> historyMementos = new List<HistoryMemento>(); bool success = true; if (success) { if (!documentWorkspace.Selection.IsEmpty) { HistoryMemento ha = new DeselectFunction().Execute(documentWorkspace); historyMementos.Add(ha); } } if (success) { if (layer.Width > documentWorkspace.Document.Width || layer.Height > documentWorkspace.Document.Height) { HistoryMemento ha = DoCanvasResize(documentWorkspace, layer.Size); if (ha == null) { success = false; } else { historyMementos.Add(ha); } } } if (success) { if (layer.Size != documentWorkspace.Document.Size) { BitmapLayer newLayer; try { using (new WaitCursorChanger(documentWorkspace)) { Utility.GCFullCollect(); newLayer = CanvasSizeAction.ResizeLayer((BitmapLayer)layer, documentWorkspace.Document.Size, AnchorEdge.TopLeft, ColorBgra.White.NewAlpha(0)); } } catch (OutOfMemoryException) { Utility.ErrorBox(documentWorkspace, PdnResources.GetString("ImportFromFileAction.ImportOneLayer.OutOfMemory")); success = false; newLayer = null; } if (newLayer != null) { layer.Dispose(); layer = newLayer; } } } if (success) { NewLayerHistoryMemento nlha = new NewLayerHistoryMemento(string.Empty, null, documentWorkspace, documentWorkspace.Document.Layers.Count); documentWorkspace.Document.Layers.Add(layer); historyMementos.Add(nlha); } if (success) { HistoryMemento[] has = historyMementos.ToArray(); retHA = new CompoundHistoryMemento(string.Empty, null, has); } else { Rollback(historyMementos); retHA = null; } return retHA; }
protected override Document OnLoad(System.IO.Stream input) { TgaHeader header = new TgaHeader(input); bool compressed; switch (header.imageType) { case TgaType.Map: case TgaType.Rgb: case TgaType.Mono: compressed = false; break; case TgaType.RleMap: case TgaType.RleRgb: case TgaType.RleMono: compressed = true; break; default: throw new FormatException("unknown TGA image type"); } if (header.imageWidth == 0 || header.imageHeight == 0 || header.pixelDepth == 0 || header.cmapLength > 256) { throw new FormatException("bad TGA header"); } if (header.pixelDepth != 8 && header.pixelDepth != 15 && header.pixelDepth != 16 && header.pixelDepth != 24 && header.pixelDepth != 32) { throw new FormatException("bad TGA header: pixelDepth not one of {8, 15, 16, 24, 32}"); } if (header.idLength > 0) { input.Position += header.idLength; // skip descriptor } BitmapLayer layer = Layer.CreateBackgroundLayer(header.imageWidth, header.imageHeight); try { Surface surface = layer.Surface; surface.Clear((ColorBgra)0xffffffff); ColorBgra[] palette = null; if (header.cmapType != 0) { palette = LoadPalette(input, header.cmapLength); } if (header.imageType == TgaType.Mono || header.imageType == TgaType.RleMono) { palette = CreateGrayPalette(); } // Bits 0 - 3 of the image descriptor byte describe number of bits used for alpha channel // For loading, we won't worry about this. Not all TGA implementations are correct (such // as older Paint.NET TGA implementations!) and we don't want to lose all their alpha bits. //int alphaBits = header.imageDesc & 0xf; // Bits 4 & 5 of the image descriptor byte control the ordering of the pixels bool xReversed = ((header.imageDesc & 16) == 16); bool yReversed = ((header.imageDesc & 32) == 32); byte rleLeftOver = 255; // for images with illegal packet boundary for (int y = 0; y < header.imageHeight; ++y) { MemoryBlock dstRow; if (yReversed) { dstRow = surface.GetRow(y); } else { dstRow = surface.GetRow(header.imageHeight - y - 1); } if (compressed) { rleLeftOver = ExpandCompressedLine(dstRow, 0, ref header, input, header.imageWidth, y, rleLeftOver, palette); } else { ExpandUncompressedLine(dstRow, 0, ref header, input, header.imageWidth, y, 0, palette); } } if (xReversed) { MirrorX(surface); } Document document = new Document(surface.Width, surface.Height); document.Layers.Add(layer); return(document); } catch { if (layer != null) { layer.Dispose(); layer = null; } throw; } }
private HistoryMemento ImportOneLayer(DocumentWorkspace documentWorkspace, BitmapLayer layer) { HistoryMemento retHA; List <HistoryMemento> historyMementos = new List <HistoryMemento>(); bool success = true; if (success) { if (!documentWorkspace.Selection.IsEmpty) { HistoryMemento ha = new DeselectFunction().Execute(documentWorkspace); historyMementos.Add(ha); } } if (success) { if (layer.Width > documentWorkspace.Document.Width || layer.Height > documentWorkspace.Document.Height) { HistoryMemento ha = DoCanvasResize(documentWorkspace, layer.Size); if (ha == null) { success = false; } else { historyMementos.Add(ha); } } } if (success) { if (layer.Size != documentWorkspace.Document.Size) { BitmapLayer newLayer; try { using (new WaitCursorChanger(documentWorkspace)) { Utility.GCFullCollect(); newLayer = CanvasSizeAction.ResizeLayer((BitmapLayer)layer, documentWorkspace.Document.Size, AnchorEdge.TopLeft, ColorBgra.White.NewAlpha(0)); } } catch (OutOfMemoryException) { Utility.ErrorBox(documentWorkspace, PdnResources.GetString("ImportFromFileAction.ImportOneLayer.OutOfMemory")); success = false; newLayer = null; } if (newLayer != null) { layer.Dispose(); layer = newLayer; } } } if (success) { NewLayerHistoryMemento nlha = new NewLayerHistoryMemento(string.Empty, null, documentWorkspace, documentWorkspace.Document.Layers.Count); documentWorkspace.Document.Layers.Add(layer); historyMementos.Add(nlha); } if (success) { HistoryMemento[] has = historyMementos.ToArray(); retHA = new CompoundHistoryMemento(string.Empty, null, has); } else { Rollback(historyMementos); retHA = null; } return(retHA); }
public static Document Load(Stream stream) { using (BinaryReverseReader reader = new BinaryReverseReader(stream)) { ReadOnlyCollection <Brush> brushes; short version = reader.ReadInt16(); switch (version) { case 1: case 2: brushes = DecodeVersion1(reader, version); break; case 6: case 7: // Used by Photoshop CS and later for brushes containing 16-bit data. case 10: // Used by Photoshop CS6 and/or CC? brushes = DecodeVersion6(reader, version); break; default: throw new FormatException(string.Format(CultureInfo.CurrentCulture, Resources.UnsupportedABRVersion, version)); } if (brushes.Count == 0) { throw new FormatException(Resources.NoSampledBrushes); } int maxWidth = 0; int maxHeight = 0; foreach (var item in brushes) { if (item.Surface.Width > maxWidth) { maxWidth = item.Surface.Width; } if (item.Surface.Height > maxHeight) { maxHeight = item.Surface.Height; } } Document doc = null; Document tempDoc = null; try { tempDoc = new Document(maxWidth, maxHeight); for (int i = 0; i < brushes.Count; i++) { Brush abr = brushes[i]; BitmapLayer layer = null; BitmapLayer tempLayer = null; try { tempLayer = new BitmapLayer(maxWidth, maxHeight); tempLayer.IsBackground = i == 0; tempLayer.Name = !string.IsNullOrEmpty(abr.Name) ? abr.Name : string.Format(CultureInfo.CurrentCulture, Resources.BrushNameFormat, i); tempLayer.Metadata.SetUserValue(AbrMetadataNames.BrushSpacing, abr.Spacing.ToString(CultureInfo.InvariantCulture)); tempLayer.Surface.CopySurface(abr.Surface); layer = tempLayer; tempLayer = null; } finally { if (tempLayer != null) { tempLayer.Dispose(); tempLayer = null; } abr.Dispose(); } tempDoc.Layers.Add(layer); } doc = tempDoc; tempDoc = null; } finally { if (tempDoc != null) { tempDoc.Dispose(); tempDoc = null; } } return(doc); } }
protected override Document OnLoad(Stream input) { bool flag; Document document2; TgaHeader header = new TgaHeader(input); switch (header.imageType) { case TgaType.Map: case TgaType.Rgb: case TgaType.Mono: flag = false; break; case TgaType.RleMap: case TgaType.RleRgb: case TgaType.RleMono: flag = true; break; default: throw new FormatException("unknown TGA image type"); } if (((header.imageWidth == 0) || (header.imageHeight == 0)) || ((header.pixelDepth == 0) || (header.cmapLength > 0x100))) { throw new FormatException("bad TGA header"); } if ((((header.pixelDepth != 8) && (header.pixelDepth != 15)) && ((header.pixelDepth != 0x10) && (header.pixelDepth != 0x18))) && (header.pixelDepth != 0x20)) { throw new FormatException("bad TGA header: pixelDepth not one of {8, 15, 16, 24, 32}"); } if (header.idLength > 0) { input.Position += header.idLength; } BitmapLayer layer = Layer.CreateBackgroundLayer(header.imageWidth, header.imageHeight); try { Surface surface = layer.Surface; surface.Clear((ColorBgra)(-1)); ColorBgra[] palette = null; if (header.cmapType != 0) { palette = this.LoadPalette(input, header.cmapLength); } if ((header.imageType == TgaType.Mono) || (header.imageType == TgaType.RleMono)) { palette = this.CreateGrayPalette(); } bool flag2 = (header.imageDesc & 0x10) == 0x10; bool flag3 = (header.imageDesc & 0x20) == 0x20; byte rleLeftOver = 0xff; for (int i = 0; i < header.imageHeight; i++) { MemoryBlock row; if (flag3) { row = surface.GetRow(i); } else { row = surface.GetRow((header.imageHeight - i) - 1); } if (flag) { rleLeftOver = this.ExpandCompressedLine(row, 0, ref header, input, header.imageWidth, i, rleLeftOver, palette); } else { this.ExpandUncompressedLine(row, 0, ref header, input, header.imageWidth, i, 0, palette); } } if (flag2) { this.MirrorX(surface); } Document document = new Document(surface.Width, surface.Height); document.Layers.Add(layer); document2 = document; } catch { if (layer != null) { layer.Dispose(); layer = null; } throw; } return(document2); }