Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/image/ImageOps.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2
 *
3
 * This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_image_ImageOps_h
8
#define mozilla_image_ImageOps_h
9
10
#include "nsCOMPtr.h"
11
#include "nsRect.h"
12
#include "ImageMetadata.h"
13
14
class gfxDrawable;
15
class imgIContainer;
16
class nsIInputStream;
17
18
namespace mozilla {
19
20
namespace gfx {
21
class SourceSurface;
22
}
23
24
namespace image {
25
26
class Image;
27
struct Orientation;
28
class SourceBuffer;
29
30
class ImageOps
31
{
32
public:
33
  class ImageBuffer {
34
  public:
35
    NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ImageOps::ImageBuffer);
36
  protected:
37
    friend class ImageOps;
38
39
0
    ImageBuffer() { }
40
0
    virtual ~ImageBuffer() { }
41
42
    virtual already_AddRefed<SourceBuffer> GetSourceBuffer() const = 0;
43
  };
44
45
  /**
46
   * Creates a version of an existing image which does not animate and is frozen
47
   * at the first frame.
48
   *
49
   * @param aImage         The existing image.
50
   */
51
  static already_AddRefed<Image> Freeze(Image* aImage);
52
  static already_AddRefed<imgIContainer> Freeze(imgIContainer* aImage);
53
54
  /**
55
   * Creates a clipped version of an existing image. Animation is unaffected.
56
   *
57
   * @param aImage           The existing image.
58
   * @param aClip            The rectangle to clip the image against.
59
   * @param aSVGViewportSize The specific viewort size of aImage. Unless aImage
60
   *                         is a vector image without intrinsic size, this
61
   *                         argument should be pass as Nothing().
62
   */
63
  static already_AddRefed<Image> Clip(Image* aImage, nsIntRect aClip,
64
                                      const Maybe<nsSize>& aSVGViewportSize =
65
                                        Nothing());
66
  static already_AddRefed<imgIContainer> Clip(imgIContainer* aImage,
67
                                              nsIntRect aClip,
68
                                              const Maybe<nsSize>& aSVGViewportSize =
69
                                                Nothing());
70
71
  /**
72
   * Creates a version of an existing image which is rotated and/or flipped to
73
   * the specified orientation.
74
   *
75
   * @param aImage         The existing image.
76
   * @param aOrientation   The desired orientation.
77
   */
78
  static already_AddRefed<Image> Orient(Image* aImage,
79
                                        Orientation aOrientation);
80
  static already_AddRefed<imgIContainer> Orient(imgIContainer* aImage,
81
                                                Orientation aOrientation);
82
83
  /**
84
   * Creates an image from a gfxDrawable.
85
   *
86
   * @param aDrawable      The gfxDrawable.
87
   */
88
  static already_AddRefed<imgIContainer>
89
  CreateFromDrawable(gfxDrawable* aDrawable);
90
91
  /**
92
   * Create a buffer to be used with DecodeMetadata and DecodeToSurface. Reusing
93
   * an ImageBuffer representing the given input stream is more efficient if one
94
   * has multiple Decode* calls to make on that stream.
95
   *
96
   * @param aInputStream An input stream containing an encoded image. The
97
   * ownership is taken.
98
   * @return An image buffer derived from the input stream.
99
   */
100
  static already_AddRefed<ImageBuffer>
101
  CreateImageBuffer(already_AddRefed<nsIInputStream> aInputStream);
102
103
  /**
104
   * Decodes an image's metadata from an nsIInputStream into the given
105
   * structure. This function may be called off-main-thread.
106
   *
107
   * @param aInputStream An input stream containing an encoded image. Ownership
108
   * is taken.
109
   * @param aMimeType The MIME type of the image.
110
   * @param aMetadata Where the image metadata is stored upon success.
111
   * @return The status of the operation.
112
   */
113
  static nsresult
114
  DecodeMetadata(already_AddRefed<nsIInputStream> aInputStream,
115
                 const nsACString& aMimeType,
116
                 ImageMetadata& aMetadata);
117
118
  /**
119
   * Same as above but takes an ImageBuffer instead of nsIInputStream.
120
   */
121
  static nsresult
122
  DecodeMetadata(ImageBuffer* aBuffer,
123
                 const nsACString& aMimeType,
124
                 ImageMetadata& aMetadata);
125
126
  /**
127
   * Decodes an image from an nsIInputStream directly into a SourceSurface,
128
   * without ever creating an Image or imgIContainer (which are mostly
129
   * main-thread-only). That means that this function may be called
130
   * off-main-thread.
131
   *
132
   * @param aInputStream An input stream containing an encoded image. The
133
   * ownership is taken.
134
   * @param aMimeType The MIME type of the image.
135
   * @param aFlags Flags of the imgIContainer::FLAG_DECODE_* variety.
136
   * @return A SourceSurface containing the first frame of the image at its
137
   *         intrinsic size, or nullptr if the image cannot be decoded.
138
   */
139
  static already_AddRefed<gfx::SourceSurface>
140
  DecodeToSurface(already_AddRefed<nsIInputStream> aInputStream,
141
                  const nsACString& aMimeType,
142
                  uint32_t aFlags,
143
                  const Maybe<gfx::IntSize>& aSize = Nothing());
144
145
  /**
146
   * Same as above but takes an ImageBuffer instead of nsIInputStream.
147
   */
148
  static already_AddRefed<gfx::SourceSurface>
149
  DecodeToSurface(ImageBuffer* aBuffer,
150
                  const nsACString& aMimeType,
151
                  uint32_t aFlags,
152
                  const Maybe<gfx::IntSize>& aSize = Nothing());
153
154
private:
155
  class ImageBufferImpl;
156
157
  // This is a static utility class, so disallow instantiation.
158
  virtual ~ImageOps() = 0;
159
};
160
161
} // namespace image
162
} // namespace mozilla
163
164
#endif // mozilla_image_ImageOps_h