~ubuntu-branches/ubuntu/trusty/pdfmod/trusty

« back to all changes in this revision

Viewing changes to lib/PdfSharp/PdfSharp.Drawing/XImage.cs

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2010-06-18 03:44:46 UTC
  • Revision ID: james.westby@ubuntu.com-20100618034446-bogifrsscpayp361
Tags: upstream-0.8.3
ImportĀ upstreamĀ versionĀ 0.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region PDFsharp - A .NET library for processing PDF
 
2
//
 
3
// Authors:
 
4
//   Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
 
5
//
 
6
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
 
7
//
 
8
// http://www.pdfsharp.com
 
9
// http://sourceforge.net/projects/pdfsharp
 
10
//
 
11
// Permission is hereby granted, free of charge, to any person obtaining a
 
12
// copy of this software and associated documentation files (the "Software"),
 
13
// to deal in the Software without restriction, including without limitation
 
14
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
15
// and/or sell copies of the Software, and to permit persons to whom the
 
16
// Software is furnished to do so, subject to the following conditions:
 
17
//
 
18
// The above copyright notice and this permission notice shall be included
 
19
// in all copies or substantial portions of the Software.
 
20
//
 
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
22
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
23
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 
24
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
25
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
26
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 
27
// DEALINGS IN THE SOFTWARE.
 
28
#endregion
 
29
 
 
30
using System;
 
31
using System.Diagnostics;
 
32
using System.Runtime.InteropServices;
 
33
using System.IO;
 
34
using System.ComponentModel;
 
35
#if GDI
 
36
using System.Drawing;
 
37
using System.Drawing.Drawing2D;
 
38
using System.Drawing.Imaging;
 
39
#endif
 
40
#if WPF
 
41
using System.Windows;
 
42
using System.Windows.Media;
 
43
using System.Windows.Media.Imaging;
 
44
#endif
 
45
using PdfSharp.Internal;
 
46
using PdfSharp.Fonts.TrueType;
 
47
using PdfSharp.Pdf;
 
48
using PdfSharp.Pdf.IO;
 
49
using PdfSharp.Pdf.Advanced;
 
50
 
 
51
// WPFHACK
 
52
#pragma warning disable 162
 
53
 
 
54
namespace PdfSharp.Drawing
 
55
{
 
56
  /// <summary>
 
57
  /// Defines an object used to draw image files (bmp, png, jpeg, gif) and PDF forms.
 
58
  /// An abstract base class that provides functionality for the Bitmap and Metafile descended classes.
 
59
  /// </summary>
 
60
  public class XImage : IDisposable
 
61
  {
 
62
    /// <summary>
 
63
    /// Initializes a new instance of the <see cref="XImage"/> class.
 
64
    /// </summary>
 
65
    protected XImage()
 
66
    {
 
67
    }
 
68
 
 
69
#if GDI
 
70
    /// <summary>
 
71
    /// Initializes a new instance of the <see cref="XImage"/> class from a GDI+ image.
 
72
    /// </summary>
 
73
    XImage(Image image)
 
74
    {
 
75
      this.gdiImage = image;
 
76
#if WPF
 
77
      this.wpfImage = ImageHelper.CreateBitmapSource(image);
 
78
#endif
 
79
      Initialize();
 
80
    }
 
81
#endif
 
82
 
 
83
#if WPF
 
84
    /// <summary>
 
85
    /// Initializes a new instance of the <see cref="XImage"/> class from a WPF image.
 
86
    /// </summary>
 
87
    XImage(BitmapSource image)
 
88
    {
 
89
      this.wpfImage = image;
 
90
      Initialize();
 
91
    }
 
92
#endif
 
93
 
 
94
    XImage(string path)
 
95
    {
 
96
      path = Path.GetFullPath(path);
 
97
      if (!File.Exists(path))
 
98
        throw new FileNotFoundException(PSSR.FileNotFound(path), path);
 
99
 
 
100
      this.path = path;
 
101
 
 
102
      //FileStream file = new FileStream(filename, FileMode.Open);
 
103
      //BitsLength = (int)file.Length;
 
104
      //Bits = new byte[BitsLength];
 
105
      //file.Read(Bits, 0, BitsLength);
 
106
      //file.Close();
 
107
#if GDI
 
108
      this.gdiImage = Image.FromFile(path);
 
109
#endif
 
110
#if WPF
 
111
      this.wpfImage = new BitmapImage(new Uri(path));
 
112
#endif
 
113
 
 
114
#if false
 
115
      float vres = this.image.VerticalResolution;
 
116
      float hres = this.image.HorizontalResolution;
 
117
      SizeF size = this.image.PhysicalDimension;
 
118
      int flags  = this.image.Flags;
 
119
      Size sz    = this.image.Size;
 
120
      GraphicsUnit units = GraphicsUnit.Millimeter;
 
121
      RectangleF rect = this.image.GetBounds(ref units);
 
122
      int width = this.image.Width;
 
123
#endif
 
124
      Initialize();
 
125
    }
 
126
 
 
127
    XImage(Stream stream)
 
128
    {
 
129
      // Create a dummy unique path
 
130
      this.path = "*" + Guid.NewGuid().ToString("B");
 
131
 
 
132
#if GDI
 
133
      this.gdiImage = Image.FromStream(stream);
 
134
#endif
 
135
#if WPF
 
136
      throw new NotImplementedException();
 
137
      //this.wpfImage = new BitmapImage(new Uri(path));
 
138
#endif
 
139
 
 
140
#if true_
 
141
      float vres = this.image.VerticalResolution;
 
142
      float hres = this.image.HorizontalResolution;
 
143
      SizeF size = this.image.PhysicalDimension;
 
144
      int flags  = this.image.Flags;
 
145
      Size sz    = this.image.Size;
 
146
      GraphicsUnit units = GraphicsUnit.Millimeter;
 
147
      RectangleF rect = this.image.GetBounds(ref units);
 
148
      int width = this.image.Width;
 
149
#endif
 
150
      Initialize();
 
151
    }
 
152
 
 
153
#if GDI
 
154
#if UseGdiObjects
 
155
    /// <summary>
 
156
    /// Implicit conversion from Image to XImage.
 
157
    /// </summary>
 
158
    public static implicit operator XImage(Image image)
 
159
    {
 
160
      return new XImage(image);
 
161
    }
 
162
#endif
 
163
 
 
164
    /// <summary>
 
165
    /// Conversion from Image to XImage.
 
166
    /// </summary>
 
167
    public static XImage FromGdiPlusImage(Image image)
 
168
    {
 
169
      return new XImage(image);
 
170
    }
 
171
#endif
 
172
 
 
173
#if WPF
 
174
    /// <summary>
 
175
    /// Conversion from BitmapSource to XImage.
 
176
    /// </summary>
 
177
    public static XImage FromBitmapSource(BitmapSource image)
 
178
    {
 
179
      return new XImage(image);
 
180
    }
 
181
#endif
 
182
 
 
183
    /// <summary>
 
184
    /// Creates an image from the specified file.
 
185
    /// </summary>
 
186
    /// <param name="path">The path to a BMP, PNG, GIF, JPEG, TIFF, or PDF file.</param>
 
187
    public static XImage FromFile(string path)
 
188
    {
 
189
      if (PdfReader.TestPdfFile(path) > 0)
 
190
        return new XPdfForm(path);
 
191
      return new XImage(path);
 
192
    }
 
193
 
 
194
    /// <summary>
 
195
    /// Tests if a file exist. Supports PDF files with page number suffix.
 
196
    /// </summary>
 
197
    /// <param name="path">The path to a BMP, PNG, GIF, JPEG, TIFF, or PDF file.</param>
 
198
    public static bool ExistsFile(string path)
 
199
    {
 
200
      if (PdfReader.TestPdfFile(path) > 0)
 
201
        return true;
 
202
      return File.Exists(path);
 
203
    }
 
204
 
 
205
    void Initialize()
 
206
    {
 
207
#if GDI
 
208
      if (this.gdiImage != null)
 
209
      {
 
210
        // ImageFormat has no overridden Equals...
 
211
        string guid = this.gdiImage.RawFormat.Guid.ToString("B").ToUpper();
 
212
        switch (guid)
 
213
        {
 
214
          case "{B96B3CAA-0728-11D3-9D7B-0000F81EF32E}":  // memoryBMP
 
215
          case "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}":  // bmp
 
216
          case "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}":  // png
 
217
            this.format = XImageFormat.Png;
 
218
            break;
 
219
 
 
220
          case "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}":  // jpeg
 
221
            this.format = XImageFormat.Jpeg;
 
222
            break;
 
223
 
 
224
          case "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}":  // gif
 
225
            this.format = XImageFormat.Gif;
 
226
            break;
 
227
 
 
228
          case "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}":  // tiff
 
229
            this.format = XImageFormat.Tiff;
 
230
            break;
 
231
 
 
232
          case "{B96B3CB5-0728-11D3-9D7B-0000F81EF32E}":  // icon
 
233
            this.format = XImageFormat.Icon;
 
234
            break;
 
235
 
 
236
          case "{B96B3CAC-0728-11D3-9D7B-0000F81EF32E}":  // emf
 
237
          case "{B96B3CAD-0728-11D3-9D7B-0000F81EF32E}":  // wmf
 
238
          case "{B96B3CB2-0728-11D3-9D7B-0000F81EF32E}":  // exif
 
239
          case "{B96B3CB3-0728-11D3-9D7B-0000F81EF32E}":  // photoCD
 
240
          case "{B96B3CB4-0728-11D3-9D7B-0000F81EF32E}":  // flashPIX
 
241
 
 
242
          default:
 
243
            throw new InvalidOperationException("Unsupported image format.");
 
244
        }
 
245
        return;
 
246
      }
 
247
#endif
 
248
#if WPF
 
249
      if (this.wpfImage != null)
 
250
      {
 
251
        string pixelFormat = this.wpfImage.Format.ToString();
 
252
        switch (pixelFormat)
 
253
        {
 
254
          case "Bgr32":
 
255
          case "Bgra32":
 
256
          case "Pbgra32":
 
257
          case "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}":  // bmp
 
258
          case "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}":  // png
 
259
            this.format = XImageFormat.Png;
 
260
            break;
 
261
 
 
262
          case "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}":  // jpeg
 
263
            this.format = XImageFormat.Jpeg;
 
264
            break;
 
265
 
 
266
          case "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}":  // gif
 
267
          case"BlackWhite":
 
268
          case "Indexed1":
 
269
          case "Indexed4":
 
270
          case "Indexed8":
 
271
            this.format = XImageFormat.Gif;
 
272
            break;
 
273
 
 
274
          case "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}":  // tiff
 
275
            this.format = XImageFormat.Tiff;
 
276
            break;
 
277
 
 
278
          case "{B96B3CB5-0728-11D3-9D7B-0000F81EF32E}":  // icon
 
279
            this.format = XImageFormat.Icon;
 
280
            break;
 
281
 
 
282
          case "{B96B3CAC-0728-11D3-9D7B-0000F81EF32E}":  // emf
 
283
          case "{B96B3CAD-0728-11D3-9D7B-0000F81EF32E}":  // wmf
 
284
          case "{B96B3CB2-0728-11D3-9D7B-0000F81EF32E}":  // exif
 
285
          case "{B96B3CB3-0728-11D3-9D7B-0000F81EF32E}":  // photoCD
 
286
          case "{B96B3CB4-0728-11D3-9D7B-0000F81EF32E}":  // flashPIX
 
287
 
 
288
          default:
 
289
            Debug.Assert(false, "Unknown pixel format: " + pixelFormat);
 
290
            this.format = XImageFormat.Gif;
 
291
            break;// throw new InvalidOperationException("Unsupported image format.");
 
292
        }
 
293
      }
 
294
#endif
 
295
    }
 
296
 
 
297
    /// <summary>
 
298
    /// under construction
 
299
    /// </summary>
 
300
    public void Dispose()
 
301
    {
 
302
      Dispose(true);
 
303
      //GC.SuppressFinalize(this);
 
304
    }
 
305
 
 
306
    /// <summary>
 
307
    /// Disposes underlying GDI+ object.
 
308
    /// </summary>
 
309
    protected virtual void Dispose(bool disposing)
 
310
    {
 
311
      if (!this.disposed)
 
312
        this.disposed = true;
 
313
 
 
314
#if GDI
 
315
      if (this.gdiImage != null)
 
316
      {
 
317
        this.gdiImage.Dispose();
 
318
        this.gdiImage = null;
 
319
      }
 
320
#endif
 
321
#if WPF
 
322
#endif
 
323
    }
 
324
    bool disposed;
 
325
 
 
326
 
 
327
    /// <summary>
 
328
    /// Gets the width of the image.
 
329
    /// </summary>
 
330
    [Obsolete("Use either PixelWidth or PointWidth. Temporarily obsolete because of rearrangements for WPF. Currently same as PixelWidth, but will become PointWidth in future releases of PDFsharp.")]
 
331
    public virtual double Width
 
332
    {
 
333
      get
 
334
      {
 
335
#if GDI && WPF
 
336
        double gdiWidth = this.gdiImage.Width;
 
337
        double wpfWidth = this.wpfImage.PixelWidth;
 
338
        Debug.Assert(gdiWidth == wpfWidth);
 
339
        return wpfWidth;
 
340
#endif
 
341
#if GDI && !WPF
 
342
        return this.gdiImage.Width;
 
343
#endif
 
344
#if WPF && !GDI
 
345
        return this.wpfImage.PixelWidth;
 
346
#endif
 
347
      }
 
348
    }
 
349
 
 
350
    /// <summary>
 
351
    /// Gets the height of the image.
 
352
    /// </summary>
 
353
    [Obsolete("Use either PixelHeight or PointHeight. Temporarily obsolete because of rearrangements for WPF. Currently same as PixelHeight, but will become PointHeight in future releases of PDFsharp.")]
 
354
    public virtual double Height
 
355
    {
 
356
      get
 
357
      {
 
358
#if GDI && WPF
 
359
        double gdiHeight = this.gdiImage.Height;
 
360
        double wpfHeight = this.wpfImage.PixelHeight;
 
361
        Debug.Assert(gdiHeight == wpfHeight);
 
362
        return wpfHeight;
 
363
#endif
 
364
#if GDI && !WPF
 
365
        return this.gdiImage.Height;
 
366
#endif
 
367
#if WPF && !GDI
 
368
        return this.wpfImage.PixelHeight;
 
369
#endif
 
370
      }
 
371
    }
 
372
 
 
373
    /// <summary>
 
374
    /// Gets the width of the image in point.
 
375
    /// </summary>
 
376
    public virtual double PointWidth
 
377
    {
 
378
      get
 
379
      {
 
380
#if GDI && WPF
 
381
        double gdiWidth = this.gdiImage.Width * 72 / this.gdiImage.HorizontalResolution;
 
382
        double wpfWidth = this.wpfImage.Width * 72.0 / 96.0;
 
383
        //Debug.Assert(gdiWidth == wpfWidth);
 
384
        Debug.Assert(DoubleUtil.AreRoughlyEqual(gdiWidth, wpfWidth, 5));
 
385
        return wpfWidth;
 
386
#endif
 
387
#if GDI && !WPF
 
388
        return this.gdiImage.Width * 72 / this.gdiImage.HorizontalResolution;
 
389
#endif
 
390
#if WPF && !GDI
 
391
        Debug.Assert(Math.Abs(this.wpfImage.PixelWidth * 72 / this.wpfImage.DpiX - this.wpfImage.Width * 72.0 / 96.0) < 0.001);
 
392
        return this.wpfImage.Width * 72.0 / 96.0;
 
393
#endif
 
394
      }
 
395
    }
 
396
 
 
397
    /// <summary>
 
398
    /// Gets the height of the image in point.
 
399
    /// </summary>
 
400
    public virtual double PointHeight
 
401
    {
 
402
      get
 
403
      {
 
404
#if GDI && WPF
 
405
        double gdiHeight = this.gdiImage.Height * 72 / this.gdiImage.HorizontalResolution;
 
406
        double wpfHeight = this.wpfImage.Height * 72.0 / 96.0;
 
407
        Debug.Assert(DoubleUtil.AreRoughlyEqual(gdiHeight, wpfHeight, 5));
 
408
        return wpfHeight;
 
409
#endif
 
410
#if GDI && !WPF
 
411
        return this.gdiImage.Height * 72 / this.gdiImage.HorizontalResolution;
 
412
#endif
 
413
#if WPF && !GDI
 
414
        Debug.Assert(Math.Abs(this.wpfImage.PixelHeight * 72 / this.wpfImage.DpiY - this.wpfImage.Height * 72.0 / 96.0) < 0.001);
 
415
        return this.wpfImage.Height * 72.0 / 96.0;
 
416
#endif
 
417
      }
 
418
    }
 
419
 
 
420
    /// <summary>
 
421
    /// Gets the width of the image in pixels.
 
422
    /// </summary>
 
423
    public virtual int PixelWidth
 
424
    {
 
425
      get
 
426
      {
 
427
#if GDI && WPF
 
428
        int gdiWidth = this.gdiImage.Width;
 
429
        int wpfWidth = this.wpfImage.PixelWidth;
 
430
        Debug.Assert(gdiWidth == wpfWidth);
 
431
        return wpfWidth;
 
432
#endif
 
433
#if GDI && !WPF
 
434
        return this.gdiImage.Width;
 
435
#endif
 
436
#if WPF && !GDI
 
437
        return this.wpfImage.PixelWidth;
 
438
#endif
 
439
      }
 
440
    }
 
441
 
 
442
    /// <summary>
 
443
    /// Gets the height of the image in pixels.
 
444
    /// </summary>
 
445
    public virtual int PixelHeight
 
446
    {
 
447
      get
 
448
      {
 
449
#if GDI && WPF
 
450
        int gdiHeight = this.gdiImage.Height;
 
451
        int wpfHeight = this.wpfImage.PixelHeight;
 
452
        Debug.Assert(gdiHeight == wpfHeight);
 
453
        return wpfHeight;
 
454
#endif
 
455
#if GDI && !WPF
 
456
        return this.gdiImage.Height;
 
457
#endif
 
458
#if WPF && !GDI
 
459
        return this.wpfImage.PixelHeight;
 
460
#endif
 
461
      }
 
462
    }
 
463
 
 
464
    /// <summary>
 
465
    /// Gets the size in point of the image.
 
466
    /// </summary>
 
467
    public virtual XSize Size
 
468
    {
 
469
      get { return new XSize(PointWidth, PointHeight); }
 
470
    }
 
471
 
 
472
    /// <summary>
 
473
    /// Gets the horizontal resolution of the image.
 
474
    /// </summary>
 
475
    public virtual double HorizontalResolution
 
476
    {
 
477
      get
 
478
      {
 
479
#if GDI && WPF
 
480
        double gdiResolution = this.gdiImage.HorizontalResolution;
 
481
        double wpfResolution = this.wpfImage.PixelWidth * 96.0 / this.wpfImage.Width;
 
482
        Debug.Assert(gdiResolution == wpfResolution);
 
483
        return wpfResolution;
 
484
#endif
 
485
#if GDI && !WPF
 
486
        return this.gdiImage.HorizontalResolution;
 
487
#endif
 
488
#if WPF && !GDI
 
489
        return this.wpfImage.DpiX; //.PixelWidth * 96.0 / this.wpfImage.Width;
 
490
#endif
 
491
      }
 
492
    }
 
493
 
 
494
    /// <summary>
 
495
    /// Gets the vertical resolution of the image.
 
496
    /// </summary>
 
497
    public virtual double VerticalResolution
 
498
    {
 
499
      get
 
500
      {
 
501
#if GDI && WPF
 
502
        double gdiResolution = this.gdiImage.VerticalResolution;
 
503
        double wpfResolution = this.wpfImage.PixelHeight * 96.0 / this.wpfImage.Height;
 
504
        Debug.Assert(gdiResolution == wpfResolution);
 
505
        return wpfResolution;
 
506
#endif
 
507
#if GDI && !WPF
 
508
        return this.gdiImage.VerticalResolution;
 
509
#endif
 
510
#if WPF && !GDI
 
511
        return this.wpfImage.DpiY; //.PixelHeight * 96.0 / this.wpfImage.Height;
 
512
#endif
 
513
      }
 
514
    }
 
515
 
 
516
    /// <summary>
 
517
    /// Gets or sets a flag indicating whether image interpolation is to be performed. 
 
518
    /// </summary>
 
519
    public virtual bool Interpolate
 
520
    {
 
521
      get { return this.interpolate; }
 
522
      set { this.interpolate = value; }
 
523
    }
 
524
    bool interpolate = true;
 
525
 
 
526
    /// <summary>
 
527
    /// Gets the format of the image.
 
528
    /// </summary>
 
529
    public XImageFormat Format
 
530
    {
 
531
      get { return this.format; }
 
532
    }
 
533
    XImageFormat format;
 
534
 
 
535
#if DEBUG_
 
536
    // TEST
 
537
    internal void CreateAllImages(string name)
 
538
    {
 
539
      if (this.image != null)
 
540
      {
 
541
        this.image.Save(name + ".bmp", ImageFormat.Bmp);
 
542
        this.image.Save(name + ".emf", ImageFormat.Emf);
 
543
        this.image.Save(name + ".exif", ImageFormat.Exif);
 
544
        this.image.Save(name + ".gif", ImageFormat.Gif);
 
545
        this.image.Save(name + ".ico", ImageFormat.Icon);
 
546
        this.image.Save(name + ".jpg", ImageFormat.Jpeg);
 
547
        this.image.Save(name + ".png", ImageFormat.Png);
 
548
        this.image.Save(name + ".tif", ImageFormat.Tiff);
 
549
        this.image.Save(name + ".wmf", ImageFormat.Wmf);
 
550
        this.image.Save(name + "2.bmp", ImageFormat.MemoryBmp);
 
551
      }
 
552
    }
 
553
#endif
 
554
#if GDI
 
555
    internal Image gdiImage;
 
556
#endif
 
557
#if WPF
 
558
    internal BitmapSource wpfImage;
 
559
#endif
 
560
 
 
561
    /// <summary>
 
562
    /// If path starts with '*' the image is created from a stream and the path is a GUID.
 
563
    /// </summary>
 
564
    internal string path;
 
565
 
 
566
    /// <summary>
 
567
    /// Cache PdfImageTable.ImageSelector to speed up finding the right PdfImage
 
568
    /// if this image is used more than once.
 
569
    /// </summary>
 
570
    internal PdfImageTable.ImageSelector selector;
 
571
  }
 
572
}
 
 
b'\\ No newline at end of file'