Line data Source code
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 0 : 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.
97 : * @return An image buffer derived from the input stream.
98 : */
99 : static already_AddRefed<ImageBuffer>
100 : CreateImageBuffer(nsIInputStream* aInputStream);
101 :
102 : /**
103 : * Decodes an image's metadata from an nsIInputStream into the given
104 : * structure. This function may be called off-main-thread.
105 : *
106 : * @param aInputStream An input stream containing an encoded image.
107 : * @param aMimeType The MIME type of the image.
108 : * @param aMetadata Where the image metadata is stored upon success.
109 : * @return The status of the operation.
110 : */
111 : static nsresult
112 : DecodeMetadata(nsIInputStream* aInputStream,
113 : const nsACString& aMimeType,
114 : ImageMetadata& aMetadata);
115 :
116 : /**
117 : * Same as above but takes an ImageBuffer instead of nsIInputStream.
118 : */
119 : static nsresult
120 : DecodeMetadata(ImageBuffer* aBuffer,
121 : const nsACString& aMimeType,
122 : ImageMetadata& aMetadata);
123 :
124 : /**
125 : * Decodes an image from an nsIInputStream directly into a SourceSurface,
126 : * without ever creating an Image or imgIContainer (which are mostly
127 : * main-thread-only). That means that this function may be called
128 : * off-main-thread.
129 : *
130 : * @param aInputStream An input stream containing an encoded image.
131 : * @param aMimeType The MIME type of the image.
132 : * @param aFlags Flags of the imgIContainer::FLAG_DECODE_* variety.
133 : * @return A SourceSurface containing the first frame of the image at its
134 : * intrinsic size, or nullptr if the image cannot be decoded.
135 : */
136 : static already_AddRefed<gfx::SourceSurface>
137 : DecodeToSurface(nsIInputStream* aInputStream,
138 : const nsACString& aMimeType,
139 : uint32_t aFlags,
140 : const Maybe<gfx::IntSize>& aSize = Nothing());
141 :
142 : /**
143 : * Same as above but takes an ImageBuffer instead of nsIInputStream.
144 : */
145 : static already_AddRefed<gfx::SourceSurface>
146 : DecodeToSurface(ImageBuffer* aBuffer,
147 : const nsACString& aMimeType,
148 : uint32_t aFlags,
149 : const Maybe<gfx::IntSize>& aSize = Nothing());
150 :
151 : private:
152 : class ImageBufferImpl;
153 :
154 : // This is a static utility class, so disallow instantiation.
155 : virtual ~ImageOps() = 0;
156 : };
157 :
158 : } // namespace image
159 : } // namespace mozilla
160 :
161 : #endif // mozilla_image_ImageOps_h
|