image/ImageOps.cpp
author Ryan VanderMeulen <ryanvm@gmail.com>
Wed, 17 Jan 2018 15:55:38 -0500
changeset 399709 ef04f3ad847540c876c130b7d140a960d922a024
parent 387065 23e2e65211f5c7787d45f56da9ea56bcf5f73a94
child 403584 858d629f761ddb77258cbb3e5c557df1ead5b2df
permissions -rw-r--r--
Backed out 20 changesets (bug 1411654) for incorrect android:debuggable. r=nalexander, a=RyanVM Backed out changeset cfad693be918 (bug 1411654) Backed out changeset 55776829a744 (bug 1411654) Backed out changeset c5bf85d56fed (bug 1411654) Backed out changeset c270f97bb0da (bug 1411654) Backed out changeset fde9bf9c14c3 (bug 1411654) Backed out changeset 01836fd98c63 (bug 1411654) Backed out changeset 730a70767743 (bug 1411654) Backed out changeset 690e265c684c (bug 1411654) Backed out changeset f918500d9cf5 (bug 1411654) Backed out changeset cec2b8828cc8 (bug 1411654) Backed out changeset 76085ddd5ac7 (bug 1411654) Backed out changeset 2b37201606f5 (bug 1411654) Backed out changeset d0d513d1c379 (bug 1411654) Backed out changeset e7b0cc801cf1 (bug 1411654) Backed out changeset 901b304603d9 (bug 1411654) Backed out changeset 373c9a71d945 (bug 1411654) Backed out changeset 3dc3beab95f8 (bug 1411654) Backed out changeset 22a861db1573 (bug 1411654) Backed out changeset 0850b319efd4 (bug 1411654) Backed out changeset d276d3deba05 (bug 1411654)

/* -*- 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 "ImageOps.h"

#include "ClippedImage.h"
#include "DecodePool.h"
#include "Decoder.h"
#include "DecoderFactory.h"
#include "DynamicImage.h"
#include "FrozenImage.h"
#include "IDecodingTask.h"
#include "Image.h"
#include "ImageMetadata.h"
#include "imgIContainer.h"
#include "mozilla/gfx/2D.h"
#include "nsStreamUtils.h"
#include "OrientedImage.h"
#include "SourceBuffer.h"

using namespace mozilla::gfx;

namespace mozilla {
namespace image {

/* static */ already_AddRefed<Image>
ImageOps::Freeze(Image* aImage)
{
  RefPtr<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,
               const Maybe<nsSize>& aSVGViewportSize)
{
  RefPtr<Image> clippedImage = new ClippedImage(aImage, aClip, aSVGViewportSize);
  return clippedImage.forget();
}

/* static */ already_AddRefed<imgIContainer>
ImageOps::Clip(imgIContainer* aImage, nsIntRect aClip,
               const Maybe<nsSize>& aSVGViewportSize)
{
  nsCOMPtr<imgIContainer> clippedImage =
    new ClippedImage(static_cast<Image*>(aImage), aClip, aSVGViewportSize);
  return clippedImage.forget();
}

/* static */ already_AddRefed<Image>
ImageOps::Orient(Image* aImage, Orientation aOrientation)
{
  RefPtr<Image> orientedImage = new OrientedImage(aImage, aOrientation);
  return orientedImage.forget();
}

/* static */ already_AddRefed<imgIContainer>
ImageOps::Orient(imgIContainer* aImage, Orientation aOrientation)
{
  nsCOMPtr<imgIContainer> orientedImage =
    new OrientedImage(static_cast<Image*>(aImage), aOrientation);
  return orientedImage.forget();
}

/* static */ already_AddRefed<imgIContainer>
ImageOps::CreateFromDrawable(gfxDrawable* aDrawable)
{
  nsCOMPtr<imgIContainer> drawableImage = new DynamicImage(aDrawable);
  return drawableImage.forget();
}

class ImageOps::ImageBufferImpl final : public ImageOps::ImageBuffer {
public:
  explicit ImageBufferImpl(already_AddRefed<SourceBuffer> aSourceBuffer)
    : mSourceBuffer(aSourceBuffer)
  { }

protected:
  ~ImageBufferImpl() override { }

  already_AddRefed<SourceBuffer> GetSourceBuffer() const override
  {
    RefPtr<SourceBuffer> sourceBuffer = mSourceBuffer;
    return sourceBuffer.forget();
  }

private:
  RefPtr<SourceBuffer> mSourceBuffer;
};

/* static */ already_AddRefed<ImageOps::ImageBuffer>
ImageOps::CreateImageBuffer(already_AddRefed<nsIInputStream> aInputStream)
{
  nsCOMPtr<nsIInputStream> inputStream = Move(aInputStream);
  MOZ_ASSERT(inputStream);

  nsresult rv;

  // Prepare the input stream.
  if (!NS_InputStreamIsBuffered(inputStream)) {
    nsCOMPtr<nsIInputStream> bufStream;
    rv = NS_NewBufferedInputStream(getter_AddRefs(bufStream),
                                   inputStream.forget(), 1024);
    if (NS_WARN_IF(NS_FAILED(rv))) {
      return nullptr;
    }
  }

  // Figure out how much data we've been passed.
  uint64_t length;
  rv = inputStream->Available(&length);
  if (NS_FAILED(rv) || length > UINT32_MAX) {
    return nullptr;
  }

  // Write the data into a SourceBuffer.
  RefPtr<SourceBuffer> sourceBuffer = new SourceBuffer();
  sourceBuffer->ExpectLength(length);
  rv = sourceBuffer->AppendFromInputStream(inputStream, length);
  if (NS_FAILED(rv)) {
    return nullptr;
  }
  // Make sure our sourceBuffer is marked as complete.
  if (sourceBuffer->IsComplete()) {
    NS_WARNING("The SourceBuffer was unexpectedly marked as complete. This may "
               "indicate either an OOM condition, or that imagelib was not "
               "initialized properly.");
    return nullptr;
  }
  sourceBuffer->Complete(NS_OK);

  RefPtr<ImageBuffer> imageBuffer = new ImageBufferImpl(sourceBuffer.forget());
  return imageBuffer.forget();
}

/* static */ nsresult
ImageOps::DecodeMetadata(already_AddRefed<nsIInputStream> aInputStream,
                         const nsACString& aMimeType,
                         ImageMetadata& aMetadata)
{
  nsCOMPtr<nsIInputStream> inputStream = Move(aInputStream);
  RefPtr<ImageBuffer> buffer = CreateImageBuffer(inputStream.forget());
  return DecodeMetadata(buffer, aMimeType, aMetadata);
}

/* static */ nsresult
ImageOps::DecodeMetadata(ImageBuffer* aBuffer,
                         const nsACString& aMimeType,
                         ImageMetadata& aMetadata)
{
  if (!aBuffer) {
    return NS_ERROR_FAILURE;
  }

  RefPtr<SourceBuffer> sourceBuffer = aBuffer->GetSourceBuffer();
  if (NS_WARN_IF(!sourceBuffer)) {
    return NS_ERROR_FAILURE;
  }

  // Create a decoder.
  DecoderType decoderType =
    DecoderFactory::GetDecoderType(PromiseFlatCString(aMimeType).get());
  RefPtr<Decoder> decoder =
    DecoderFactory::CreateAnonymousMetadataDecoder(decoderType,
                                                   WrapNotNull(sourceBuffer));
  if (!decoder) {
    return NS_ERROR_FAILURE;
  }

  // Run the decoder synchronously.
  RefPtr<IDecodingTask> task = new AnonymousDecodingTask(WrapNotNull(decoder));
  task->Run();
  if (!decoder->GetDecodeDone() || decoder->HasError()) {
    return NS_ERROR_FAILURE;
  }

  aMetadata = decoder->GetImageMetadata();
  if (aMetadata.GetNativeSizes().IsEmpty() && aMetadata.HasSize()) {
    aMetadata.AddNativeSize(aMetadata.GetSize());
  }

  return NS_OK;
}

/* static */ already_AddRefed<gfx::SourceSurface>
ImageOps::DecodeToSurface(already_AddRefed<nsIInputStream> aInputStream,
                          const nsACString& aMimeType,
                          uint32_t aFlags,
                          const Maybe<IntSize>& aSize /* = Nothing() */)
{
  nsCOMPtr<nsIInputStream> inputStream = Move(aInputStream);
  RefPtr<ImageBuffer> buffer = CreateImageBuffer(inputStream.forget());
  return DecodeToSurface(buffer, aMimeType, aFlags, aSize);
}

/* static */ already_AddRefed<gfx::SourceSurface>
ImageOps::DecodeToSurface(ImageBuffer* aBuffer,
                          const nsACString& aMimeType,
                          uint32_t aFlags,
                          const Maybe<IntSize>& aSize /* = Nothing() */)
{
  if (!aBuffer) {
    return nullptr;
  }

  RefPtr<SourceBuffer> sourceBuffer = aBuffer->GetSourceBuffer();
  if (NS_WARN_IF(!sourceBuffer)) {
    return nullptr;
  }

  // Create a decoder.
  DecoderType decoderType =
    DecoderFactory::GetDecoderType(PromiseFlatCString(aMimeType).get());
  RefPtr<Decoder> decoder =
    DecoderFactory::CreateAnonymousDecoder(decoderType,
                                           WrapNotNull(sourceBuffer),
                                           aSize, ToSurfaceFlags(aFlags));
  if (!decoder) {
    return nullptr;
  }

  // Run the decoder synchronously.
  RefPtr<IDecodingTask> task = new AnonymousDecodingTask(WrapNotNull(decoder));
  task->Run();
  if (!decoder->GetDecodeDone() || decoder->HasError()) {
    return nullptr;
  }

  // Pull out the surface.
  RawAccessFrameRef frame = decoder->GetCurrentFrameRef();
  if (!frame) {
    return nullptr;
  }

  RefPtr<SourceSurface> surface = frame->GetSourceSurface();
  if (!surface) {
    return nullptr;
  }

  return surface.forget();
}

} // namespace image
} // namespace mozilla