openpdn /src/Data/BitmapLayer.cs

Language C# Lines 379
MD5 Hash 49f96d276b31d9545930403e9ccb2a74 Estimated Cost $7,205 (why?)
Repository https://bitbucket.org/tuldok89/openpdn View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET                                                                   //
// Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors.     //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved.          //
// See src/Resources/Files/License.txt for full licensing and attribution      //
// details.                                                                    //
// .                                                                           //
/////////////////////////////////////////////////////////////////////////////////

using System;
using System.Drawing;
using System.Runtime.Serialization;

namespace PaintDotNet
{
    [Serializable]
    public class BitmapLayer
        : Layer,
          IDeserializationCallback
    {
        public override Surface RenderThumbnail(int maxEdgeLength)
        {
            Size thumbSize = Utility.ComputeThumbnailSize(Size, maxEdgeLength);
            var thumb = new Surface(thumbSize);

            thumb.SuperSamplingFitSurface(_surface);

            var thumb2 = new Surface(thumbSize);
            thumb2.ClearWithCheckboardPattern();
            var nbop = new UserBlendOps.NormalBlendOp();
            nbop.Apply(thumb2, thumb);

            thumb.Dispose();
            thumb = null;

            return thumb2;
        }

        private bool _disposed;
        protected override void Dispose(bool disposing)
        {
            if (_disposed)
            {
            }
            else
            {
                _disposed = true;

                try
                {
                    if (disposing)
                    {
                        if (_surface != null)
                        {
                            _surface.Dispose();
                            _surface = null;
                        }
                    }
                }

                finally
                {
                    base.Dispose(disposing);
                }
            }
        }

        [NonSerialized]
        private BinaryPixelOp _compiledBlendOp;

        private void CompileBlendOp()
        {
            bool isDefaultOp = (_properties.BlendOp.GetType() == UserBlendOps.GetDefaultBlendOp());

            _compiledBlendOp = Opacity == 255 ? _properties.BlendOp : _properties.BlendOp.CreateWithOpacity(Opacity);
        }

        protected override void OnPropertyChanged(string propertyName)
        {
            _compiledBlendOp = null;
            base.OnPropertyChanged (propertyName);
        }

        [Serializable]
        internal sealed class BitmapLayerProperties
            : ICloneable,
              ISerializable
        {
            public UserBlendOp BlendOp;
            internal int Opacity; // this is ONLY used when loading older version PDN files! should normally equal -1

            private const string BlendOpTag = "blendOp";
            private const string OpacityTag = "opacity";

            public static string BlendOpName
            {
                get
                {
                    return PdnResources.GetString("BitmapLayer.Properties.BlendOp.Name");
                }
            }

            public BitmapLayerProperties(UserBlendOp blendOp)
            {
                BlendOp = blendOp;
                Opacity = -1;
            }

            public BitmapLayerProperties(BitmapLayerProperties cloneMe)
            {
                BlendOp = cloneMe.BlendOp;
                Opacity = -1;
            }

            public object Clone()
            {
                return new BitmapLayerProperties(this);
            }

            public BitmapLayerProperties(SerializationInfo info, StreamingContext context)
            {
                BlendOp = (UserBlendOp)info.GetValue(BlendOpTag, typeof(UserBlendOp));

                // search for 'opacity' and load it if it exists
                Opacity = -1;

                foreach (SerializationEntry entry in info)
                {
                    if (entry.Name != OpacityTag) continue;
                    Opacity = (byte)entry.Value;
                    break;
                }
            }

            public void GetObjectData(SerializationInfo info, StreamingContext context)
            {
                info.AddValue(BlendOpTag, BlendOp);
            }
        }

        private BitmapLayerProperties _properties;
        private Surface _surface;

        public override object SaveProperties()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("BitmapLayer");
            }

            object baseProperties = base.SaveProperties();
            return new List(_properties.Clone(), new List(baseProperties, null));
        }

        public override void LoadProperties(object oldState, bool suppressEvents)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("BitmapLayer");
            }

            var list = (List)oldState;

            // Get the base class' state, and our state
            var baseState = (LayerProperties)list.Tail.Head;
            var blp = (BitmapLayerProperties)(((List)oldState).Head);

            // Opacity is only couriered for compatibility with PDN v2.0 and v1.1
            // files. It should not be present in v2.1+ files (well, it'll be
            // part of the base class' serialization)
            if (blp.Opacity != -1)
            {
                baseState.opacity = (byte)blp.Opacity;
                blp.Opacity = -1;
            }            

            // Have the base class load its properties
            base.LoadProperties(baseState, suppressEvents);

            // Now load our properties, and announce them to the world
            bool raiseBlendOp = false;

            if (blp.BlendOp.GetType() != _properties.BlendOp.GetType())
            {
                if (!suppressEvents)
                {
                    raiseBlendOp = true;
                    OnPropertyChanging(BitmapLayerProperties.BlendOpName);
                }
            }

            _properties = (BitmapLayerProperties)blp.Clone();
            _compiledBlendOp = null;

            Invalidate();

            if (raiseBlendOp)
            {
                OnPropertyChanged(BitmapLayerProperties.BlendOpName);
            }
        }

        public void SetBlendOp(UserBlendOp blendOp)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("BitmapLayer");
            }

            if (blendOp.GetType() == _properties.BlendOp.GetType()) return;
            OnPropertyChanging(BitmapLayerProperties.BlendOpName);
            _properties.BlendOp = blendOp;
            _compiledBlendOp = null;
            Invalidate();
            OnPropertyChanged(BitmapLayerProperties.BlendOpName);
        }

        public override object Clone()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("BitmapLayer");
            }

            return new BitmapLayer(this);
        }

        public Surface Surface
        {
            get
            {
                if (_disposed)
                {
                    throw new ObjectDisposedException("BitmapLayer");
                }

                return _surface;
            }
        }

        public UserBlendOp BlendOp
        {
            get
            {
                if (_disposed)
                {
                    throw new ObjectDisposedException("BitmapLayer");
                }

                return _properties.BlendOp;
            }
        }

        public BitmapLayer(int width, int height)
            : this(width, height, ColorBgra.FromBgra(255, 255, 255, 0))
        {
        }

        public BitmapLayer(int width, int height, ColorBgra fillColor)
            : base(width, height)
        {
            _surface = new Surface(width, height);
            // clear to see-through white, 0x00ffffff
            Surface.Clear(fillColor);
            _properties = new BitmapLayerProperties(UserBlendOps.CreateDefaultBlendOp());
        }

        /// <summary>
        /// Creates a new BitmapLayer of the same size as the given Surface, and copies the 
        /// pixels from the given Surface.
        /// </summary>
        /// <param name="surface">The Surface to copy pixels from.</param>
        public BitmapLayer(Surface surface)
            : this(surface, false)
        {
        }

        /// <summary>
        /// Creates a new BitmapLayer of the same size as the given Surface, and either
        /// copies the pixels of the given Surface or takes ownership of it.
        /// </summary>
        /// <param name="surface">The Surface.</param>
        /// <param name="takeOwnership">
        /// true to take ownership of the surface (make sure to Dispose() it yourself), or
        /// false to copy its pixels
        /// </param>
        public BitmapLayer(Surface surface, bool takeOwnership)
            : base(surface.Width, surface.Height)
        {
            _surface = takeOwnership ? surface : surface.Clone();

            _properties = new BitmapLayerProperties(UserBlendOps.CreateDefaultBlendOp());
        }

        protected BitmapLayer(BitmapLayer copyMe)
            : base(copyMe)
        {
            _surface = copyMe.Surface.Clone();
            _properties = (BitmapLayerProperties)copyMe._properties.Clone();
        }

        protected unsafe override void RenderImpl(RenderArgs args, Rectangle roi)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("BitmapLayer");
            }

            if (Opacity == 0)
            {
                return;
            }

            if (_compiledBlendOp == null)
            {
                CompileBlendOp();
            }

            for (int y = roi.Top; y < roi.Bottom; ++y)
            {
                ColorBgra *dstPtr = args.Surface.GetPointAddressUnchecked(roi.Left, y);
                ColorBgra *srcPtr = _surface.GetPointAddressUnchecked(roi.Left, y);

                _compiledBlendOp.Apply(dstPtr, srcPtr, roi.Width);
            }
        }

        protected unsafe override void RenderImpl(RenderArgs args, Rectangle[] rois, int startIndex, int length)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("BitmapLayer");
            }

            if (Opacity == 0)
            {
                return;
            }

            if (_compiledBlendOp == null)
            {
                CompileBlendOp();
            }

            for (int i = startIndex; i < startIndex + length; ++i)
            {
                Rectangle roi = rois[i];

                for (int y = roi.Top; y < roi.Bottom; ++y)
                {
                    ColorBgra *dstPtr = args.Surface.GetPointAddressUnchecked(roi.Left, y);
                    ColorBgra *srcPtr = _surface.GetPointAddressUnchecked(roi.Left, y);

                    _compiledBlendOp.Apply(dstPtr, srcPtr, roi.Width);
                }
            }
        }

        public override PdnBaseForm CreateConfigDialog()
        {
            var blpd = new BitmapLayerPropertiesDialog {Layer = this};
            return blpd;
        }

        public void OnDeserialization(object sender)
        {
            if (_properties.Opacity != -1)
            {
                PushSuppressPropertyChanged();
                Opacity = (byte)_properties.Opacity;
                _properties.Opacity = -1;
                PopSuppressPropertyChanged();
            }

            _compiledBlendOp = null;
        }
    }
}
Back to Top