Bug 826093 (Part 2) - Create a static utility class for image operations. r=joe, sr=bz
authorSeth Fowler <seth@mozilla.com>
Tue, 26 Mar 2013 11:56:46 -0700
changeset 126627 98258ea50c1e628f9f2ffd8b88138e9055c1171b
parent 126626 5e573640e390ebb86fd23761b281d5eaaa9e6511
child 126628 9129587531fe3ba91c18ad7eafdd8ab680c5a9e9
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersjoe, bz
bugs826093
milestone22.0a1
Bug 826093 (Part 2) - Create a static utility class for image operations. r=joe, sr=bz
image/src/ClippedImage.h
image/src/FrozenImage.h
image/src/ImageFactory.cpp
image/src/ImageFactory.h
image/src/ImageOps.cpp
image/src/ImageOps.h
image/src/Makefile.in
image/src/imgRequestProxy.cpp
--- a/image/src/ClippedImage.h
+++ b/image/src/ClippedImage.h
@@ -68,15 +68,15 @@ private:
                           const SVGImageContext* aSVGContext,
                           uint32_t aWhichFrame,
                           uint32_t aFlags);
 
   nsIntRect   mClip;              // The region to clip to.
   Maybe<bool> mShouldClip;        // Memoized ShouldClip() if present.
 
   friend class DrawSingleTileCallback;
-  friend class ImageFactory;
+  friend class ImageOps;
 };
 
 } // namespace image
 } // namespace mozilla
 
 #endif // MOZILLA_IMAGELIB_CLIPPEDIMAGE_H_
--- a/image/src/FrozenImage.h
+++ b/image/src/FrozenImage.h
@@ -57,15 +57,15 @@ public:
   NS_IMETHOD GetAnimationMode(uint16_t* aAnimationMode) MOZ_OVERRIDE;
   NS_IMETHOD SetAnimationMode(uint16_t aAnimationMode) MOZ_OVERRIDE;
   NS_IMETHOD ResetAnimation() MOZ_OVERRIDE;
 
 protected:
   FrozenImage(Image* aImage) : ImageWrapper(aImage) { }
 
 private:
-  friend class ImageFactory;
+  friend class ImageOps;
 };
 
 } // namespace image
 } // namespace mozilla
 
 #endif // MOZILLA_IMAGELIB_FROZENIMAGE_H_
--- a/image/src/ImageFactory.cpp
+++ b/image/src/ImageFactory.cpp
@@ -16,17 +16,16 @@
 #include "nsMimeTypes.h"
 #include "nsIURI.h"
 #include "nsIRequest.h"
 
 #include "imgIContainer.h"
 #include "imgStatusTracker.h"
 #include "RasterImage.h"
 #include "VectorImage.h"
-#include "FrozenImage.h"
 #include "Image.h"
 #include "nsMediaFragmentURIParser.h"
 
 #include "ImageFactory.h"
 
 namespace mozilla {
 namespace image {
 
@@ -171,23 +170,16 @@ GetContentSize(nsIRequest* aRequest)
     }
   }
 
   // Fallback - neither http nor file. We'll use dynamic allocation.
   return 0;
 }
 
 /* static */ already_AddRefed<Image>
-ImageFactory::Freeze(Image* aImage)
-{
-  nsRefPtr<Image> frozenImage = new FrozenImage(aImage);
-  return frozenImage.forget();
-}
-
-/* static */ already_AddRefed<Image>
 ImageFactory::CreateRasterImage(nsIRequest* aRequest,
                                 imgStatusTracker* aStatusTracker,
                                 const nsCString& aMimeType,
                                 nsIURI* aURI,
                                 uint32_t aImageFlags,
                                 uint32_t aInnerWindowId)
 {
   nsresult rv;
--- a/image/src/ImageFactory.h
+++ b/image/src/ImageFactory.h
@@ -41,24 +41,16 @@ public:
   /**
    * Creates a new image which isn't associated with a URI or loaded through
    * the usual image loading mechanism.
    *
    * @param aMimeType      The mimetype of the image.
    */
   static already_AddRefed<Image> CreateAnonymousImage(const nsCString& aMimeType);
 
-  /**
-   * Creates a version of an existing image which does not animate and is frozen
-   * at the first frame.
-   *
-   * @param aImage         The existing image.
-   */
-  static already_AddRefed<Image> Freeze(Image* aImage);
-
 private:
   // Factory functions that create specific types of image containers.
   static already_AddRefed<Image> CreateRasterImage(nsIRequest* aRequest,
                                                    imgStatusTracker* aStatusTracker,
                                                    const nsCString& aMimeType,
                                                    nsIURI* aURI,
                                                    uint32_t aImageFlags,
                                                    uint32_t aInnerWindowId);
new file mode 100644
--- /dev/null
+++ b/image/src/ImageOps.cpp
@@ -0,0 +1,48 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "imgIContainer.h"
+#include "ClippedImage.h"
+#include "FrozenImage.h"
+#include "Image.h"
+
+#include "ImageOps.h"
+
+namespace mozilla {
+namespace image {
+
+/* static */ already_AddRefed<Image>
+ImageOps::Freeze(Image* aImage)
+{
+  nsRefPtr<Image> frozenImage = new FrozenImage(aImage);
+  return frozenImage.forget();
+}
+
+/* static */ already_AddRefed<imgIContainer>
+ImageOps::Freeze(imgIContainer* aImage)
+{
+  nsCOMPtr<imgIContainer> frozenImage =
+    new FrozenImage(static_cast<Image*>(aImage));
+  return frozenImage.forget();
+}
+
+/* static */ already_AddRefed<Image>
+ImageOps::Clip(Image* aImage, nsIntRect aClip)
+{
+  nsRefPtr<Image> clippedImage = new ClippedImage(aImage, aClip);
+  return clippedImage.forget();
+}
+
+/* static */ already_AddRefed<imgIContainer>
+ImageOps::Clip(imgIContainer* aImage, nsIntRect aClip)
+{
+  nsCOMPtr<imgIContainer> clippedImage =
+    new ClippedImage(static_cast<Image*>(aImage), aClip);
+  return clippedImage.forget();
+}
+
+} // namespace image
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/image/src/ImageOps.h
@@ -0,0 +1,49 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef MOZILLA_IMAGELIB_IMAGEOPS_H_
+#define MOZILLA_IMAGELIB_IMAGEOPS_H_
+
+#include "nsCOMPtr.h"
+#include "nsRect.h"
+
+class imgIContainer;
+
+namespace mozilla {
+namespace image {
+
+class Image;
+
+class ImageOps
+{
+public:
+  /**
+   * Creates a version of an existing image which does not animate and is frozen
+   * at the first frame.
+   *
+   * @param aImage         The existing image.
+   */
+  static already_AddRefed<Image> Freeze(Image* aImage);
+  static already_AddRefed<imgIContainer> Freeze(imgIContainer* aImage);
+
+  /**
+   * Creates a clipped version of an existing image. Animation is unaffected.
+   *
+   * @param aImage         The existing image.
+   * @param aClip          The rectangle to clip the image against.
+   */
+  static already_AddRefed<Image> Clip(Image* aImage, nsIntRect aClip);
+  static already_AddRefed<imgIContainer> Clip(imgIContainer* aImage, nsIntRect aClip);
+
+private:
+  // This is a static utility class, so disallow instantiation.
+  virtual ~ImageOps() = 0;
+};
+
+} // namespace image
+} // namespace mozilla
+
+#endif // MOZILLA_IMAGELIB_IMAGEOPS_H_
--- a/image/src/Makefile.in
+++ b/image/src/Makefile.in
@@ -15,22 +15,24 @@ FORCE_STATIC_LIB = 1
 MODULE_NAME	= nsImageLib2Module
 LIBXUL_LIBRARY  = 1
 FAIL_ON_WARNINGS = 1
 
 
 EXPORTS		=  imgLoader.h \
 		   imgRequest.h \
 		   imgRequestProxy.h \
+		   ImageOps.h \
 		   $(NULL)
 
 CPPSRCS		= \
 			Image.cpp \
 			ImageFactory.cpp \
                         ImageMetadata.cpp \
+			ImageOps.cpp \
 			ImageWrapper.cpp \
 			ClippedImage.cpp \
 			Decoder.cpp \
 			DiscardTracker.cpp \
 			FrozenImage.cpp \
 			RasterImage.cpp \
 			ScriptedNotificationObserver.cpp \
 			SVGDocumentWrapper.cpp \
--- a/image/src/imgRequestProxy.cpp
+++ b/image/src/imgRequestProxy.cpp
@@ -13,17 +13,17 @@
 #include "nsIMultiPartChannel.h"
 
 #include "nsString.h"
 #include "nsXPIDLString.h"
 #include "nsReadableUtils.h"
 #include "nsCRT.h"
 
 #include "Image.h"
-#include "ImageFactory.h"
+#include "ImageOps.h"
 #include "nsError.h"
 #include "ImageLogging.h"
 
 #include "nspr.h"
 
 using namespace mozilla::image;
 
 // The split of imgRequestProxy and imgRequestProxyStatic means that
@@ -910,17 +910,17 @@ imgRequestProxy::GetStaticRequest(imgReq
   // Check for errors in the image. Callers code rely on GetStaticRequest
   // failing in this case, though with FrozenImage there's no technical reason
   // for it anymore.
   if (image->HasError()) {
     return NS_ERROR_FAILURE;
   }
 
   // We are animated. We need to create a frozen version of this image.
-  nsRefPtr<Image> frozenImage = ImageFactory::Freeze(image);
+  nsRefPtr<Image> frozenImage = ImageOps::Freeze(image);
 
   // Create a static imgRequestProxy with our new extracted frame.
   nsCOMPtr<nsIPrincipal> currentPrincipal;
   GetImagePrincipal(getter_AddRefs(currentPrincipal));
   nsRefPtr<imgRequestProxy> req = new imgRequestProxyStatic(frozenImage,
                                                             currentPrincipal);
   req->Init(nullptr, nullptr, mURI, nullptr);