Пример #1
0
 private static void DoClear(IAsyncContext context, IWritableBitmapData bitmapData, Color32 color, IDitherer ditherer)
 {
     IBitmapDataInternal accessor = bitmapData as IBitmapDataInternal ?? new BitmapDataWrapper(bitmapData, false, true);
     try
     {
         if (ditherer == null || !accessor.PixelFormat.CanBeDithered())
             ClearDirect(context, accessor, color);
         else
             ClearWithDithering(context, accessor, color, ditherer);
     }
     finally
     {
         if (!ReferenceEquals(accessor, bitmapData))
             accessor.Dispose();
     }
 }
Пример #2
0
        private static IReadWriteBitmapData?DoLoadBitmapData(IAsyncContext context, Stream stream)
        {
            context.Progress?.New(DrawingOperation.Loading, 1000);
            var reader = new BinaryReader(stream);

            if (reader.ReadInt32() != magicNumber)
            {
                throw new ArgumentException(Res.ImagingNotBitmapDataStream, nameof(stream));
            }
            var     size           = new Size(reader.ReadInt32(), reader.ReadInt32());
            var     pixelFormat    = (PixelFormat)reader.ReadInt32();
            Color32 backColor      = Color32.FromArgb(reader.ReadInt32());
            byte    alphaThreshold = reader.ReadByte();

            Palette?palette       = null;
            int     paletteLength = reader.ReadInt32();

            if (paletteLength > 0)
            {
                var entries = new Color32[paletteLength];
                for (int i = 0; i < paletteLength; i++)
                {
                    entries[i] = Color32.FromArgb(reader.ReadInt32());
                }
                palette = new Palette(entries, backColor, alphaThreshold);
            }

            context.Progress?.SetProgressValue((int)(stream.Position * 1000 / stream.Length));
            if (context.IsCancellationRequested)
            {
                return(null);
            }

            IBitmapDataInternal result = CreateManagedBitmapData(size, pixelFormat, backColor, alphaThreshold, palette);
            int  bpp      = pixelFormat.ToBitsPerPixel();
            bool canceled = false;

            try
            {
                IBitmapDataRowInternal row = result.DoGetRow(0);
                for (int y = 0; y < result.Height; y++)
                {
                    if (canceled = context.IsCancellationRequested)
                    {
                        return(null);
                    }

                    switch (bpp)
                    {
                    case 32:
                        for (int x = 0; x < result.Width; x++)
                        {
                            row.DoWriteRaw(x, reader.ReadInt32());
                        }
                        break;

                    case 16:
                        for (int x = 0; x < result.Width; x++)
                        {
                            row.DoWriteRaw(x, reader.ReadInt16());
                        }
                        break;

                    case 64:
                        for (int x = 0; x < result.Width; x++)
                        {
                            row.DoWriteRaw(x, reader.ReadInt64());
                        }
                        break;

                    default:
                        for (int x = 0; x < result.RowSize; x++)
                        {
                            row.DoWriteRaw(x, reader.ReadByte());
                        }
                        break;
                    }

                    row.MoveNextRow();
                    context.Progress?.SetProgressValue((int)(stream.Position * 1000 / stream.Length));
                }

                return((canceled = context.IsCancellationRequested) ? null : result);
            }
            finally
            {
                if (canceled)
                {
                    result.Dispose();
                }
            }
        }